python3.2.5 + mysql-connector-python-1.2.2-py3.2 连接mysql基本用法
2014-07-08 00:58
686 查看
今天又发现另一个python连接mysql的工具,mysql-connector-python,这是一个由python官方推出的一款连接mysql的工具,对高版本Python有较好的支持。基本使用方法与前文所述的pymysql类似。
在实验中发现conn.commit()语句仅针对存储引擎为InnoDB的数据库明显有效。实验内容如下(所有编码均为utf-8):
创建数据库:
创建数据表:
实验程序:
在实验中发现conn.commit()语句仅针对存储引擎为InnoDB的数据库明显有效。实验内容如下(所有编码均为utf-8):
创建数据库:
create database ere default charset=utf8
创建数据表:
create table test ( id int primary key auto_increment, name varchar(10) not null )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
实验程序:
import mysql.connector; conn=mysql.connector.connect(host='localhost',user='root',passwd='123',db='ere',charset='utf8'); cur=conn.cursor(); try:# 清空表中原有数据 cur.execute('delete from test'); conn.commit(); print('删除成功'); except Exception as exp: print('删除失败:'); print(exp) try: cur.execute("insert into test values(null,'中国')");#添加记录 conn.commit(); print('插入成功',cur.lastrowid); except Exception as exp: print('插入失败'); print(exp); <pre name="code" class="python">cur.execute('select * from test');#查询表中记录 for each in cur: print(each);
相关文章推荐
- Python连接MySQL ---mysql-connector-python-2.1.3-py3.4-winx64.msi
- Python 3.2: 使用pymysql连接Mysql
- php连接mysql count与下拉基本用法
- Python 3.2: 使用pymysql连接Mysql
- 关于python 连接 mysql pip3安装mysql-connector 纠正廖雪峰安装小细节
- Python连接MySql(.py文件直接操作MySql)
- php连接mysql 基本用法
- python对mysql.connector的简单封装,读改增删基本操作
- MySQL官方文档推荐的一个Python连接MySQL的方法—mysql.connector
- python-mysql基本用法
- php连接mysql 插入 更新 删除基本用法
- python3-连接MySQL(mysql.connector与MySQLdb区别)
- Python连接MySQL数据库(mysql-connector-python)
- Python3 通过 pika 连接 RabbitMQ 的基本用法
- win7的python连接mysql用法
- python下使用mysql.connector 安装以及连接数据库的操作
- Java连接Mysql的基本用法
- python_fullstack数据库(七)-Python连接MySQL数据库之pymysql模块使用
- python-mysql基本用法
- python --- mysql启动与基本用法