最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python連接mysql實例分享

 更新時間:2016年10月09日 08:45:26   投稿:hebedich  
本文給大家匯總介紹了使用python連接mysql的幾個實例,非常的簡單實用,有需要的小伙伴可以參考下

示例一

#coding=UTF-8

import sys
import MySQLdb
import time

reload(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 MySQLdb
conn = 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 x
cursor.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 
>>> 

  這里導(dǎo)入MySQLdb沒有報錯,就說明安裝成功.

2 下面就可以連接數(shù)據(jù)庫,可以進行增刪改操作.

root@10.1.1.45:python# cat create.py  
#!/usr/bin/env python 
#coding=utf-8 
 
#導(dǎo)入相關(guān)模塊 
import MySQLdb 
 
#建立和mysql數(shù)據(jù)庫的連接 
conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe') 
#獲取游標(biāo) 
curs = conn.cursor() 
#執(zhí)行SQL,創(chuàng)建一個數(shù)據(jù)庫 
curs.execute("create database pythondb") 
#選擇連接哪個數(shù)據(jù)庫 
conn.select_db('pythondb') 
#執(zhí)行SQL,創(chuàng)建一個表 
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() 
#關(guān)閉游標(biāo)連接,釋放資源 
curs.close() 
#關(guān)閉連接 
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 
 
#導(dǎo)入相關(guān)模塊 
import MySQLdb 
 
#建立和mysql數(shù)據(jù)庫的連接 
conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226') 
#獲取游標(biāo) 
curs = conn.cursor() 
#選擇連接哪個數(shù)據(jù)庫 
conn.select_db('pythondb') 
#查看共有多少條記錄 
count = curs.execute('select * from test') 
print "一共有%s條記錄" % count 
#獲取一條記錄,以一個元組返回 
result = curs.fetchone() 
print "當(dāng)前的一條記錄 ID:%s message:%s" % result 
#獲取后10條記錄,由于之前執(zhí)行了getchone(),所以游標(biāo)已經(jīng)指到第二條記錄,下面也就從第二條記錄開始返回 
results = curs.fetchmany(10) 
for r in results: 
  print r 
#重置游標(biāo)位置,0,為偏移量,mode = relative(默認(rèn)) 
curs.scroll(0,mode='absolute') 
#獲取所有記錄 
results = curs.fetchall() 
for r in results: 
  print r 
 
#提交修改 
conn.commit() 
#關(guān)閉游標(biāo)連接,釋放資源 
curs.close() 
#關(guān)閉連接 
conn.close() 

root@10.1.1.45:python# ./select.py  
一共有21條記錄 
當(dāng)前的一條記錄 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') 

相關(guān)文章

最新評論

永兴县| 萨嘎县| 襄垣县| 保德县| 防城港市| 南漳县| 突泉县| 萨嘎县| 开原市| 阜宁县| 辉南县| 济南市| 余干县| 准格尔旗| 鹤壁市| 洪泽县| 深水埗区| 林周县| 通山县| 清水河县| 邢台县| 雷山县| 安溪县| 湟源县| 蒙城县| 清水县| 山丹县| 逊克县| 康定县| 嫩江县| 新丰县| 玉田县| 三原县| 公主岭市| 通江县| 武乡县| 许昌县| 新宁县| 库尔勒市| 昭通市| 延川县|