win64位安裝python-mysqldb1.2.5
ubuntu下安裝MySQLdb
sudo apt-get install python-MySQLdb
導(dǎo)入MySQLdb庫
import MySQLdb
創(chuàng)建數(shù)據(jù)庫連接
conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test",charset="utf8")
connect對象屬性
commit()
:如果數(shù)據(jù)庫表進(jìn)行了修改,提交保存當(dāng)前的數(shù)據(jù)。當(dāng)然,如果此用戶沒有權(quán)限就作罷了,什么也不會發(fā)生。
rollback()
:如果有權(quán)限,就取消當(dāng)前的操作,否則報(bào)錯(cuò)。
cursor([cursorclass])
:游標(biāo)指針。
創(chuàng)建游標(biāo)(指針)cursor
cur = conn.cursor()
cursor執(zhí)行命令的方法:
execute(query, args)
:執(zhí)行單條sql語句。query為sql語句本身,args為參數(shù)值的列表。執(zhí)行后返回值為受影響的行數(shù)。
executemany(query, args)
:執(zhí)行單條sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回值為受影響的行數(shù)
在數(shù)據(jù)表中插入一條記錄
cur.execute("insert into users (username,password,email) values (%s,%s,%s)",("python","123456","python@gmail.com"))
在數(shù)據(jù)表中插入多條記錄
cur.executemany("insert into users (username,password,email) values (%s,%s,%s)",(("google","111222","g@gmail.com"),("facebook","222333","f@face.book"),("github","333444","git@hub.com"),("docker","444555","doc@ker.com")))
提交數(shù)據(jù)庫操作
conn.commit()
查詢數(shù)據(jù)
cur.execute("select * from users")
fetchall(self)
:接收全部的返回結(jié)果行.
fetchmany(size=None)
:接收size條返回結(jié)果行.如果size的值大于返回的結(jié)果行的數(shù)量,則會返回cursor.arraysize條數(shù)據(jù).
fetchone()
:返回一條結(jié)果行.
scroll(value, mode='relative')
:移動指針到某一行.如果mode='relative',則表示從當(dāng)前所在行移動value條,如果mode='absolute',則表示從結(jié)果集的第一行移動value條.
cur.execute("select * from users") lines = cur.fetchall() for line in lines: print line cur.execute("select * from users where id=1") line_first = cur.fetchone() #只返回一條 print line_first cur.execute("select * from users") print cur.fetchall()
cursor對象獲取數(shù)據(jù)的方法
游標(biāo)cursor的操作
cur.scroll(n)
或cur.scroll(n,"relative")
:意思是相對當(dāng)前位置向上或者向下移動,n為正數(shù),表示向下(向前),n為負(fù)數(shù),表示向上(向后)
還有一種方式,可以實(shí)現(xiàn)“絕對”移動,不是“相對”移動:增加一個(gè)參數(shù)"absolute"
cur.scroll(1) cur.scroll(-2) cur.scroll(2,"absolute") #回到序號是2,但指向第三條
更新數(shù)據(jù)
cur.execute("update users set username=%s where id=2",("mypython")) conn.commit()
指定數(shù)據(jù)庫
conn = MySQLdb.connect("localhost","root","123456",port=3306,charset="utf8") #創(chuàng)建數(shù)據(jù)庫時(shí)不指定那個(gè)數(shù)據(jù)庫 conn.select_db("test") #連接創(chuàng)建后再指定
關(guān)閉數(shù)據(jù)庫
cur.close() #先關(guān)閉游標(biāo) conn.close() #再關(guān)閉數(shù)據(jù)庫
面向過程和面向?qū)ο蟮木幊?/p>
面向過程的編程:函數(shù)式編程,C程序等
面向?qū)ο蟮木幊蹋篊++,Java,Python等
類和對象:是面向?qū)ο笾械膬蓚€(gè)重要概念
類:是對事物的抽象,比如:汽車模型
對象:是類的一個(gè)實(shí)例,比如:QQ轎車,大客車
范例說明
汽車模型可以對汽車的特征和行為進(jìn)行抽象,然后可以實(shí)例化一臺真實(shí)的汽車實(shí)體出來
Python類的定義
使用class關(guān)鍵字定義一個(gè)類,并且類名的首字母要大寫
當(dāng)程序員需要創(chuàng)建的類型不能用簡單類型表示時(shí)就需要創(chuàng)建類
類把需要的變量和函數(shù)組合在一起,這種包含也稱之為“封裝”
Python類的結(jié)構(gòu)
class 類名: 成員變量 成員函數(shù) class MyClass(): first = 123 def fun(self): print "I am function"
對象的創(chuàng)建
句柄用于區(qū)分不同的對象
對象的屬性和方法與類中的成員變量和成員函數(shù)對應(yīng)
if __name__ == "__main__": myClass = MyClass() #創(chuàng)建類的一個(gè)實(shí)例
創(chuàng)建對象的過程稱之為實(shí)例化;當(dāng)一個(gè)對象被創(chuàng)建后,包含三個(gè)方面的特性:對象的句柄、屬性和方法。
構(gòu)造函數(shù)__init__
class Person: def __init__(self, name, lang, website): self.name = name self.lang = lang self.website = website
self是一個(gè)很神奇的參數(shù)
self指向類的一個(gè)實(shí)例,當(dāng)實(shí)例調(diào)用方法時(shí),self就指向這個(gè)調(diào)用的方法的實(shí)例
子類、父類和繼承
# 抽象形狀類 class Shape: # 類的屬性 edge = 0 # 構(gòu)造函數(shù) def __init__(self, edge): self.edge = edge # 類的方法 def getEdge(self): return self.edge # 抽象方法 def getArea(self): pass #三角形類,繼承抽象形狀類 class Triangle(Shape): width = 0 height = 0 # 構(gòu)造函數(shù) def __init__(self, width, height): #調(diào)用父類構(gòu)造函數(shù) Shape.__init__(self, 3) self.width = width self.height = height #重寫方法 def getArea(self): return self.width * self.height / 2 #四邊形類,繼承抽象形狀類 class Rectangle(Shape): width = 0 height = 0 # 構(gòu)造函數(shù) def __init__(self, width, height): #調(diào)用父類構(gòu)造函數(shù) Shape.__init__(self, 4) self.width = width self.height = height #重寫方法 def getArea(self): return self.width * self.height triangle = Triangle(4,5); print triangle.getEdge() print triangle.getArea() rectangle = Rectangle(4,5); print rectangle.getEdge() print rectangle.getArea()
python支持多繼承,但不推薦使用
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com