最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python操作MySQL數(shù)據(jù)庫(kù)的方法分享

 更新時(shí)間:2012年05月29日 19:48:01   作者:  
堅(jiān)持每天學(xué)一點(diǎn),每天積累一點(diǎn)點(diǎn),作為自己每天的業(yè)余收獲,這個(gè)文章是我在吃飯的期間寫的,利用自己零散的時(shí)間學(xué)了一下python操作MYSQL,所以整理一下
我采用的是MySQLdb操作的MYSQL數(shù)據(jù)庫(kù)。先來一個(gè)簡(jiǎn)單的例子吧:
復(fù)制代碼 代碼如下:

import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)
cur=conn.cursor()
cur.execute('select * from user')
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

請(qǐng)注意修改你的數(shù)據(jù)庫(kù),主機(jī)名,用戶名,密碼。

下面來大致演示一下插入數(shù)據(jù),批量插入數(shù)據(jù),更新數(shù)據(jù)的例子吧:
復(fù)制代碼 代碼如下:

import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306)
cur=conn.cursor()

cur.execute('create database if not exists python')
conn.select_db('python')
cur.execute('create table test(id int,info varchar(20))')

value=[1,'hi rollen']
cur.execute('insert into test values(%s,%s)',value)

values=[]
for i in range(20):
values.append((i,'hi rollen'+str(i)))

cur.executemany('insert into test values(%s,%s)',values)

cur.execute('update test set info="I am rollen" where id=3')

conn.commit()
cur.close()
conn.close()

except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

請(qǐng)注意一定要有conn.commit()這句來提交事務(wù),要不然不能真正的插入數(shù)據(jù)。

運(yùn)行之后我的MySQL數(shù)據(jù)庫(kù)的結(jié)果就不上圖了。
復(fù)制代碼 代碼如下:

import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306)
cur=conn.cursor()

conn.select_db('python')

count=cur.execute('select * from test')
print 'there has %s rows record' % count

result=cur.fetchone()
print result
print 'ID: %s info %s' % result

results=cur.fetchmany(5)
for r in results:
print r

print '=='*10
cur.scroll(0,mode='absolute')

results=cur.fetchall()
for r in results:
print r[1]


conn.commit()
cur.close()
conn.close()

except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

運(yùn)行結(jié)果就不貼了,太長(zhǎng)了。

查詢后中文會(huì)正確顯示,但在數(shù)據(jù)庫(kù)中卻是亂碼的。經(jīng)過我從網(wǎng)上查找,發(fā)現(xiàn)用一個(gè)屬性有可搞定:

在Python代碼

conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python') 中加一個(gè)屬性:
改為:
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8')
charset是要跟你數(shù)據(jù)庫(kù)的編碼一樣,如果是數(shù)據(jù)庫(kù)是gb2312 ,則寫charset='gb2312'。



下面貼一下常用的函數(shù):

然后,這個(gè)連接對(duì)象也提供了對(duì)事務(wù)操作的支持,標(biāo)準(zhǔn)的方法
commit() 提交
rollback() 回滾

cursor用來執(zhí)行命令的方法:
callproc(self, procname, args):用來執(zhí)行存儲(chǔ)過程,接收的參數(shù)為存儲(chǔ)過程名和參數(shù)列表,返回值為受影響的行數(shù)
execute(self, query, args):執(zhí)行單條sql語(yǔ)句,接收的參數(shù)為sql語(yǔ)句本身和使用的參數(shù)列表,返回值為受影響的行數(shù)
executemany(self, query, args):執(zhí)行單挑sql語(yǔ)句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)
nextset(self):移動(dòng)到下一個(gè)結(jié)果集

cursor用來接收返回值的方法:
fetchall(self):接收全部的返回結(jié)果行.
fetchmany(self, size=None):接收size條返回結(jié)果行.如果size的值大于返回的結(jié)果行的數(shù)量,則會(huì)返回cursor.arraysize條數(shù)據(jù).
fetchone(self):返回一條結(jié)果行.
scroll(self, value, mode='relative'):移動(dòng)指針到某一行.如果mode='relative',則表示從當(dāng)前所在行移動(dòng)value條,如果 mode='absolute',則表示從結(jié)果集的第一行移動(dòng)value條.

參考資料:

MySQLdb‘s user guide

package MySQLdb

相關(guān)文章

最新評(píng)論

乐亭县| 临海市| 永城市| 镶黄旗| 砚山县| 宣汉县| 乌鲁木齐市| 宜良县| 体育| 永寿县| 东乌珠穆沁旗| 八宿县| 汝州市| 曲阜市| 芦山县| 新化县| 尚志市| 柯坪县| 邛崃市| 徐闻县| 尚义县| 柳州市| 巴林左旗| 青海省| 卫辉市| 娄烦县| 东莞市| 茶陵县| 南涧| 松滋市| 绥江县| 万全县| 虹口区| 宕昌县| 科尔| 绥滨县| 卫辉市| 孝义市| 黔西县| 金乡县| 尼木县|