python串連mysql執行個體分享_python

來源:互聯網
上載者:User

樣本一

#coding=UTF-8import sysimport MySQLdbimport timereload(sys)sys.setdefaultencoding('utf-8')def connectDemo():  return MySQLdb.Connection("127.0.0.1","root","root","demo",3306,charset="utf8")if __name__ == '__main__':  begin=time.time()  conn=connectDemo()  cursor = conn.cursor()  sql="""    show tables    """  count = cursor.execute(sql)  rows = cursor.fetchall()  cursor.close()  conn.close()  print "========demo庫共:%s 張表============" % (count)  print '耗時:%s 秒' % (time.time()-begin)

樣本二

import MySQLdbconn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test")cursor = conn.cursor()cursor.execute("select * from hard")res = cursor.fetchall()for x in res:print xcursor.close()conn.close()

樣本三

1 安裝Python的Mysql包

root@10.1.1.45:~# apt-get install python-mysqldb root@10.1.1.45:~# python Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32)  [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> 

  這裡匯入MySQLdb沒有報錯,就說明安裝成功.

2 下面就可以串連資料庫,可以進行增刪改操作.

root@10.1.1.45:python# cat create.py  #!/usr/bin/env python #coding=utf-8  #匯入相關模組 import MySQLdb  #建立和mysql資料庫的串連 conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe') #擷取遊標 curs = conn.cursor() #執行SQL,建立一個資料庫 curs.execute("create database pythondb") #選擇串連哪個資料庫 conn.select_db('pythondb') #執行SQL,建立一個表 curs.execute("create table test(id int,message varchar(50))") #插入一條記錄 value = [1,"davehe"] curs.execute("insert into test values(%s,%s)",value) #插入多條記錄 values = [] for i in range(20):   values.append((i,'hello mysqldb' + str(i))) curs.executemany("insert into test values(%s,%s)",values) #提交修改                 conn.commit() #關閉遊標串連,釋放資源 curs.close() #關閉串連 conn.close() root@10.1.1.45:python# ./create.py 

3 下面利用python查看mysql裡剛添加的記錄.

root@10.1.1.45:python# cat select.py  #!/usr/bin/env python #coding=utf-8  #匯入相關模組 import MySQLdb  #建立和mysql資料庫的串連 conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226') #擷取遊標 curs = conn.cursor() #選擇串連哪個資料庫 conn.select_db('pythondb') #查看共有多少條記錄 count = curs.execute('select * from test') print "一共有%s條記錄" % count #擷取一條記錄,以一個元組返回 result = curs.fetchone() print "當前的一條記錄 ID:%s message:%s" % result #擷取後10條記錄,由於之前執行了getchone(),所以遊標已經指到第二條記錄,下面也就從第二條記錄開始返回 results = curs.fetchmany(10) for r in results:   print r #重設遊標位置,0,為位移量,mode = relative(預設) curs.scroll(0,mode='absolute') #擷取所有記錄 results = curs.fetchall() for r in results:   print r  #提交修改 conn.commit() #關閉遊標串連,釋放資源 curs.close() #關閉串連 conn.close() 
root@10.1.1.45:python# ./select.py  一共有21條記錄 當前的一條記錄 ID:1 message:davehe (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (1L, 'davehe') (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (10L, 'hello mysqldb10') (11L, 'hello mysqldb11') (12L, 'hello mysqldb12') (13L, 'hello mysqldb13') (14L, 'hello mysqldb14') (15L, 'hello mysqldb15') (16L, 'hello mysqldb16') (17L, 'hello mysqldb17') (18L, 'hello mysqldb18') (19L, 'hello mysqldb19') 

聯繫我們

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