安装第三方连接mysqldb 的库
yum install -y MySQL-python
apt-get install -y python-mysqldb
gunzip MySQL-python-1.2.2.tar.gz tar -xvf MySQL-python-1.2.2.tar cd MySQL-python-1.2.2 python setup.py build python setup.py install
pip install wheel
pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
python27 -m pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
def connect_mysql(): db_config = { 'host': '192.168.48.128', 'port': 3306, 'user': 'xiang', 'passwd': '123456', 'db': 'python', 'charset': 'utf8' } cnx = MySQLdb.connect(**db_config) return cnx
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='toor',db='mysql',port=3306) ##连接数据库 cur=conn.cursor() ##使用cursor()方法获取操做游标 cur.execute('select User,Host from user') ##使用execute方法执行SQL语句 qur_result = cur.fetchall() ##使用 fetchone() 方法获取一条数据库 for record in qur_result: print record cur.close() #关闭操做 conn.close() ##关闭连接 except MySQLdb.Error,e: print 'Myslq Rrror Msg:',e
建立数据库,建立表,插入数据python
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb try: db=MySQLdb.connect(host='localhost',user='root',passwd='toor',port=3306) ##连接数据库 cur=db.cursor() ##使用cursor()方法获取操做游标 # 1 建立库 cur.execute('create database if not exists test') ##建立数据 test db.select_db('test') ##切换数据库 # 2 建立表 cur.execute(''' create table stu_info( id int(5) not null primary key auto_increment, name char(10) not null, phone int(12) not null, class char(20) ); ''') # 3 插入单条数据,.rollback()插入错误回滚 try: info = ('abc',123456,'linux') sql = 'insert into stu_info values(null,%s,%s,%s)',info cur.execute(sql) ##执行sql语句插入数据 db.commit() ##提交,提交后没法回滚 except: db.rollback() ##发生错误时回滚 # 4 插入多条数据,.executemany()重复执行带参数的单条命令 try: values_list = [] for i in range(10): values_list.append(('abc_%s' i,'12345670%s' i,'python')) cur.executemany('insert into stu_info values(null.%s,%s,%s)',valuse_list) db.commit() except: db.rollback() # 5 .fetchone() 查询返回一条结果 cur.execute('select * from stu_info') ##查询插入后的表 qur_result = cur.fetchone() ##使用 fetchone() 方法获取一条数据库 for record in qur_result: ##循环打印出获取到的每条数据 print record # 6 .fetchall() 返回所有结果 cur.execute('select * from stu_info') ##查询插入后的表 qur_result = cur.fetchall() ##使用 fetchall() 所有的返回结果行 print qur_result # 7 .scroll() 移动指针到某一行,查询行后面的结果 cur.execute('select * from stu_info') cur.scroll(14,'python') print cur.fetchall() cur.close() ##关闭操做 db.close() ##关闭连接 except MySQLdb.Error,e: print 'Myslq Rrror Msg:',e
commit() 提交
rollback() 回滚mysql
cursor: 用来执行命令的方法linux
.callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
.execute(self, query, args):执行单条sql语句,接收的参数为sql语句自己和使用的参数列表,返回值为受影响的行数
.executemany(self, query, args):执行单挑sql语句,可是重复执行参数列表里的参数,返回值为受影响的行数
.nextset(self):移动到下一个结果集
cursor用来接收返回值的方法:
.fetchall(self):接收所有的返回结果行.
.fetchmany(self, size=None):接收size条返回结果行.若是size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.
.fetchone(self):返回一条结果行.
.scroll(self, value, mode='relative'):移动指针到某一行.若是mode='relative',则表示从当前所在行移动value条,若是 mode='absolute',则表示从结果集的第一行移动value条.sql