二十二、python mysql-connector的安装和使用
Python和sql整合
安装mysql:
安装navcate-for-mysql可视化操作工具:
开发包:
mysql-connector:
http://dev.mysql.com/downloads/connector/python
MYSQLdb:
https://pypi.python.org/pypi/MySQL-python
SQLAlchemy:
http://www.sqlalchemy.org
windows中安装mysql-connector
windows中安装mysql-connector-python-2.1.6-py2.7-winx64.msi 直接安装,一直next即可。
操作4步骤:
创建连接
获取游标
执行sql
关闭连接
-------------------------------------------------------
#from mysql import connector #也可以
import mysql.connector
#Python连接数据库的方式有好几个模块可以提供,包括pymysql,MySQLdb,和MySQL官网的mysql-connector-python
#获取连接conn
conn = mysql.connector.connect(host='192.168.216.7',port=3306,user='root',password='root',database='test',charset='utf8')
conn.autocommit=True #可以获取连接后设置为自动提交
#获取一个cursor
cursor = conn.cursor()
#1、创建表
sql_create = 'CREATE TABLE `user` ( \
`id` int(11) NOT NULL AUTO_INCREMENT, \
`name` varchar(20) DEFAULT NULL, \
`age` int(1) DEFAULT NULL, \
PRIMARY KEY (`id`) \
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 \
'
#提交表创建
#cursor.execute(sql_create)
########################################编写插入sql语句
#a、直接字符串插入方式
#ssqltext = 'insert into user(id,name,age) VALUES(3,"你好 ",20)'
sql_insert1 = 'insert into user(name,age) VALUES("你好 ",20)' #id为递增,不用写都可以
#使用cursor执行sql语句
#cursor.execute(sql_insert1)
#b、元组方式插入
sql_insert2 = 'insert into user(name,age) VALUES(%s,%s) ' #%s不是格式化符,而是占位符。所以age也是%s
data = ('你好tuple',18)
#cursor.execute(sql_insert2, data)
#c、使用字典方式插入
sql_insert3 = 'insert into user(name,age) VALUES(%(name)s,%(age)s) ' #%()s : 中的字段与字典中赋值字段相同
data1 = {'name':"你好dict",'age':16}
#cursor.execute(sql_insert3, data1)
##############################################支持多次插入 使用
sql_insert4 = 'insert into user(name,age) VALUES(%s,%s) ' #%s不是格式化符,而是占位符。所以age也是%s
data2 = [('你好tuple',18),('你t',16),('好uple',12)]
#cursor.executemany(sql_insert4,data2)
#引入faker包的Factory模块 来生成伪数据
#安装 pip install fake-factory(老版本) 或者 pip install fake
#卸载 pip uninstall fake-factory 或者 pip uninstall fake ,重报错信息可知道,需要重新安装Faker
# ImportError: The ``fake-factory`` package is now called ``Faker``
#from faker import factory
#userFaker = factory.Factory.create() #pip install fake-factory包中的
#print userFaker.name()
from faker import Factory
userFaker = Factory.create()
#print userFaker.name(),userFaker.email(),userFaker.address(),userFaker.text()
sql_insert5 = 'insert into pyuser(name,email,address,content) VALUES(%s,%s,%s,%s) '
userFakerData = [(userFaker.name(),userFaker.email(),userFaker.address(),userFaker.text()) for i in range(1,20)]
#print userFakerData
#cursor.executemany(sql_insert5,userFakerData)
######################################更新数据,,尝试批量更新???
sql_update = 'update user set age = 16'
#cursor.execute(sql_update)
#####################################查询
sql_select = 'select id,name from user where age > %s and age < %s'
cursor.execute(sql_select, (16, 18))
for id,name in cursor:
print '%s age is older than 18,and her/his id is %d'%(name, id)
##################################删除 尝试删除多个???
sql_delete ='delete from user where age=%(age)s and name=%(name)s'
deleteData = {'age': 18, 'name': u'你好tuple'}
cursor.execute(sql_delete,deleteData)
#如果数据库为innerdb类型,则需要提交(如果没有设置自动提交)
#conn.commit()
#关闭cursor和conn
cursor.close()
conn.close()