python操作mysql数据库的常用方法使用详解
生活随笔
收集整理的這篇文章主要介紹了
python操作mysql数据库的常用方法使用详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python操作mysql數(shù)據(jù)庫
安裝mysql: apt-get install mysql-server
下載安裝mysql
python操作mysql模塊:MySQL-python-1.2.3.win32-py2.7.exe 或 MySQL-python-1.2.3.win-amd64-py2.7.exe
安裝python開發(fā)環(huán)境,具體可參考:windows下搭建eclipse關(guān)于python的開發(fā)環(huán)境及初始化參數(shù)配置
注意修改你連接的數(shù)據(jù)庫,主機IP,用戶名,密碼。
請注意一定要有conn.commit()這句來提交事務(wù),要不然不能真正的插入數(shù)據(jù)
#!/usr/bin/env python #_*_ coding:utf-8 _*_import MySQLdbtry:conn = MySQLdb.Connect(host='192.168.8.40',user='root',passwd='root',db='mysql',port=3306,charset='utf8')cur = conn.cursor()conn.select_db('python')count = cur.execute('select * from test') print 'there has %s rows record' % countresult = cur.fetchone()print resultprint 'id: %s info %s' % resultresult2 = cur.fetchmany(3)for record in result2:print recordprint '=='*10cur.scroll(0,mode='absolute')result3 = cur.fetchall()for record in result3:print record[0] ,'---',record[1]conn.commit()cur.close()conn.close() except MySQLdb.Error,e:print 'mysql error msg: %d,%s' % (e.args[0],e.args[1])
查詢后中文會顯示亂碼,但在數(shù)據(jù)庫中卻是正常的,發(fā)現(xiàn)用一個屬性有可搞定:
在Python代碼?
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python') 中加一個屬性:
?改為:
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8')?
charset是要跟你數(shù)據(jù)庫的編碼一樣,如果是數(shù)據(jù)庫是gb2312 ,則寫charset='gb2312'。
然后,這個連接對象也提供了對事務(wù)操作的支持,標(biāo)準(zhǔn)的方法
commit() 提交
rollback() 回滾
cursor用來執(zhí)行命令的方法:
callproc(self, procname, args):用來執(zhí)行存儲過程,接收的參數(shù)為存儲過程名和參數(shù)列表,返回值為受影響的行數(shù)
execute(self, query, args):執(zhí)行單條sql語句,接收的參數(shù)為sql語句本身和使用的參數(shù)列表,返回值為受影響的行數(shù)
executemany(self, query, args):執(zhí)行單挑sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)
nextset(self):移動到下一個結(jié)果集
cursor用來接收返回值的方法:
fetchall(self):接收全部的返回結(jié)果行.
fetchmany(self, size=None):接收size條返回結(jié)果行.如果size的值大于返回的結(jié)果行的數(shù)量,則會返回cursor.arraysize條數(shù)據(jù).
fetchone(self):返回一條結(jié)果行.
scroll(self, value, mode='relative'):移動指針到某一行.如果mode='relative',則表示從當(dāng)前所在行移動value條,如果 mode='absolute',則表示從結(jié)果集的第一行移動value條.
1、環(huán)境準(zhǔn)備:
Linux安裝mysql: apt-get install mysql-server
安裝python-mysql模塊:apt-get install python-mysqldb
下載安裝mysql
python操作mysql模塊:MySQL-python-1.2.3.win32-py2.7.exe 或 MySQL-python-1.2.3.win-amd64-py2.7.exe
下載地址:http://blog.csdn.net/reblue520/article/details/51702485
不安裝python-mysqldb模塊會報錯:
Unused import: MySQLdb,需要安裝pytho-mysqldb
安裝python開發(fā)環(huán)境,具體可參考:windows下搭建eclipse關(guān)于python的開發(fā)環(huán)境及初始化參數(shù)配置
2、一個簡單連接數(shù)據(jù)庫的例子:
import MySQLdbtry:conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)cur=conn.cursor()cur.execute('select user,password from user')cur.close()conn.close() except MySQLdb.Error,e:print "Mysql Error %d: %s" % (e.args[0], e.args[1])注意修改你連接的數(shù)據(jù)庫,主機IP,用戶名,密碼。
3、插入數(shù)據(jù),批量插入數(shù)據(jù),更新數(shù)據(jù)的例子:
#!/usr/bin/env python #_*_ coding:utf-8 _*_import MySQLdbtry:conn = MySQLdb.Connect(host='192.168.8.40',user='root',passwd='root',db='mysql',port=3306)cur = conn.cursor()rs = cur.execute('select user,password,host from user')rs = cur.execute('create database if not exists python')conn.select_db('python')cur.execute('create table test(id int,info varchar(30))') value = [1,'hi jack']cur.execute('insert into test values(%s,%s)',value)values = []for i in range(20):values.append((i,'hi jack' + str(i)))cur.executemany('insert into test values(%s,%s)',values)cur.execute('update test set info="i am jack" where id=3')conn.commit()cur.close()conn.close() except MySQLdb.Error,e:print 'mysql error msg: %d,%s' % (e.args[0],e.args[1])請注意一定要有conn.commit()這句來提交事務(wù),要不然不能真正的插入數(shù)據(jù)
4、常用獲取數(shù)據(jù)的一些方法
#!/usr/bin/env python #_*_ coding:utf-8 _*_import MySQLdbtry:conn = MySQLdb.Connect(host='192.168.8.40',user='root',passwd='root',db='mysql',port=3306,charset='utf8')cur = conn.cursor()conn.select_db('python')count = cur.execute('select * from test') print 'there has %s rows record' % countresult = cur.fetchone()print resultprint 'id: %s info %s' % resultresult2 = cur.fetchmany(3)for record in result2:print recordprint '=='*10cur.scroll(0,mode='absolute')result3 = cur.fetchall()for record in result3:print record[0] ,'---',record[1]conn.commit()cur.close()conn.close() except MySQLdb.Error,e:print 'mysql error msg: %d,%s' % (e.args[0],e.args[1])
查詢后中文會顯示亂碼,但在數(shù)據(jù)庫中卻是正常的,發(fā)現(xiàn)用一個屬性有可搞定:
在Python代碼?
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python') 中加一個屬性:
?改為:
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8')?
charset是要跟你數(shù)據(jù)庫的編碼一樣,如果是數(shù)據(jù)庫是gb2312 ,則寫charset='gb2312'。
常用的函數(shù):
然后,這個連接對象也提供了對事務(wù)操作的支持,標(biāo)準(zhǔn)的方法
commit() 提交
rollback() 回滾
cursor用來執(zhí)行命令的方法:
callproc(self, procname, args):用來執(zhí)行存儲過程,接收的參數(shù)為存儲過程名和參數(shù)列表,返回值為受影響的行數(shù)
execute(self, query, args):執(zhí)行單條sql語句,接收的參數(shù)為sql語句本身和使用的參數(shù)列表,返回值為受影響的行數(shù)
executemany(self, query, args):執(zhí)行單挑sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)
nextset(self):移動到下一個結(jié)果集
cursor用來接收返回值的方法:
fetchall(self):接收全部的返回結(jié)果行.
fetchmany(self, size=None):接收size條返回結(jié)果行.如果size的值大于返回的結(jié)果行的數(shù)量,則會返回cursor.arraysize條數(shù)據(jù).
fetchone(self):返回一條結(jié)果行.
scroll(self, value, mode='relative'):移動指針到某一行.如果mode='relative',則表示從當(dāng)前所在行移動value條,如果 mode='absolute',則表示從結(jié)果集的第一行移動value條.
轉(zhuǎn)載于:https://www.cnblogs.com/reblue520/p/6239778.html
總結(jié)
以上是生活随笔為你收集整理的python操作mysql数据库的常用方法使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 例题 3-5 生成元 d
- 下一篇: Java String 学习笔记 (一)