国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python操作MySQL數據庫的方法分享

2019-11-25 18:42:38
字體:
來源:轉載
供稿:網友
我采用的是MySQLdb操作的MYSQL數據庫。先來一個簡單的例子吧:
復制代碼 代碼如下:

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])

請注意修改你的數據庫,主機名,用戶名,密碼。

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

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])

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

運行之后我的MySQL數據庫的結果就不上圖了。
復制代碼 代碼如下:

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])

運行結果就不貼了,太長了。

查詢后中文會正確顯示,但在數據庫中卻是亂碼的。經過我從網上查找,發現用一個屬性有可搞定:

在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是要跟你數據庫的編碼一樣,如果是數據庫是gb2312 ,則寫charset='gb2312'。



下面貼一下常用的函數:

然后,這個連接對象也提供了對事務操作的支持,標準的方法
commit() 提交
rollback() 回滾

cursor用來執行命令的方法:
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條.

參考資料:

MySQLdb‘s user guide

package MySQLdb

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连云港市| 崇州市| 偃师市| 日喀则市| 莒南县| 和田市| 武强县| 巫溪县| 上虞市| 武义县| 平江县| 渑池县| 永登县| 枣阳市| 呼图壁县| 德兴市| 云阳县| 墨玉县| 延长县| 嘉黎县| 伊吾县| 上林县| 织金县| 五峰| 通渭县| 永顺县| 读书| 永顺县| 晋城| 夹江县| 横峰县| 汉阴县| 梅州市| 阜城县| 法库县| 桦川县| 连城县| 沽源县| 乌兰县| 岐山县| 溆浦县|