標籤:python
一,MySQL-Python外掛程式
Python裡操作MySQL資料庫,需要Python下安裝訪問MySQL資料庫介面API包即外掛程式,從而使得Python2.7能訪問操作MySQL資料庫。MySQL軟體可以去官網下載:http://www.mysql.com/
二,訪問MySQL資料庫
1,串連資料庫mysql
基本格式:connect ([host=]‘ip‘,[user=]‘user‘,[passwd=]‘password‘,[db=]‘dbname‘)
2,資料庫的基本操作
1)create建立表
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#create a tablecursor.execute(‘create table test(ID int primary key auto_increment,Name char(25))‘)#Closing databasecursor.close()conn.close()
2)fetchall訪問:
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#fetch datasn=cursor.execute(‘select * from test;‘)r=cursor.fetchall()print n,r#Closing databasecursor.close()conn.close()>>> ================================ RESTART ================================>>> 3 ((4L, ‘zhangbc‘), (5L, ‘lis08‘), (6L, ‘wangw‘))>>>
在Mysql5.6環境下運行:
3)insert向表中插入資料:
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#insert data into table ‘test‘mysql=‘‘‘insert into test(id,sname) values(4,‘zhanghua‘)‘‘‘cursor.execute(mysql)conn.commit()#below mysql5.0 needed#fetch datasn=cursor.execute(‘select * from test;‘)r=cursor.fetchall()print n,r#Closing databasecursor.close()conn.close()>>> ================================ RESTART ================================>>>4 ((1L, ‘zhangbc‘), (2L, ‘lis‘), (3L, ‘wangw‘), (4L, ‘zhanghua‘))
注意:一定要寫上conn.commit();事物不提交,將復原。比較:
?
4)update修改表中資料:
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#update data of the table ‘test‘mysql=‘‘‘update test set sname=‘Lisi08‘ where id=2‘‘‘cursor.execute(mysql)conn.commit()#below mysql5.0 needed#fetch datasn=cursor.execute(‘select * from test;‘)r=cursor.fetchall()print n,r#Closing databasecursor.close()conn.close()>>> ================================ RESTART ================================>>>4 ((1L, ‘zhangbc‘), (2L, ‘Lisi08‘), (3L, ‘wangw‘), (4L, ‘zhanghua‘))
5)delete刪除表中資料:
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#delete data of the table ‘test‘mysql=‘‘‘delete from test where id=4‘‘‘cursor.execute(mysql)conn.commit()#below mysql5.0 needed#fetch datasn=cursor.execute(‘select * from test;‘)r=cursor.fetchall()print n,r#Closing databasecursor.close()conn.close()>>> ================================ RESTART ================================>>>3 ((1L, ‘zhangbc‘), (2L, ‘Lisi08‘), (3L, ‘wangw‘))
6)關於select及其遍曆:
i)使用元組tuple與fetchone結合
import MySQLdb#connect to a database ‘test‘conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)cursor=conn.cursor()#fetch datascursor.execute(‘select * from test;‘)#獲得結果集的記錄numrows=int(cursor.rowcount)#迴圈,取行資料for i in range(numrows): row=cursor.fetchone() print row[0],row[1]#Closing databasecursor.close()conn.close()>>> ================================ RESTART ================================>>> 4 zhangbc5 lis086 wangw
ii)使用字典cursor
#-*- coding:UTF-8 -*-import MySQLdb as mdb#connect to a database ‘test‘conn=mdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)with conn: #擷取串連上的字典cursor,每一個cursor其實都是cursor的子類 cur=conn.cursor(mdb.cursors.DictCursor)#fetch datascur.execute(‘select * from test;‘)#獲得結果集rows=cur.fetchall()#迴圈,取行資料for row in rows: print ‘%s %s‘%(row[‘ID‘],row[‘Name‘])#Closing databasecur.close()conn.close()>>> ================================ RESTART ================================>>> 4 zhangbc5 lis086 wangw
iii)擷取單個表的欄位名及其資訊
#-*- coding:UTF-8 -*-import MySQLdb as mdb#connect to a database ‘test‘conn=mdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘zbc123‘,db=‘test‘)with conn: #擷取串連上的字典cursor,每一個cursor其實都是cursor的子類 cur=conn.cursor()#fetch datascur.execute(‘select * from test;‘)#獲得結果集rows=cur.fetchall()#獲得連結化物件的描述資訊desc=cur.descriptionprint ‘cur.description:‘,desc#列印表頭print ‘%2s %3s‘%(desc[0][0],desc[1][0])#迴圈,取行資料for row in rows: print ‘%2s %3s‘%row#Closing databasecur.close()conn.close()>>> ================================ RESTART ================================>>> cur.description: ((‘ID‘, 3, 1, 11, 11, 0, 0), (‘Name‘, 254, 7, 25, 25, 0, 1))ID Name 4 zhangbc 5 lis08 6 wangw
三,小結
本文主要介紹了Python下如何訪問並操資料庫的基本知識,如:如何串連資料庫,如何執行執行SQL語句等等。
Python學習系列(七)( 資料庫編程)