python 数据库操作以及一个GUI界面
来源:互联网 发布:centos 7 ip地址设置 编辑:程序博客网 时间:2024/06/05 07:57
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-8import sysimport MySQLdbimport 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选项:
1 0
- python 数据库操作以及一个GUI界面
- 图书数据库以及操作界面
- 一个GUI界面
- pyFormUI: 一个简单的Python GUI界面框架
- BotHunter的GUI操作界面
- python 3.0 GUI界面例子
- Python: Tkinter实现GUI界面
- Python+PyQt,GUI操作
- Python连接以及操作MySQL数据库详解
- Python连接以及操作MySQL数据库详解
- Python GUI 以及绘图参考
- 6.优化一下代码,添加一个GUI的用户操作界面
- 选择Python GUI界面开发工具
- 选择Python GUI界面开发工具
- Python 简单串口收发GUI界面
- python GUI 和命令行界面的区别
- 实现一个Java GUI计算器应用程序界面
- 给加密程序做一个GUI界面
- 导航--状态栏和导航栏颜色变化
- Lua-require
- test
- 解决mysql级联删除时不会启动delete触发器
- linux命令之awk终极系列
- python 数据库操作以及一个GUI界面
- 简单的ajax评论
- 百度地图生成器
- 安卓自动检测工具--AppsPlayground 的第六节
- 使用iOS企业版正式打包文件并在企业内部分发
- webkit浏览器自定义滚动条样式代码
- ECharts的简单使用
- Your project path contains non-ASCII characters
- 电机的S型曲线加速