Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構

10.4. python-mysqldb

$ apt-cache search python | grep mysql
python-mysqldb - A Python interface to MySQL
python-mysqldb-dbg - A Python interface to MySQL (debug extension)

$ sudo apt-get install python-mysqldb		
		
# -*- coding: utf-8 -*-     
#mysqldb    
import time, MySQLdb    
   
#連接    
conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8")  
cursor = conn.cursor() 
   
#寫入    
sql = "insert into user(name,created) values(%s,%s)"   
param = ("neo",int(time.time()))    
n = cursor.execute(sql,param) 
print n    
   
#更新    
sql = "update user set name=%s where id=3"   
param = ("jam")    
n = cursor.execute(sql,param)    
print n
   
#查詢    
n = cursor.execute("select * from user")    
for row in cursor.fetchall():    
    for r in row:    
        print r    
   
#刪除    
sql = "delete from user where name=%s"   
param =("neo")    
n = cursor.execute(sql,param)    
print n    
cursor.close()    
   
#關閉    
conn.close()