第十六節 MySQLdb "win64位安裝python mysqldb1.2.5" ubuntu下安裝MySQLdb sudo apt get install python MySQLdb 導入MySQLdb庫 import MySQLdb 創建資料庫連接 conn = MySQLdb.conne ...
第十六節 MySQLdb
ubuntu下安裝MySQLdb
sudo apt-get install python-MySQLdb
導入MySQLdb庫
import MySQLdb
創建資料庫連接
conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test",charset="utf8")
- connect對象屬性
commit()
:如果資料庫表進行了修改,提交保存當前的數據。當然,如果此用戶沒有許可權就作罷了,什麼也不會發生。rollback()
:如果有許可權,就取消當前的操作,否則報錯。cursor([cursorclass])
:游標指針。
創建游標(指針)cursor
cur = conn.cursor()
- cursor執行命令的方法:
execute(query, args)
:執行單條sql語句。query為sql語句本身,args為參數值的列表。執行後返回值為受影響的行數。executemany(query, args)
:執行單條sql語句,但是重覆執行參數列表裡的參數,返回值為受影響的行數
在數據表中插入一條記錄
cur.execute("insert into users (username,password,email) values (%s,%s,%s)",("python","123456","[email protected]"))
在數據表中插入多條記錄
cur.executemany("insert into users (username,password,email) values (%s,%s,%s)",(("google","111222","[email protected]"),("facebook","222333","[email protected]"),("github","333444","[email protected]"),("docker","444555","[email protected]")))
提交資料庫操作
conn.commit()
查詢數據
cur.execute("select * from users")
- cursor對象獲取數據的方法
fetchall(self)
:接收全部的返回結果行.fetchmany(size=None)
:接收size條返回結果行.如果size的值大於返回的結果行的數量,則會返回cursor.arraysize條數據.fetchone()
:返回一條結果行.scroll(value, mode='relative')
:移動指針到某一行.如果mode='relative',則表示從當前所在行移動value條,如果mode='absolute',則表示從結果集的第一行移動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對象獲取數據的方法
游標cursor的操作
cur.scroll(n)
或cur.scroll(n,"relative")
:意思是相對當前位置向上或者向下移動,n為正數,表示向下(向前),n為負數,表示向上(向後)還有一種方式,可以實現“絕對”移動,不是“相對”移動:增加一個參數"absolute"
cur.scroll(1) cur.scroll(-2) cur.scroll(2,"absolute") #回到序號是2,但指向第三條
更新數據
cur.execute("update users set username=%s where id=2",("mypython")) conn.commit()
指定資料庫
conn = MySQLdb.connect("localhost","root","123456",port=3306,charset="utf8") #創建資料庫時不指定那個資料庫 conn.select_db("test") #連接創建後再指定
關閉資料庫
cur.close() #先關閉游標 conn.close() #再關閉資料庫
第十七節 面向對象
類和對象
- 面向過程和麵向對象的編程
- 面向過程的編程:函數式編程,C程式等
- 面向對象的編程:C++,Java,Python等
- 類和對象:是面向對象中的兩個重要概念
- 類:是對事物的抽象,比如:汽車模型
- 對象:是類的一個實例,比如:QQ轎車,大客車
- 範例說明
- 汽車模型可以對汽車的特征和行為進行抽象,然後可以實例化一臺真實的汽車實體出來
Python類定義
- Python類的定義
- 使用class關鍵字定義一個類,並且類名的首字母要大寫
- 當程式員需要創建的類型不能用簡單類型表示時就需要創建類
- 類把需要的變數和函數組合在一起,這種包含也稱之為“封裝”
Python類的結構
class 類名: 成員變數 成員函數 class MyClass(): first = 123 def fun(self): print "I am function"
- 對象的創建
- 創建對象的過程稱之為實例化;當一個對象被創建後,包含三個方面的特性:對象的句柄、屬性和方法。
- 句柄用於區分不同的對象
對象的屬性和方法與類中的成員變數和成員函數對應
if __name__ == "__main__": myClass = MyClass() #創建類的一個實例
- 創建對象的過程稱之為實例化;當一個對象被創建後,包含三個方面的特性:對象的句柄、屬性和方法。
構造函數__init__
class Person: def __init__(self, name, lang, website): self.name = name self.lang = lang self.website = website
- self是一個很神奇的參數
- self指向類的一個實例,當實例調用方法時,self就指向這個調用的方法的實例
子類、父類和繼承
# 抽象形狀類 class Shape: # 類的屬性 edge = 0 # 構造函數 def __init__(self, edge): self.edge = edge # 類的方法 def getEdge(self): return self.edge # 抽象方法 def getArea(self): pass #三角形類,繼承抽象形狀類 class Triangle(Shape): width = 0 height = 0 # 構造函數 def __init__(self, width, height): #調用父類構造函數 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 # 構造函數 def __init__(self, width, height): #調用父類構造函數 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支持多繼承,但不推薦使用