安装:
MySQLdb:MySQLdb是用于python链接MySQL数据库的接口
安装包:
mysql-connector-python-1.0.12-py3.3 直接单击安装即可。
验证:
新建一个python文件,输入import mysql.connector看是否会报错,如果不报错,那么就是安装成功了!
查询数据库操作步骤:
- 提供链接数据库信息:ip地址、端口、数据库名字、登录名、密码,都写到一个名为config的字典里面。
- 利用mysql.connector.connect()方法创建链接数据库的字符串cnn:
- 创建游标:cursor=cnn.cursor()—>每次操作都要获取游标才能进行
- 进行第一个简单的操作:查询表student1 sql语句:slect * from student1
- 调用方法:cursor.excute(“sql语句”)
- 展示数据:fetchall()与fetchone()两个函数都可以把数据读取出来
数据以列表的形式展现出来,每条数据作为列表的一个元素,注意列表的属性。 - 关闭连接:要养成这个好习惯,不然数据库的连接数不够用
其他操作:
- where条件查询:元祖传递数据、%s占位符
- 加入异常跑错处理,判断基数:mysql.connector.Error
Try…except…finally
插入数据:
1)直插入法: sql_insert1=“insert into student1 (name, age) values (‘华华’, 22)”
2)元组插入法: sql_insert1=“insert into student1 (name, age) values (%s, %s)” ,data=(‘威威’,18)
3)字典插入法: sql_insert3="insert into student (name, age) values (%(name)s, %(age)s)
data={‘name’:‘不语’,‘age’:20}
4) 列表插入法: sql_insert4 =‘insert into student (name, age) values (%s,%s)’
data=[
(‘美丽’,21),
(‘嫦娥’,22),
(‘吴刚’,21)]
举个例子:连接数据库–>创建表–>insert数据–>查询数据–>删除数据
import mysql.connector
'''1.数据库要配置的信息,存到字典里'''
config={'host':'127.0.0.1',
'user':'root',
'password':'root',
'port':'3306',
'database':'python',
'charset':'utf8',
}
try:
cnn=mysql.connector.connect(**config)#创建数据库的连接字符,**是必须要有的
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
'''2.创建表'''
sql_create_table='CREATE TABLE student2\
(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)#execute就是执行操作的意思,括号里面的参数是sql语句
except mysql.connector.Error as e:
print('create table student fails!{}'.format(e))
'''3.插入数据'''
cursor=cnn.cursor()
try:
'第一种:直接字符串插入方式'
sql_insert1="insert into student2 (name, age) values ('orange', 20)"
cursor.execute(sql_insert1)
'第二种:元组连接插入方式'
sql_insert2="insert into student2 (name, age) values (%s, %s)"
#此处的%s为占位符,而不是格式化字符串,所以age用%s
data=('shiki',25)
cursor.execute(sql_insert2,data)
'第三种:字典连接插入方式'
sql_insert3="insert into student2 (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()
'''多行插入数据,用executemanya关键字'''
cursor=cnn.cursor()
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_delete1='delete from student where id>%s'
cursor.execute(sql_delete1,(1,))
except mysql.connector.Error as e:
print('insert datas error!{}'.format(e))
finally:
cursor.close()
cnn.close()