Python中的MySQLConnector使用介绍
来源:互联网 发布:大连住电软件 编辑:程序博客网 时间:2024/06/03 23:39
MySQL Connector/Python 是 MySQL 官方提供的 Python 连接 MySQL 数据库的驱动程序了,很多初学者对于 在python中连接mysql数据库还是有点为难了,下文我们只需要了解这个MySQLConnector模块问题就可以解决了。相较于MySQLdb模块来说,其支持python3,而MySQLdb目前只支持到python2.7版本。这里就结合示例,总结下MySQL Connector模块的用法。
1、数据库连接
连接数据库的代码如下
import mysql.connectorconfig={'host':'127.0.0.1',#默认127.0.0.1 'user':'root', 'password':'123456', 'port':3306 ,#默认即为3306 'database':'test', 'charset':'utf8'#默认即为utf8 }try: cnn=mysql.connector.connect(**config)except mysql.connector.Error as e: print('connect fails!{}'.format(e))
连接方法上和MySQLdb模块略有不同。MySQLdb使用的是=号,这里使用的是 : 号。
2、创建表
下面我们根据上面新建的一个数据库连接创建一张名为student的表:
sql_create_table='CREATE TABLE `student` \(`id` int(10) NOT NULL AUTO_INCREMENT,\`name` varchar(10) DEFAULT NULL,\`age` int(3) DEFAULT NULL,\PRIMARY KEY (`id`)) \ENGINE=MyISAM DEFAULT CHARSET=utf8'cursor=cnn.cursor()try: cursor.execute(sql_create_table)except mysql.connector.Error as e: print('create table orange fails!{}'.format(e))
3、插入数据
插入数据的语法上和MySQLdb上基本上是一样的:
cursor=cnn.cursor()try: '第一种:直接字符串插入方式' sql_insert1="insert into student (name, age) values ('orange', 20)" cursor.execute(sql_insert1) '第二种:元组连接插入方式' sql_insert2="insert into student (name, age) values (%s, %s)" #此处的%s为占位符,而不是格式化字符串,所以age用%s data=('shiki',25) cursor.execute(sql_insert2,data) '第三种:字典连接插入方式' sql_insert3="insert into student (name, age) values (%(name)s, %(age)s)" data={'name':'mumu','age':30} cursor.execute(sql_insert3,data) #如果数据库引擎为Innodb,执行完成后需执行cnn.commit()进行事务提交except mysql.connector.Error as e: print('insert datas error!{}'.format(e))finally: cursor.close() cnn.close()同样,MySQL Connector也支持多次插入,同样其使用的也是cursor.executemany,示例如下:
stmt='insert into student (name, age) values (%s,%s)'data=[ ('Lucy',21), ('Tom',22), ('Lily',21)]cursor.executemany(stmt,data)
4、查询操作
cursor=cnn.cursor()try: sql_query='select id,name from student where age > %s' cursor.execute(sql_query,(21,)) for id,name in cursor: print ('%s\'s age is older than 25,and her/his id is %d'%(name,id))except mysql.connector.Error as e: print('query error!{}'.format(e))finally: cursor.close() cnn.close()
5、删除操作
cursor=cnn.cursor()try: sql_delete='delete from student where name = %(name)s and age < %(age)s' data={'name':'orange','age':24} cursor.execute(sql_delete,data)except mysql.connector.Error as e: print('delete error!{}'.format(e))finally: cursor.close() cnn.close()
阅读全文
0 0
- Python中的MySQLConnector使用介绍
- Python 的MySQLConnector模块使用方法详解
- Python中MySQLConnector模块使用方法详解
- mysqlconnector-python出现Unread result found错误
- 使用MySQLConnector/C++连接MySQL数据库
- Python列表内涵实际中的使用介绍
- Python中的模块介绍和使用
- ubuntu下c++使用mysqlconnector连接mysql数据库
- Python中的lambda介绍
- python中的reduce介绍:
- python中的import介绍
- 简单介绍Python中的len()函数的使用
- jsonwidget-python使用介绍
- Python-UiAutomator使用介绍
- Python中的推导式介绍
- python中的字典详细介绍
- python中的select相关介绍
- Python中的time模块介绍
- MarkDown页内跳转实现
- 邮票分你一半
- reason: 'UICollectionView must be initialized with a non-nil layout parameter'
- 原码,反码,补码,详解
- Myeclipse导入包出现不了Referenced Libraries
- Python中的MySQLConnector使用介绍
- ZCMU—1949
- 【IOS学习之常见问题】 App上架之发布App到Appstore 2(Application Loader的使用)
- LeetCode
- 掌握Maven_1
- Java基础拾遗:线程相关问题
- PX4源码分析6_uorb通信机制
- Hadoop--Hive安装错误解析
- JSP内置对象:在同一个页面实现成绩输入及总成绩显示