日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

[Python]Python連接MySQL的實(shí)例代碼

 思念是一種餅 2011-04-11
!/usr/bin/env python    
 
-*-coding:UTF-8-*-   
這一句告訴python用UTF-8編碼    
 
=========================================================================    
    
 
NAME: Python MySQL test    
 
 
AUTHOR: benyur    
 
DATE : 2004-12-28    
 
 
COMMENT: 這是一個(gè)python連接mysql的例子    
 
=========================================================================    
    
"""   
***** This is a MySQL test *****   
select:   
conn=Connection()   
conn.select_db('test')   
cur=conn.cursor()   
cur.execute('select * from user')   
cur.scroll(0)   
row1=cur.fetchone()   
row1[0]   
row1[1]   
row1[2]   
insert:   
cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')')   
cur.insert_id()   
update:   
cur.execute('update user set passwd=\'123456\' where name=\'benyur\'')   
delete:   
cur.execute('delete from user where id=2')   
**********************************   
"""    
    
from MySQLdb import *    
    
    
def conn():    
conn=Connection()    
conn.select_db('test')    
cur=conn.cursor()    
cur.execute('select * from user')    
cur.scroll(0)    
row1=cur.fetchone()    
row1[0]    
row1[1]    
row1[2]    
    
    
def usage():    
print __doc__    
    
    
if __name__=='__main__':    
usage()  
!/usr/bin/env python 
-*-coding:UTF-8-*-
這一句告訴python用UTF-8編碼 
========================================================================= 
 
NAME: Python MySQL test 

AUTHOR: benyur 
DATE : 2004-12-28 

COMMENT: 這是一個(gè)python連接mysql的例子 
========================================================================= 
 
""" 
***** This is a MySQL test ***** 
select: 
conn=Connection() 
conn.select_db('test') 
cur=conn.cursor() 
cur.execute('select * from user') 
cur.scroll(0) 
row1=cur.fetchone() 
row1[0] 
row1[1] 
row1[2] 
insert: 
cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')') 
cur.insert_id() 
update: 
cur.execute('update user set passwd=\'123456\' where name=\'benyur\'') 
delete: 
cur.execute('delete from user where id=2') 
********************************** 
""" 
 
from MySQLdb import * 
 
 
def conn(): 
conn=Connection() 
conn.select_db('test') 
cur=conn.cursor() 
cur.execute('select * from user') 
cur.scroll(0) 
row1=cur.fetchone() 
row1[0] 
row1[1] 
row1[2] 
 
 
def usage(): 
print __doc__ 
 
 
if __name__=='__main__': 
usage()
MySQLdb下載地址:http:///projects/mysql-python/
下載解壓縮后放到%Python_HOME%\Lib\site-packages目錄中,python會自動找到此包。
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。
其他:
1. 平臺及版本
linux 內(nèi)核2.6,gcc 3.4.4,glibc 2.4
python 2.4.3
mysql 5.0.19
mysql-python 1.2.1-p2

2. 安裝mysql-python
tar xvfz MySQL-python-1.2.1_p2.tar.gz
cd MySQL-python-1.2.1_p2
python setup.py build
python setup.py install
3. 使用
import MySQLdb

3.1. 連接
conn = MySQLdb.Connection(host, user, password, dbname)

3.2. 選擇數(shù)據(jù)庫
conn.select_db(’database name’)

3.3. 獲得cursor
cur = conn.cursor()

3.4. cursor位置設(shè)定
cur.scroll(int, mode)
mode可為相對位置或者絕對位置,分別為relative和absolute。

3.5. select
cur.execute(‘select clause’)
例如
cur.execute(‘select * from mytable’)

row = cur.fetchall()
或者:
row1 = cur.fetchone()

3.6. insert
cur.execute(‘inset clause’)
例如
cur.execute(‘insert into table (row1, row2) values (\’111\’, \’222\’)’)
conn.commit()
3.7. update
cur.execute(‘update clause’)
例如
cur.execute(“update table set row1 = ‘’ where row2 = ‘row2 ‘ ”)
conn.commit()

3.8. delete
cur.execute(‘delete clause’)
例如
cur.execute(“delete from table where row1 = ‘row1’ ”)

conn.commit()
 
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/wenhai_zhang/archive/2009/12/14/5006526.aspx

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多