python-mysql


1.MySQLdb的安裝,下了一個安裝包(MySQL-python-1.2.3.win-amd64-py2.7),直接按提示就好,然后cmd進入python,敲python setup.py install,結束。可以檢查一下,import MySQLdb~

但是之前改過python的path,不知道有沒有用,若失敗,請參考解決下。

2.數據庫連接

MySQLdb提供了connect方法用來和數據庫建立連接,接收數個參數,返回連接對象:

復制代碼 代碼如下:
conn=MySQLdb.connect(host="localhost",user="root",passwd="jb51",db="test",charset="utf8")

比較常用的參數包括:
host:數據庫主機名.默認是用本地主機
user:數據庫登陸名.默認是當前用戶
passwd:數據庫登陸的秘密.默認為空
db:要使用的數據庫名.沒有默認值
port:MySQL服務使用的TCP端口.默認是3306
charset:數據庫編碼
更多關於參數的信息可以查這里 http://mysql-python.sourceforge.net/MySQLdb.html

然后,這個連接對象也提供了對事務操作的支持,標准的方法:
commit() 提交
rollback() 回滾

3.Connect() 方法用於創建數據庫的連接,里面可以指定參數:用戶名,密碼,主機等信息。

這只是連接到了數據庫,要想操作數據庫需要創建游標。

 

>>> cur = conn.cursor()

通過獲取到的數據庫連接conn下的cursor()方法來創建游標。

 

 

五,python 操作mysql數據庫基礎

復制代碼
#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#創建數據表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一條數據
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")


#修改查詢條件的數據
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#刪除查詢條件的數據
#cur.execute("delete from student where age='9'")

cur.close()
conn.commit()
conn.close()
復制代碼

>>> conn = MySQLdb.connect(host='localhost',port = 3306,user='root', passwd='123456',db ='test',)

Connect() 方法用於創建數據庫的連接,里面可以指定參數:用戶名,密碼,主機等信息。

這只是連接到了數據庫,要想操作數據庫需要創建游標。

 

>>> cur = conn.cursor()

通過獲取到的數據庫連接conn下的cursor()方法來創建游標。

 

>>> cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

通過游標cur 操作execute()方法可以寫入純sql語句。通過execute()方法中寫如sql語句來對數據進行操作。

 

>>>cur.close()

cur.close() 關閉游標

>>>conn.commit()

conn.commit()方法在提交事物,在向數據庫插入一條數據時必須要有這個方法,否則數據不會被真正的插入。

>>>conn.close()

Conn.close()關閉數據庫連接

 

 

六,插入數據

 

通過上面execute()方法中寫入純的sql語句來插入數據並不方便。如:

>>>cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

我要想插入新的數據,必須要對這條語句中的值做修改。我們可以做如下修改:

復制代碼
#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#插入一條數據
sqli="insert into student values(%s,%s,%s,%s)"
cur.execute(sqli,('3','Huhu','2 year 1 class','7'))

cur.close()
conn.commit()
conn.close()
復制代碼

 

假如要一次向數據表中插入多條值呢?

復制代碼
#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#一次插入多條記錄
sqli="insert into student values(%s,%s,%s,%s)"
cur.executemany(sqli,[
    ('3','Tom','1 year 1 class','6'),
    ('3','Jack','2 year 1 class','7'),
    ('3','Yaheng','2 year 2 class','7'),
    ])

cur.close()
conn.commit()
conn.close()
復制代碼

executemany()方法可以一次插入多條值,執行單挑sql語句,但是重復執行參數列表里的參數,返回值為受影響的行數。

 

 

七,查詢數據

 

也許你已經嘗試了在python中通過

>>>cur.execute("select * from student")

來查詢數據表中的數據,但它並沒有把表中的數據打印出來,有些失望。

來看看這條語句獲得的是什么

>>>aa=cur.execute("select * from student")

>>>print aa

5

它獲得的只是我們的表中有多少條數據。那怎樣才能獲得表中的數據呢?進入python shell

 

復制代碼
>>> import MySQLdb
>>> conn = MySQLdb.connect(host='localhost',port = 3306,user='root',    passwd='123456',db ='test',)
>>> cur = conn.cursor()
>>> cur.execute("select * from student")
5L
>>> cur.fetchone()
(1L, 'Alen', '1 year 2 class', '6')
>>> cur.fetchone()
(3L, 'Huhu', '2 year 1 class', '7')
>>> cur.fetchone()
(3L, 'Tom', '1 year 1 class', '6')
...
>>>cur.scroll(0,'absolute') 
復制代碼

 

  fetchone()方法可以幫助我們獲得表中的數據,可是每次執行cur.fetchone() 獲得的數據都不一樣,換句話說我沒執行一次,游標會從表中的第一條數據移動到下一條數據的位置,所以,我再次執行的時候得到的是第二條數據。

  scroll(0,'absolute') 方法可以將游標定位到表中的第一條數據。

 

還是沒解決我們想要的結果,如何獲得表中的多條數據並打印出來呢?

復制代碼
#coding=utf-8
import MySQLdb

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
cur = conn.cursor()

#獲得表中有多少條數據
aa=cur.execute("select * from student")
print aa

#打印表中的多少數據
info = cur.fetchmany(aa)
for ii in info:
    print ii
cur.close()
conn.commit()
conn.close()
復制代碼

  通過之前的print aa 我們知道當前的表中有5條數據,fetchmany()方法可以獲得多條數據,但需要指定數據的條數,通過一個for循環就可以把多條數據打印出啦!執行結果如下:

 

復制代碼
5
(1L, 'Alen', '1 year 2 class', '6')
(3L, 'Huhu', '2 year 1 class', '7')
(3L, 'Tom', '1 year 1 class', '6')
(3L, 'Jack', '2 year 1 class', '7')
(3L, 'Yaheng', '2 year 2 class', '7')
[Finished in 0.1s]
復制代碼【轉】http://www.cnblogs.com/fnng/p/3565912.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM