1.数据库的连接操作

import pymysql 

conn = pymysql.connect(host='localhost', user='root', passwd='', db='oldboydb')  

# host表示ip地址,user表示用户名,passwd表示密码,db表示数据库名称

2. 进行数据库的查询,执行select * from student

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor() effect_row = cursor.execute('select * from student')
print(effect_row) # 打印信息的条数 print(cursor.fetchone()) # 取出一条数据
print(cursor.fetchall()) # 取出剩下的数据

3. 数据的增加操作 insert into student(name, register_data, sex) values('N4', '2015-02-03', 'M')

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor() # 单条数据的插入
cursor.excute('insert into student (name, register_data, sex) values("N4", "2015-02-03", "M")') conn.commit() # 批量数据的插入
data = [
('N1', '2015-05-22', 'M'),
('N2', '2015-02-22', 'F'),
('N3', '2012-02-22', 'F'),
]
# 进行批量插入操作
cursor.executemany('insert into student (name, register_data, sex) values(%s, %s, %s)', data)
print(cursor.lastrowid) # 获取最新的一条数据的索引值
conn.commit() 

4. 进行表User_2的创建

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor() sql = """
create table User_2(
id int auto_increment primary key,
name char(10) not null unique,
age tinyint not null) engine = innodb default charset='utf8';
""" cursor.execute(sql)

conn.commit() # 进行数据的提交
cursor.close() # 关闭光标对象 # 关闭数据库连接
conn.close()

5. 进行数据的删除操作 drop from student where name = '%s'

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor()
# 进行单条数据的删除操作
sq1 = 'drop from student where name = %s'
name = 'N1'
cursor.excute_many(sql, name)
conn.commit() # 批量删除数据
sql = 'drop from student where name = %s'
name = ['N3', 'N4']
cursor.excute_many(sql, name)
conn.commit()

6. 进行数据的属性内容更改  update student set sex = ‘M’ where name = ’Rain‘ and id = 16

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor() sql = 'update student set sex = "F" where name="N1" and id=16'
cursor.execute(sql)
conn.commit()

7. 数据的回滚操作

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='lishentao22', db='oldboydb')

# 创建游标
cursor = conn.cursor() try:
cursor.execute('insert into hobby (id, name, hobby) values("错误的id", "xxx", "iii")')
conn.commit()
except Exception as e:
print(e)
conn.rollback()

python pymysql 连接 mysql数据库进行操作的更多相关文章

  1. django 中连接mysql数据库的操作步骤

    django中连接mysql数据库的操作步骤: 1 settings配置文件中 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mys ...

  2. 使用Python编程语言连接MySQL数据库代码

    使用Python编程语言连接MySQL数据库代码,跟大家分享一下: 前几天我用python操作了mysql的数据库,发现非常的有趣,而且python操作mysql的方法非常的简单和快速,所以我把代码分 ...

  3. Python3.x使用PyMysql连接MySQL数据库

    Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...

  4. Python3.x:使用PyMysql连接Mysql数据库

    Python3.x:使用PyMysql连接Mysql数据库 Python3.x完全不向前兼容,导致Python2.x中可以正常使用的库,到了Python3就用不了: 比如说mysqldb,目前MySQ ...

  5. python3.4怎么连接mysql pymysql连接mysql数据库

    本文介绍了python3 4连接mysql数据库的方法,在python3 4中使用原来python2 7的mysqldb已不能连接mysql数据库了,可以使用pymysql.   在python3.4 ...

  6. 在python中连接mysql数据库,并进行增删改查

    数据库在开发过程中是最常见的,基本上在服务端的编程过程中都会使用到,mysql是较常见的一种数据库,这里介绍python如果连接到数据库中,并对数据库进行增删改查. 安装mysql的python扩展 ...

  7. python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作

    1.通过 pip 安装 pymysql 进入 cmd  输入  pip install pymysql   回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...

  8. python3使用PyMysql连接mysql数据库

    python语言的3 x完全不向前兼容,导致我们在python2 x中可以正常使用的库,到了python3就用不了了 比如说mysqldb目前MySQLdb并不支持python3 python语言的3 ...

  9. python如何连接mysql数据库

    先花点时间来说说一个程序怎么和数据库进行交互1.和数据库建立连接2.执行sql语句,接收返回值3.关闭数据库连接使用MySQLdb也要遵循上面的几步.让我们一步步的进行. 1.MySQL数据库要用My ...

随机推荐

  1. 读书笔记《SpringBoot编程思想》

    目录 一. springboot总览 1.springboot特性 2.准备运行环境 二.理解独立的spring应用 1.应用类型 2.@RestController 3.官网创建springboot ...

  2. Win10带有网络连接的安全模式怎么开启?

    安全模式是在Windows系统中不加载第三方设备驱动程序的情况下启动电脑,从而可以方便的检测与修复电脑系统的错误,比如在安全模式下可以删除某些顽固的文件.查杀病毒.修复系统故障.卸载恶意软件等.不过在 ...

  3. JavaScript在页面中的执行顺序(理解声明式函数与赋值式函数) 转载

    JavaScript在页面中的执行顺序 https://blog.csdn.net/superhoy/article/details/52946277 2016年10月27日 15:38:52 阅读数 ...

  4. TextView跑马灯

    TextView跑马灯 textView跑马灯实现:1.定义textView标签的4个属性:android:singleLine="true"//使其只能单行android:ell ...

  5. FLUSH TABLES WITH READ LOCK 获取锁的速度

    最近有一台MySQL的从库老是报延迟,观察到:FLUSH TABLES WITH READ LOCK,阻塞了4个多小时,还有另外一条SQL语句select *,从现象上来看是select * 阻塞了f ...

  6. QTP(2)

    注意: 在使用QTP录制代码时,能使用鼠标点击的就不要使用键盘操作,能单击的操作就不要使用双击 一.QTP的工作流程 1.录制测试脚本前的准备: a.分析被测系统是否可以实现自动化测试 b.分析被测系 ...

  7. java8学习之Lambda表达式初步与函数式接口

    对于Java8其实相比之前的的版本增加的内容是相当多的,其中有相当一大块的内容是关于Lambda表达式与Stream API,而这两部分是紧密结合而不能将其拆开来对待的,但是是可以单独使用的,所以从学 ...

  8. 使用curl出现,curl: /usr/local/lib/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)

    主要原因是curl找不到openssl的路径,所以只要将openssl的路径添加到相应的变量中就可以了. 参考连接https://blog.csdn.net/RookieWutongshu/artic ...

  9. timestamp和datetime

    datetime数据类型在MySQL之前占8个字节,5.6之后占5个字节,datetime的范围1000-01-01 00:00:00------9999-12-31 23:59:59,格式采用YYY ...

  10. GIT 工作流程常用用命令大全

    一.Git基本工作流程 1.Git工作区域   2.向仓库中添加文件流程 二.Git初始化及仓库创建和操作 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git  config -- g ...