python操作MySQL數(shù)據(jù)庫具體方法
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])
請注意修改你的數(shù)據(jù)庫,主機(jī)名,用戶名,密碼。
下面來大致演示一下插入數(shù)據(jù),批量插入數(shù)據(jù),更新數(shù)據(jù)的例子吧:
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()這句來提交事務(wù),要不然不能真正的插入數(shù)據(jù)。
運(yùn)行之后我的MySQL數(shù)據(jù)庫的結(jié)果就不上圖了。
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é)果就不貼了,太長了。
查詢后中文會(huì)正確顯示,但在數(shù)據(jù)庫中卻是亂碼的。經(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ù)庫的編碼一樣,如果是數(shù)據(jù)庫是gb2312 ,則寫charset='gb2312'。
下面貼一下常用的函數(shù):
然后,這個(gè)連接對象也提供了對事務(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語句,接收的參數(shù)為sql語句本身和使用的參數(shù)列表,返回值為受影響的行數(shù)
executemany(self, query, args):執(zhí)行單挑sql語句,但是重復(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條.
- python Django連接MySQL數(shù)據(jù)庫做增刪改查
- Python操作MySQL數(shù)據(jù)庫9個(gè)實(shí)用實(shí)例
- python連接mysql數(shù)據(jù)庫示例(做增刪改操作)
- Python連接mysql數(shù)據(jù)庫的正確姿勢
- python備份文件以及mysql數(shù)據(jù)庫的腳本代碼
- Python Mysql數(shù)據(jù)庫操作 Perl操作Mysql數(shù)據(jù)庫
- python3使用PyMysql連接mysql數(shù)據(jù)庫實(shí)例
- linux下python3連接mysql數(shù)據(jù)庫問題
- win7上python2.7連接mysql數(shù)據(jù)庫的方法
- Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例
- Python操作mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪查改功能的方法
相關(guān)文章
python數(shù)據(jù)可視化matplotlib繪制折線圖示例
這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例
在python編程中,我們經(jīng)常要用到j(luò)son對象作為數(shù)據(jù)交換格式,下面這篇文章主要給大家介紹了關(guān)于Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08使用python?matplotlib?contour畫等高線圖的詳細(xì)過程講解
最近學(xué)習(xí)了matplotlib中的高線圖的繪制,所以下面這篇文章主要給大家介紹了關(guān)于使用python?matplotlib?contour畫等高線圖的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python數(shù)據(jù)結(jié)構(gòu)之鏈表詳解
在順序存儲(chǔ)方式中,根據(jù)數(shù)據(jù)元素的序號就可隨機(jī)存取表中任何一個(gè)元素,但同時(shí)在插入和刪除運(yùn)算需要移動(dòng)大量的元素,造成算法效率較低。解決此缺陷的一個(gè)辦法是:對線性表采用鏈?zhǔn)酱鎯?chǔ)方式。本文將介紹鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)的特點(diǎn)以及各種基本操作的實(shí)現(xiàn)。需要的可以參考一下2022-01-01Python基于回溯法子集樹模板實(shí)現(xiàn)圖的遍歷功能示例
這篇文章主要介紹了Python基于回溯法子集樹模板實(shí)現(xiàn)圖的遍歷功能,結(jié)合實(shí)例形式分析了Python使用回溯法子集樹模板針對圖形遍歷問題的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-09-09