Linux下安裝MySQLdb
python 數據庫編程提供的接口
connect(dsn,user,password,host,database,charset)
- dsn: 數據源名稱,給出該參數表示數據庫依賴
- user: 用戶名
- password: 用戶密碼
- host: 主機名
- database: 數據庫名
- charset: 編碼方式
connect對象的方法
- close() 關閉連接之後,鏈接對象和他的遊標都不可用
- commit() 如果支持的話就提交掛起的失誤,否則不做任何事
- rollback() 回滾掛起的事物
- cursor() 返回連接的遊標對象
cursor對象的方法
- close() 關閉遊標
- execute(oper[,params]) 執行一個SQL操作
- executemany(oper,pseq) 對序列中的每一個參數集執行SQL操作
- fetchone() 把查詢結果的下一行保存爲序列
- fetchmany(size) 獲取查詢結果集中的多行,默認爲arraysize
- fetchall() 將所有(剩餘)的行爲作爲序列的序列
- setinputsize() 爲參數預先定義內存區域
- setoutputsize() 爲獲取的大數據值設定緩衝區尺寸
python數據庫操作的基本流程
- 建立和數據庫的鏈接(使用connect對象)
- 獲取遊標對象(connect.cursor)
- 選擇數據庫(沒有的話可以創建)
- 執行SQL語句(cursor對象的方法)
- 提交事務(commit()方法)
- 關閉遊標對象
- 關閉數據庫連接
實例:
Code
#!/usr/bin/python
#coding=utf-8
import sys
import MySQLdb
import wx
'''
處理數據庫類
'''
class database:
def __init__(self):
self.con = MySQLdb.connect(host='localhost',user='root',passwd='20134579',db='zhai')
self.cursor = self.con.cursor()
def insert(self,event):
table = Input.GetValue()
info = Info.GetValue()
sql = "insert into " + table + " values(" +info + ")"
self.cursor.execute(sql)
self.con.commit()
def select_id(self,id):
table = load()
sql = "select * from "+ table +" where id = " + id
self.cursor.execute(sql)
result = cursor.fetchall()
contents.SetValue(result)
def show(self,event):
table = Input.GetValue()
sql = "select * from "+ table
self.cursor.execute(sql)
result = self.cursor.fetchall()
contents.AppendText("That's all the data in table "+table+' \n')
for var in result:
for item in var:
contents.AppendText(str(item)+' , ')
contents.AppendText('\n')
def help(event):
contents.AppendText("the first TextBox is thblename\n")
contents.AppendText("the second TextBox is the infomation you want to add\n")
if __name__ == '__main__':
solve = database()
try:
app = wx.App()
win = wx.Frame(None,title='DadaBase',size=(410,335))
bkg = wx.Panel(win)
showButton = wx.Button(bkg,label = 'Show')
showButton.Bind(wx.EVT_BUTTON,solve.show)
helpButton = wx.Button(bkg,label = 'Help')
helpButton.Bind(wx.EVT_BUTTON,help)
insertButton = wx.Button(bkg,label = 'Insert')
insertButton.Bind(wx.EVT_BUTTON,solve.insert)
selectButton = wx.Button(bkg,label = 'Select')
selectButton.Bind(wx.EVT_BUTTON,solve.select_id)
Input = wx.TextCtrl(bkg)
contents = wx.TextCtrl(bkg,style = wx.TE_MULTILINE | wx.HSCROLL)
Info = wx.TextCtrl(bkg)
hbox = wx.BoxSizer()
hbox.Add(Input, proportion=1,flag = wx.EXPAND)
hbox.Add(Info, proportion=2,flag = wx.EXPAND)
hbox.Add(showButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(insertButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(selectButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(helpButton, proportion=0, flag=wx.LEFT, border=5)
vbox=wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(contents, proportion=1, flag = wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
finally:
solve.cursor.close()
solve.con.close()
實驗結果
開始界面:
Help選項:
Show選項:
Insert選項: