python 数据库操作以及一个GUI界面

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选项:
Help选项
Show选项:
Show选项
Insert选项:
Insert选项

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章