Python中的MySQLConnector使用介绍
MySQL Connector/Python 是 MySQL 官方提供的 Python 连接 MySQL 数据库的驱动程序了,很多初学者对于 在python中连接mysql数据库还是有点为难了,下文我们只需要了解这个MySQLConnector模块问题就可以解决了。相较于MySQLdb模块来说,其支持python3,而MySQLdb目前只支持到python2.7版本。这里就结合示例,总结下MySQL
Connector模块的用法。
1、数据库连接
连接数据库的代码如下
import mysql.connector
config={'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()
Python中的MySQLConnector使用介绍的更多相关文章
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python中关于函数的介绍
一.什么是函数 当我们在日常工作中编写代码时,有没有发现这种情况,写了一套代码,却发现里面有很多段代码出现了有规律的重复,这样就不符合一个合格程序员的标准了,一个合格的程序员编写的代码最重 ...
- Python中__new__与__init__介绍
在python2.x中,从object继承得来的类称为新式类(如class A(object))不从object继承得来的类称为经典类(如class A()) 新式类跟经典类的差别主要是以下几点: 1 ...
- python中property属性的介绍及其应用
Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回. 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的 ...
- python中的元类介绍
类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段,在python中也是成立的. class ObjectCreator: pass my_object = ObjectCre ...
- Python中的lambda函数介绍
Lambda函数,即Lambda 表达式(lambda expression),是一个匿名函数(不存在函数名的函数),Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lam ...
- Python中set的功能介绍
Set的功能介绍 1.集合的两种函数(方法) 1. 集合的内置函数 交集 格式:x.__and__(y)等同于x&y 例如:s1 = {'a',1,} s2 = {'b',1,} s3 = { ...
- Python中dict的功能介绍
Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...
随机推荐
- A1pass大大对黑客学习的建议
本文转自:http://bbs.hackav.com/thread-92-1-1.html 菜鸟不可怕,可怕的是你认为自己一辈子都是菜鸟.每个高手都是从菜鸟进化过来的,就算是现在黑客界的泰斗们当年也无 ...
- oem 重建
OracleDBControl启动失败to local from URL=http://your-url.co 方法: emca -deconfig dbcontrol db -repos d ...
- unbuntu中如何像Windows一样顺畅的切换中英文输入法
1.首先在unbuntu安装搜狗拼音输入法(这个不用教了) 2.点击右上角的搜狗拼音的图标点击设置进入设置页面 3.选择高级 4.选择Fcitx设置 5.添加输入法英语(美国) 6.在设置中选择按键, ...
- centos7使用haproxy1.7.5实现反向代理负载均衡实战
使用haproxy实现反向代理负载均衡实战环境准备:两台虚拟机 # yum install -y gcc glibc gcc-c++ make screen tree lrzsz node1源码编译安 ...
- CentOS 6.5 rsync+inotify实现数据实时同步备份
CentOS 6.5 rsync+inotify实现数据实时同步备份 rsync remote sync 远程同步,同步是把数据从缓冲区同步到磁盘上去的.数据在内存缓存区完成之后还没有写入到磁盘 ...
- spark简单总结—短小精悍
Spark是基于内存计算的大数据并行计算框架.因为其基于内存计算,较Hadoop中MapReduce计算框架具有更高的实时性,同时保证了高效容错性和可伸缩性.从2009年诞生于AMPLab到现在已经成 ...
- poj2342 没有上司的舞会 树形dp基础
#include<iostream> #include<cstring> #include<cstdio> #include<vector> using ...
- noip 2017 时间复杂度
自认为是少有的复杂的代码 这题思想很简单,就是大模拟 对于for循环,一行读入4个字符串,然后分类讨论: ①:如果是一个正常的O(n),那么累计n的指数加1 ②:如果是一个常数级别的,那么继续循环,但 ...
- python 全栈开发,Day113(方法和函数的区别,yield,反射)
一.方法和函数的区别 面向对象 初级 class StarkConfig(object): def __init__(self,model_class): self.model_class = mod ...
- spring-boot集成spring-data-jpa
参考这个就行, http://blog.csdn.net/wazz753/article/details/72472411 ps:集成过程中pom文件,我加入的内容如下,两个都需要,实体类记得加注解和 ...