python數(shù)據(jù)庫(kù)操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
實(shí)例1、取得MYSQL版本
# -*- coding: UTF-8 -*-
#安裝MYSQL DB for python
import MySQLdb as mdb
con = None
try:
#連接mysql的方法:connect('ip','user','password','dbname')
con = mdb.connect('localhost', 'root',
'root', 'test');
#所有的查詢,都在連接con的一個(gè)模塊cursor上面運(yùn)行的
cur = con.cursor()
#執(zhí)行一個(gè)查詢
cur.execute("SELECT VERSION()")
#取得上個(gè)查詢的結(jié)果,是單個(gè)結(jié)果
data = cur.fetchone()
print "Database version : %s " % data
finally:
if con:
#無(wú)論如何,連接記得關(guān)閉
con.close()
執(zhí)行結(jié)果:
Database version : 5.5.25
實(shí)例2、創(chuàng)建一個(gè)表并且插入數(shù)據(jù)
# -*- coding: UTF-8 -*-
import MySQLdb as mdb
import sys
#將con設(shè)定為全局連接
con = mdb.connect('localhost', 'root', 'root', 'test');
with con:
#獲取連接的cursor,只有獲取了cursor,我們才能進(jìn)行各種操作
cur = con.cursor()
#創(chuàng)建一個(gè)數(shù)據(jù)表 writers(id,name)
cur.execute("CREATE TABLE IF NOT EXISTS \
Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))")
#以下插入了5條數(shù)據(jù)
cur.execute("INSERT INTO Writers(Name) VALUES('Jack London')")
cur.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')")
cur.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')")
cur.execute("INSERT INTO Writers(Name) VALUES('Emile Zola')")
cur.execute("INSERT INTO Writers(Name) VALUES('Truman Capote')")
實(shí)例3、python使用slect獲取mysql的數(shù)據(jù)并遍歷
# -*- coding: UTF-8 -*-
import MySQLdb as mdb
import sys
#連接mysql,獲取連接的對(duì)象
con = mdb.connect('localhost', 'root', 'root', 'test');
with con:
#仍然是,第一步要獲取連接的cursor對(duì)象,用于執(zhí)行查詢
cur = con.cursor()
#類似于其他語(yǔ)言的query函數(shù),execute是python中的執(zhí)行查詢函數(shù)
cur.execute("SELECT * FROM Writers")
#使用fetchall函數(shù),將結(jié)果集(多維元組)存入rows里面
rows = cur.fetchall()
#依次遍歷結(jié)果集,發(fā)現(xiàn)每個(gè)元素,就是表中的一條記錄,用一個(gè)元組來(lái)顯示
for row in rows:
print row
執(zhí)行結(jié)果:
(1L, ‘Jack London')
(2L, ‘Honore de Balzac')
(3L, ‘Lion Feuchtwanger')
(4L, ‘Emile Zola')
(5L, ‘Truman Capote')
實(shí)例4、使用字典cursor取得結(jié)果集(可以使用表字段名字訪問(wèn)值)
# -*- coding: UTF-8 -*-
# 來(lái)源:瘋狂的螞蟻的博客www.server110.com總結(jié)整理
import MySQLdb as mdb
import sys
#獲得mysql查詢的鏈接對(duì)象
con = mdb.connect('localhost', 'root', 'root', 'test')
with con:
#獲取連接上的字典cursor,注意獲取的方法,
#每一個(gè)cursor其實(shí)都是cursor的子類
cur = con.cursor(mdb.cursors.DictCursor)
#執(zhí)行語(yǔ)句不變
cur.execute("SELECT * FROM Writers")
#獲取數(shù)據(jù)方法不變
rows = cur.fetchall()
#遍歷數(shù)據(jù)也不變(比上一個(gè)更直接一點(diǎn))
for row in rows:
#這里,可以使用鍵值對(duì)的方法,由鍵名字來(lái)獲取數(shù)據(jù)
print "%s %s" % (row["Id"], row["Name"])
實(shí)例5、獲取單個(gè)表的字段名和信息的方法
# -*- coding: UTF-8 -*-
# 來(lái)源:瘋狂的螞蟻的博客www.server110.com總結(jié)整理
import MySQLdb as mdb
import sys
#獲取數(shù)據(jù)庫(kù)的鏈接對(duì)象
con = mdb.connect('localhost', 'root', 'root', 'test')
with con:
#獲取普通的查詢cursor
cur = con.cursor()
cur.execute("SELECT * FROM Writers")
rows = cur.fetchall()
#獲取連接對(duì)象的描述信息
desc = cur.description
print 'cur.description:',desc
#打印表頭,就是字段名字
print "%s %3s" % (desc[0][0], desc[1][0])
for row in rows:
#打印結(jié)果
print "%2s %3s" % row
運(yùn)行結(jié)果: cur.description: ((‘Id', 3, 1, 11, 11, 0, 0), (‘Name', 253, 17, 25, 25, 0, 1))
Id Name
1 Jack London
2 Honore de Balzac
3 Lion Feuchtwanger
4 Emile Zola
5 Truman Capote
實(shí)例6、使用Prepared statements執(zhí)行查詢(更安全方便)
# -*- coding: UTF-8 -*-
# 來(lái)源:瘋狂的螞蟻的博客www.server110.com總結(jié)整理
import MySQLdb as mdb
import sys
con = mdb.connect('localhost', 'root', 'root', 'test')
with con:
cur = con.cursor()
#我們看到,這里可以通過(guò)寫一個(gè)可以組裝的sql語(yǔ)句來(lái)進(jìn)行
cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s",
("Guy de Maupasant", "4"))
#使用cur.rowcount獲取影響了多少行
print "Number of rows updated: %d" % cur.rowcount
結(jié)果:
Number of rows updated: 1
相關(guān)文章
python 實(shí)現(xiàn)保存最新的三份文件,其余的都刪掉
今天小編就為大家分享一篇python 實(shí)現(xiàn)保存最新的三份文件,其余的都刪掉,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12python爬蟲(chóng)之教你如何爬取地理數(shù)據(jù)
這篇文章主要介紹了python爬蟲(chóng)之教你如何爬取地理數(shù)據(jù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解
這篇文章主要介紹了python解釋器pycharm安裝及環(huán)境變量配置教程圖文詳解,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python中通過(guò)property設(shè)置類屬性的訪問(wèn)
為了達(dá)到類似C++類的封裝性能,可以使用property來(lái)設(shè)置Python類屬性的訪問(wèn)權(quán)限,本文就介紹一下Python中通過(guò)property設(shè)置類屬性的訪問(wèn),感興趣的可以了解一下,感興趣的可以了解一下2023-09-09Python實(shí)現(xiàn)Youku視頻批量下載功能
前段時(shí)間由于收集視頻數(shù)據(jù)的需要,自己搗鼓了一個(gè)YouKu視頻批量下載的程序。下面小編把實(shí)現(xiàn)過(guò)程分享到腳本之家平臺(tái),供大家參考2017-03-03Python3.7在anaconda里面使用IDLE編譯器的步驟詳解
這篇文章主要介紹了Python3.7在anaconda里面使用IDLE編譯器的步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-04-04淺談哪個(gè)Python庫(kù)才最適合做數(shù)據(jù)可視化
數(shù)據(jù)可視化是任何探索性數(shù)據(jù)分析或報(bào)告的關(guān)鍵步驟,目前有許多非常好的商業(yè)智能工具,比如Tableau、googledatastudio和PowerBI等,本文就詳細(xì)的進(jìn)行對(duì)比,感興趣的可以了解一下2021-06-06循環(huán)神經(jīng)網(wǎng)絡(luò)TextRNN實(shí)現(xiàn)情感短文本分類任務(wù)
這篇文章主要為大家介紹了循環(huán)神經(jīng)網(wǎng)絡(luò)TextRNN實(shí)現(xiàn)情感短文本分類任務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04