mysql与python的交互
来源:互联网 发布:身骑白马 知乎 编辑:程序博客网 时间:2024/05/21 14:41
安装引入模块
安装mysql模块
sudo apt-get install python-mysql
在文件中引入模块
import Mysqldb
Connection对象
- 用于建立与数据库的连接
- 创建对象:调用connect(参数列表)
conn = connect(参数列表)
参数host:连接mysql的主机,‘localhost’ 参数port:连接的mysql主机的端口,默认3306 参数db:数据库名称 参数user:连接的用户名 参数password:连接的密码 参数charset:通信采用的编码方式
对象的方法
- close()关闭连接
- commit()事务,所以需要提交才会生效
- rollback()事务,放弃之前的操作
- cursor()返回Cursor对象,用于执行sql语句并获得结果
Cursor对象
- 执行sql语句
- 创建对象:调用Connection对象的cursor()方法
cursor1=conn.cursor()
对象的方法
- close()关闭
- excute(operation[,parameters])执行语句,返回受影响的行数
- fetchone()执行查询语句时,获得查询结果集的第一个行数据,返回一个元组
- next()执行查询语句时,获取当前行的下一行
- fatchall()执行查询语句时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回
- scroll(value[,mode])将行指针移动到某个位置
mode表示移动的方式
mode的默认值为relative,表示基于当前行移动到value,value为正则向下移动,value为负则向上移动
mode的值为absolute,表示基于第一条数据的位置,第一条数据位置为零
对象的属性
- rowcount只读属性,表示最近一次execute()执行后受影响的行数
- connection获得当前连接对象
增加
-创建testInsert.py文件,向学生表中插入一条数据
#encoding = utf-8import MySQLdbtry: conn=MySQLdb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8') cs1=conn.cursor() count=cs1.execute("insert into students(sname) values('张亮')") print(count) conn.commit() cs1.close()except Exception, e:print(e.message)
修改
-创建testInsert.py文件,修改学生表中一条数据
#encoding = utf-8import MySQLdbtry: conn=MySQLdb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8') cs1=conn.cursor() count=cs1.execute("update students set sname='杨国富'where id=6") print(count) conn.commit() cs1.close()except Exception, e:print(e.message)
删除
-创建testInsert.py文件,删除学生表中一条数据
#encoding = utf-8import MySQLdbtry: conn=MySQLdb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8') cs1=conn.cursor() count=cs1.execute("delete from students where id=6") print(count) conn.commit() cs1.close()except Exception, e:print(e.message)
sql语句参数化
- 创建testInsertParam.py文件,向学生表中插入一条数据
#encoding= utf-8import MySQldbtry: conn = MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8') cs1=conn.cursor() sname=input("请输入学生姓名:") params=[sname] count=cs1.excute('insert into students(sname)value(%s)',params) print(count) cs1.close() conn.close()except Exception,e: print e.message
其他语句
- cursor对象的execute()方法,也可以用于执行create table等语句
- 建议在开发之初,就创建好数据库表结构,不要在这里执行
查询
查询一行数据
- 创建testSelectOne.py文件,查询一条学生信息
#encoding=utf8import MySQLdbtry: conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8') cur=conn.sursor() cur.excude('select * from students where id=7') result=cur.fetchone() print(result) cur.close() conn.close()except Exception,e: print(e.message)
查询多行数据
- 创建testSelectMany.py文件,查询一条学生信息
#encoding=utf8import MySQLdbtry: conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8') cur=conn.sursor() cur.excude('select * from students where id=7') result=cur.fetchall() print(result) cur.close() conn.close()except Exception,e: print(e.message)
封装
- 观察前面的文件发现,除了sql语句及参数不同,其他语句都是一样的
- 创建MysqlHelper.py文件,定义类
#encoding=utf8import MySQLdbclass MysqlHelper(): def __init__(self,host,port,db,user,passwd,charset='utf8'): self.host=host self.port=port self.db=db self.user=user self.passwd=passwd self.charset = charset def connect(self): self.conn=MYSQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset,) self.cursor=self.conn.cursor() def close(self): self.cursor.close() self.conn.close() def get_one(self,sql,params=()): result=None try: self.connect() self.cursor.excute(sql,params) result = self.cursor.fetchone() self.close() except Exception,e: print(e.message) return result def get_all(self,sql,params=()): list=() try: self.connect() self.cursor.execute(sql,params) list=self.cursor.fetchall() self.close() except Exception,e: print(e.message) return list def insert(self,sql,params=()): return self.__edit(sql,params) def update(self,sql,params=()): return self.__edit(sql,params) def delete(self,sql,params=()): return self.__edit(sql,params) def __edit(self,sql,params): count=0 try: self.connect() count=self.cursor.execute(sql,params) self.conn.commit() self.close() except Exception,e: print(e.message) return count
阅读全文
0 0
- MySQL与Python的交互
- python与Mysql的交互
- mysql与python的交互
- python与Mysql交互
- Python与MySQL首次交互
- python脚本与mysql交互
- 三、MySQL与Python交互
- 基于MySQL数据库的C#与Python语言交互调用
- Java、PHP、Python与MySQL交互的性能测试
- python实现与mysql数据库交互
- Python与MySQL交互之用户登录
- python与mysql交互/读取本地配置文件/交互报错
- python与系统的交互
- Shell 与Python的交互
- 数据库与python的交互
- MongoDB与python的交互
- 与mysql交互的技巧
- NodeJS与Mysql的交互
- Matlab图像的几何变换之图像缩放
- C语言中的函数调用(栈帧)
- mybatis相关
- 解决 MATLAB2012 破解文件lic_standalone.dat 2017年11月11号到期
- 剑指offer—二叉树的镜像
- mysql与python的交互
- 3.0的AS在生成apk的时候会导致so文件在 000A9600以后位置中的二进制发生变化。遇到0F会被改成12。
- 创建控件时出错 未知的服务器标记
- 3招教你设计令人“上瘾”的产品
- datatables 常用的操作
- git pull error: your local changes to the following files would be overwritten......
- 文本聚类教程
- Warning:No JDK specified for module
- 欢迎使用CSDN-markdown编辑器