Python 中操作 MySQL 步骤
来源:互联网 发布:linux 设置字符编码 编辑:程序博客网 时间:2024/06/04 23:31
1.引入模块
- 在py文件中引入pymysql模块
from pymysql import *
2.Connection 对象
用于建立与数据库的连接
创建对象:调用connect()方法
conn=connect(参数列表)
- 参数host:连接的mysql主机,如果本机是'localhost'
- 参数port:连接的mysql主机的端口,默认是3306
- 参数database:数据库的名称
- 参数user:连接的用户名
- 参数password:连接的密码
- 参数charset:通信采用的编码方式,推荐使用utf8
对象的方法
- close()关闭连接
- commit()提交
- rollback()回滚
- cursor()返回Cursor对象,用于执行sql语句并获得结果
3.Cursor对象
- 用于执行sql语句,使用频度最高的语句为select、insert、update、delete
- 获取Cursor对象:调用Connection对象的cursor()方法
cs1=conn.cursor()
对象的方法
- close()关闭
- execute(operation [, parameters ])执行语句,返回受影响的行数,主要用于执行insert、update、delete语句,也可以执行create、alter、drop等语句
- fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组
- fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回
对象的属性
- rowcount只读属性,表示最近一次execute()执行后受影响的行数
- connection获得当前连接对象
增删改
- 创建test_insert.py文件,向学生表中插入一条数据
#encoding=utf-8from pymysql import *if __name__=='__main__': try: #创建Connection连接 conn=connect(host='localhost',port=3306,database='test1',user='root',password='mysql',charset='utf8') #获得Cursor对象 cs1=conn.cursor() #执行insert语句,并返回受影响的行数:添加一条学生数据 # 增加 count=cs1.execute('insert into students(name) values("张良")') # 更新 count=cs1.execute('update students set name="刘邦" where id=6') # 删除 count=cs1.execute('delete from students where id=6') #打印受影响的行数 print count #关闭Cursor对象 cs1.close() #提交之前的操作,此处为insert操作 conn.commit() except Exception,e: print e finally: #关闭Connection对象 conn.close()
查询一行数据
- 创建test_one.py文件,查询一条学生信息
#coding=utf-8from pymysql import *if __name__=='__main__': try: #创建Connection连接 conn=connect(host='localhost',port=3306,user='root',password='mysql',database='python1',charset='utf8') #获得Cursor对象 cs1=conn.cursor() #执行select语句,并返回受影响的行数:查询一条学生数据 count=cs1.execute('select id,name from students where id=7') #打印受影响的行数 print count #获取查询的结果 result = cs1.fetchone() #打印查询的结果 print result #关闭Cursor对象 cs1.close() except Exception as e: print e finally: #关闭Connection对象 conn.close()
查询多行数据
- 创建test_many.py文件,查询所有学生信息
#coding=utf-8from pymysql import *if __name__=='main': try: #创建Connection连接 conn=connect(host='localhost',port=3306,user='root',password='mysql',database='python1',charset='utf8') #获得Cursor对象 cs1=conn.cursor() #执行select语句,并返回受影响的行数:查询所有学生数据 count=cs1.execute('select id,name from students') #打印受影响的行数 print count #获取查询的结果 result = cs1.fetchall() #打印查询的结果 print result #关闭Cursor对象 cs1.close() except Exception as e: print e finally: #关闭Connection对象 conn.close()
阅读全文
0 0
- Python 中操作 MySQL 步骤
- Win7下Python操作MySQL步骤
- python操作mysql数据库步骤详解
- Mac下Python操作MySql步骤
- python中mysql基本操作
- C++ 操作mysql步骤
- Mysql新项目操作步骤
- Mac中安装Python+Django+MySQL完整步骤
- python中利用MySQLdb操作mysql(二)
- python中利用MySQLdb操作mysql
- python中mysql的相关操作
- Python中对mysql数据库的操作
- [python]Python操作MySQL
- CMD登录MYSQL操作步骤
- SqlServer链接MySql操作步骤
- Python中操作MySQL中timestamp时间属性
- python操作mysql
- Python操作MySQL
- 项目 http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- 如何更改MVC WebApi 中的请求路径
- 内联函数
- 通过数据告诉你centos和debian哪个好?
- 第4周项目1 建立单链表
- Python 中操作 MySQL 步骤
- spark dataset col,$,column,apply
- read、write系统调用与IO库函数
- firewalld命令参数详解
- 将打好的jar文件上传到jar包的管理服务器
- PostgreSQL学习(一)—常用修改表操作
- Android零基础入门第69节:ViewPager快速实现引导页
- 目标检测算法SSD在Ubuntu+CPU下运行
- ThinkPHP5中的事务操作