Python 鏈接mysql數據庫

支持SQL標準的可用數據庫有不少,其中多數在Python中都有對應的客戶端模塊. java

這裏我使用的mysql,它須要安裝MySQLdb包.它至關於Python的數據接口規範Python DB API. python

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沒有報錯,就說明安裝成功.

下面就能夠鏈接數據庫,能夠進行增刪改操做. mysql

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
下面利用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')

附: linux

connect函數的經常使用參數 sql

user         #用戶名 數據庫

password   #用戶密碼 app

host         #主機名 函數

database   #數據庫名 fetch


connect函數會返回鏈接對象,鏈接對象方法 code

close()      #關閉鏈接以後,鏈接對象和它的遊標均不可用

commit()   #若是支持的話就提交掛起的事務,不然不作任何事

rollback()   #回滾掛起的事務(可能不可用)

cursor()     #返回鏈接的遊標對象


遊標對象方法

close()                          #關閉遊標

execute(oper[,params])    #執行SQL操做,可能使用參數

executemany(oper,pseq)   #對序列中的每一個參數執行SQL操做

fetchone()                     #把查詢的結果集中的下一行保存爲序列,或者None

fetchmany([size])            #獲取查詢結果集中的多行,默認爲1

fetchall()                       #將全部(剩餘)的行做爲序列的序列

相關文章
相關標籤/搜索