Python串連MySQL的執行個體代碼

來源:互聯網
上載者:User

標籤:3.1   pytho   2.0   htm   url   cursor   glib   核心   解壓縮   

 

MySQLdb:http://sourceforge.net/projects/mysql-python/ 
下載解壓縮後放到%Python_HOME%/Lib/site-packages目錄中,python會自動找到此包。 
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。 

其他: 

1. 平台及版本 
linux 核心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. 選擇資料庫 
conn.select_db(’database name’)


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


3.4. cursor位置設定 
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() 

 

http://www.python123.cn/PythonInternet/20090609_11.html 

Python串連MySQL的執行個體代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.