一、Python连接MySQL数据库

  1.导入模块

#导入模块
import pymysql

  2.打开数据库连接

#打开数据库连接
#注意:这里已经假定存在数据库testdb,db指定了连接的数据库,当然这个参数也可以没有
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='testdb', charset='utf8')

  3.创建游标对象cursor

#使用cursor方法创建一个游标
cursor = db.cursor()

二、数据库基本操作

使用execute()方法来实现对数据库的基本操作。

  1.查询数据库版本

#查询数据库版本
cursor.execute("select version()")
data = cursor.fetchone()
print(" Database Version:%s" % data)

  2.创建数据库

#创建数据库test
cursor.execute("drop database if exists test") #如果数据库已经存在,那么删除后重新创建
sql = "create database test"
cursor.execute(sql)

  3.创建数据表

#创建数据库表
cursor.execute("drop table if exists employee") #如果数据表已经存在,那么删除后重新创建
sql = """
CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )
"""
cursor.execute(sql)

  4.查询操作

#查询数据表数据
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

  5.插入操作

#插入数据
sql = "insert into employee values ('李','梅',20,'W',5000)"
cursor.execute(sql)
db.commit()
#查看插入后的结果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

  6.指定条件查询数据

#指定条件查询数据表数据
sql = " select * from employee where income > '%d' " % (1000)
cursor.execute(sql)
data = cursor.fetchone()
print(data)

  7.更新操作

#更新数据库
sql = " update employee set age = age+1 where sex = '%c' " % ('W')
cursor.execute(sql)
db.commit()
#查看更新后的结果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

   8.删除操作

#删除数据
sql = " delete from employee where age > '%d' " % (30)
cursor.execute(sql)
db.commit()
#查看更新后的结果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

三、关闭数据库连接

db.close()

四、其他

  1.说明

  • 上例中"sql=..."语句,是经典的MySQL语句的形式,将数据库语句写在双引号内,形成类似字符串的形式;
  • 使用cursor对象的execute()方法具体执行数据库的操作;
  • 对于插入、更新、删除等操作,需要使用db.commit()来提交到数据库执行,对于查询、创建数据库和数据表的操作不需要此语句。

  2.为有效避免因为错误导致的后果,使用以下方式来执行数据库的操作:

try:
# 执行 SQL 语句
cursor.execute(sql)
# 提交修改
db.commit()
except:
# 发生错误时回滚
db.rollback()

PyMySQL - Python3 MySQL数据库连接 - 基本操作的更多相关文章

  1. Python3 MySQL 数据库连接 -PyMySQL

    Python 3  操作mysql http://www.runoob.com/python3/python3-mysql.html Python3 MySQL 数据库连接 本文我们为大家介绍 Pyt ...

  2. 吴裕雄--天生自然python学习笔记:Python3 MySQL 数据库连接 - PyMySQL 驱动

    什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Python 数据库 AP ...

  3. Python3 MySQL 数据库连接

    什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Python 数据库 AP ...

  4. Python3 MySQL 数据库连接 - PyMySQL 驱动

    什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Python 数据库 AP ...

  5. Python3 MySQL 数据库连接 - PyMySQL 驱动 笔记

    sql插入语句(推荐): str_mac = "nihao" # SQL 插入语句 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ L ...

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

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

  7. Python3 - MySQL适配器 PyMySQL

    本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一 ...

  8. Python3.5 MySQL 数据库连接

    Python3.5  MySQL 数据库连接 在本文中介绍 Python3 使用PyMySQL连接数据库,并实现简单的增删改查 为什么使用PyMySQL? PyMySQL是在Pyhton3.x版本中用 ...

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

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

随机推荐

  1. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  2. Method Dispatch in Protocol Extensions

    We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different d ...

  3. vue vueRouter vuex Axios webpack 前端常用内容

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.

  4. css 最高权重 !important;

    border-top: 1px solid #ccc !important;

  5. error while loading shared libraries: libclntsh.so.11.1

    解决这个问题有两种方法 1.在当前用户下,添加链接库所在路径 LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_ ...

  6. Ubuntu16.04 python3.4.3升级到python3.7.1

    python有两个版本,一个2版本,使用的是python:另一个是3版本,使用的是python3. 简易安装python后得到的3版本的版本号是python3.4.3. 可以使用下面的命令查看py版本 ...

  7. JavaSE-22 反射

    学习要点 反射概念 反射的应用 反射概述 1  反射机制 定义 Java反射机制是指在程序在运行状态中,动态获取信息以及动态调用对象方法的功能. Java反射的动态性质:运行时生成对象实例.运行期间调 ...

  8. Java权限管理(授权与认证)

    CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析   (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...

  9. [Python3网络爬虫开发实战] 2.5-代理的基本原理

    我们在做爬虫的过程中经常会遇到这样的情况,最初爬虫正常运行,正常抓取数据,一切看起来都是那么美好,然而一杯茶的功夫可能就会出现错误,比如403 Forbidden,这时候打开网页一看,可能会看到“您的 ...

  10. Volume 1. Maths - Misc

    113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...