Python操作MySQL

來源:互聯網
上載者:User

閑話少說,看代碼:
#!/usr/bin/env python
# -*-coding:UTF-8-*-#這一句告訴python用UTF-8編碼
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE  : 2004-12-28
#
# COMMENT: 這是一個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:sql-python/">http://sourceforge.net/projects/mysql-python/
下載解壓縮後放到%Python_HOME%/Lib/site-packages目錄中,python會自動找到此包。
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。 

http://blog.csdn.net/ChumpKlutz/archive/2006/10/03/1319997.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.