pymysql操作mysql数据库
1.建库
- import pymysql
- # 建库
- try:
- conn=pymysql.connect(
- host='127.0.0.1',
- port=3306,
- user='root',
- passwd='',
- )
- cur=conn.cursor()
- create_database_sql='CREATE DATABASE IF NOT EXISTS py3_tstgr DEFAULT CHARSET utf8 COLLATE utf8_general_ci;'
- cur.execute(create_database_sql)
- cur.close()
- print('创建数据库 py3_tstgr 成功!')
- except pymysql.Error as e:
- print('pymysql.Error: ',e.args[0],e.args[1])
2.建表
- import pymysql
- # 建表
- try:
- conn=pymysql.connect(
- host='127.0.0.1',
- port=3306,
- user='root',
- passwd='',
- db='py3_tstgr',
- charset='utf8'
- )
- cur=conn.cursor()
- cur.execute('drop table if exists user;')
- create_table_sql='''
- CREATE TABLE user(
- id int(11) DEFAULT NULL ,
- name VARCHAR(50) DEFAULT NULL ,
- password VARCHAR(30) DEFAULT NULL ,
- birthday TIMESTAMP DEFAULT now()
- )engine=innodb DEFAULT CHARACTER set utf8;
- '''
- cur.execute(create_table_sql)
- print('创建数据库表成功!')
- except pymysql.Error as e:
- print('mysql.Error: ',e.args[0],e.args[1])
3.增、改、删、查数据操作
- import pymysql
- # 数据库操作
- # 连接
- def db_conn(host,port,user,passwd,db_name):
- try:
- conn=pymysql.connect(
- host=host,
- port=port,
- user=user',
- passwd=password,
- db=db_name,
- charset='utf8',
- )
- # cur=conn.cursor()
- print('数据库连接成功!')
- # 返回连接
- return conn
- except pymysql.Error as e:
- print('数据库连接失败')
- print('mysql.Error: ',e.args[0],e.args[1])
- def db_cur(conn):
- # 获取游标
- cur=conn.cursor()
- return cur
- def db_close(cur,conn):
- # 游标关闭
- cur.close()
- # 提交事务
- conn.commit()
- # 连接关闭
- conn.close()
- # 插入单行数据
- def db_insert_data(sql,cur,*args):
- try:
- # print(args)
- result=cur.execute(sql,args)
- print('添加语句受影响的行数:',result)
- except Exception as e:
- print('db_insert_data error: ',e.args)
- # 批量插入数据
- def db_insert_datas(sql,cur,list_datas):
- try:
- result=cur.executemany(sql,list_datas)
- print('批量插入受影响的行数:',result)
- except Exception as e:
- print('db_insert_datas error: ',e.args)
- # 修改单行数据
- def db_update(sql,cur):
- result=cur.execute(sql)
- print('修改语句受影响的行数:',result)
- # 批量修改数据
- def db_update_datas(sql,cur,list_datas):
- try:
- result=cur.executemany(sql,list_datas)
- print('批量修改受影响的行数:',result)
- except Exception as e:
- print('db_update_datas error: ',e.args)
- # 删除单行数据
- def db_delete_data(sql,cur):
- result=cur.execute(sql)
- print('删除语句受影响的行数:',result)
- # 批量删除数据
- def db_delete_datas(sql,cur,list_datas):
- try:
- result=cur.executemany(sql,list_datas)
- print('批量删除受影响的行数:',result)
- except Exception as e:
- print('db_delete_datas error: ',e.args)
- # 回滚
- def roll_back(conn):
- try:
- conn.rollback()
- print('回滚完成!')
- except Exception as e:
- print('rollback error: ',e.args)
- def db_select_data(sql,cur):
- result=cur.execute(sql)
- print('查询语句受影响的行数:',result)
- def db_select_datas(sql,cur,list_datas):
- try:
- result=cur.executemany(sql,list_datas)
- print('批量查询受影响的行数:',result)
- except Exception as e:
- print('db_select_datas error: ',e.args)
- if __name__=="__main__":
- host='127.0.0.1'
- port=3306
- user='root'
- passwd=''
- db='py3_tstgr'
- conn=db_conn(host,port,user,passwd,db)
- print(conn)
- cur=db_cur(conn)
- print(cur)
- insert_sql="insert into user values(1,'tom','123','1990-01-01');"
- db_insert_data(insert_sql,cur)
- insert_sql2="insert into user values(%s,%s,%s,%s);"
- db_insert_data(insert_sql2,cur,2,'lucy','aaa','1991-02-02')
- insert_datas_sql="insert into user values(%s,%s,%s,%s);"
- list_datas=[(2,'ha2','','1992-02-02'),(3,'ha3','','1993-03-03'),(4,'ha4','','1994-04-04')]
- db_insert_datas(insert_datas_sql,cur,list_datas)
- # 查询数据
- sql="select * from user;"
- cur.execute(sql)
- res1=cur.fetchone() # 获取一行数据
- print(res1)
- res2=cur.fetchmany(2) #获取多行数据
- print(res2)
- res3=cur.fetchall() # 获取所有数据
- print(res3)
- print(cur.fetchone()) # None
- # 重置游标
- cur.scroll(1,mode='relative')
- print(cur.fetchone())
- # 更新数据
- up_sql="update user set name='lala' where password='123';"
- db_update(up_sql,cur)
- # 批量更新数据
- up_sqls="update user set name=%s where password=%s;"
- up_datas=[('lw',''),('lc','aaa')]
- db_update_datas(up_sqls,cur,up_datas)
- # 删除数据
- delete_sql="delete from user where name='lw';"
- db_delete_data(delete_sql,cur)
- delete_sqls="delete from user where name=%s;"
- db_delete_datas(delete_sqls,cur,[('lc'),('hah2'),('hah3')])
- # print(cur.rownumber) # 0 获取所在行号
- # cur.scroll(0,mode='absolute')
- # cur.execute('select * from user;')
- select_sql="select * from user;"
- db_select_data(select_sql,cur)
- print(cur.fetchall())
- # roll_back(conn) # 回滚
- # print(cur.rownumber)
- # cur.scroll(0, mode='absolute')
- # cur.execute('select * from user;')
- # 查询单行
- select_sql = "select * from user;"
- db_select_data(select_sql, cur)
- # 批量查询
- select_sqls="select * from user where name=%s;"
- db_select_datas(select_sqls,cur,[('ha1'),('ha2'),('ha3')])
- print(cur.fetchall())
- db_close(cur,conn) # 断开连接
pymysql操作mysql数据库的更多相关文章
- flask + pymysql操作Mysql数据库
安装flask-sqlalchemy.pymysql模块 pip install flask-sqlalchemy pymysql ### Flask-SQLAlchemy的介绍 1. ORM:Obj ...
- 用pymysql操作MySQL数据库
工具库安装 pip install pymysql 连接关闭数据库与增删改查操作 # 导入pymysql库 import pymysql # 打开数据库连接 # 参数1:数据库服务器所在的主机+端口号 ...
- python使用pymysql操作mysql数据库
1.安装pymysql pip install pymysql 2.数据库查询示例 import pymysql # 连接database conn =pymysql.connect(user=' , ...
- PyMySQL操作mysql数据库(py3必学)
一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Python操作MySQL数据库需要使用驱动.这里选用PyMySQL驱动. 安装方式还是使用pip命令. ...
- python 3.6 +pyMysql 操作mysql数据库
版本信息:python:3.6 mysql:5.7 pyMysql:0.7.11 ########################################################### ...
- 使用pymysql 操作MySQL数据库
安装 pip install pymysql 注:连接前要有可使用的账户及有权限.可操作的数据库 先来一个栗子: import pymysql # 连接database conn = pymysql. ...
- 使用pymysql操作mysql数据库
PyMySQL的安装和连接 PyMySQL的安装 python3. -m pip install pymysql python连接数据库 import pymysql # 创建连接 conn = py ...
- pymysql操作mysql
一.使用PyMySQL操作mysql数据库 适用环境 python版本 >=2.6或3.3 mysql版本>=4.1 安装 可以使用pip安装也可以手动下载安装.使用pip安装,在命令行执 ...
- python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy
内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...
随机推荐
- Spring cloud微服务安全实战-3-7API安全机制之数据加密
这一节来聊一下密码的加密. 加密盐,为了避免两个相同的面加密出来的密文是一样的,每个人的盐不一样, 首先引入工具包,lambdaworks <!-- https://mvnrepository. ...
- 基于Python的WEB接口开发与自动化测试 pdf(内含书签)
基于Python的WEB接口开发与自动化测试 目录 目 录O V目 录章 Python 学习必知 ................................................... ...
- F110增强
1.F110 删除操作的增强: 方法:SE19 ZE_F110_DELETE_CHECK 代码: ENHANCEMENT 1 ZE_F110_DELETE_CHECK. "act ...
- window.open post传参
目录 前言 获取当前用户信息 使用window.open的两种方式 Get方式 Post方式 前言 我使用的场景是,点击弹窗,然后把我当前用户的消息传过去 获取当前用户信息 打开Chrome浏览器,在 ...
- Java连接MongoDB示例
示例代码: package com.zifeiy.snowflake.handle.etl.mongodb; import java.util.ArrayList; import java.util. ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- CSS3 mask 遮罩蒙版效果
mask demo效果演示:http://dtdxrk.github.io/game/css3-demo/mask.html mask 的属性: -webkit-mask-image:url | gr ...
- git的使用学习(一)git的简介和安装
Git简介 Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制系统? 如果你用Microsoft Word写 ...
- sublime text3 修改快捷键为eclipse
Preferences -> Key bindings - User [ { "keys": ["shift+enter"], "command ...
- activeMq学习应用
一.下载 ActiveMQ 5.15.0下载地址 二.安装 解压apache-activemq-5.15.0-bin.zip D:\apache-activemq-5.15.7-bin\apache- ...