一、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. java 解析四则混合运算表达式并计算结果

    package ch8; import java.util.LinkedList; import java.util.List; import java.util.Stack; /** * 四则混合运 ...

  2. Asp.Net MVC之 自动装配、动态路径(链接)等

    一.Model层 using System; using System.Collections.Generic; using System.Linq; using System.Web; namesp ...

  3. 移动端使用页尾文字使用绝对定位遇到input框会飘起来的处理方案

    如下版权信息的样式在遇到input框的时候会跟随输入框其后 优雅的解决方式:(定位遇上键盘飘窗解决) mounted里面写上:var originalHeight=document.documentE ...

  4. 泛型术语:占位类型placeholder

    Here’s a generic version of the same code: struct Stack<Element> { var items = [Element]() mut ...

  5. React和webpack解决 waiting for roots to load...to reload the inspector

    使用chrome调试工具,react-devtools总是显示 "waiting for roots to load...to reload the inspector" and ...

  6. CAD参数绘制块引用对象(网页版)

    主要用到函数说明: _DMxDrawX::DrawBlockReference 绘制块引用对象.详细说明如下: 参数 说明 DOUBLE dPosX 插入点的X坐标 DOUBLE dPosY 插入点的 ...

  7. 实训day01 python基础

    一.编程语言 编程语言:可以被计算机所识别的表达方式. 编程:程序员通过编程语言将自己的想法编写出来,产生的结果就是包含字符的文件. 其中,只有程序在运行时,其中的字符才有特定的语法意义. 二.计算机 ...

  8. JavaSE-20 IO序列化

    学习要点 定义 IO如何序列化 序列化 序列化:是将对象的状态存储到特定存储介质中的过程. 反序列化:从特定存储介质中的数据重新构建对象的过程. 实现了java.io.Serializable接口的类 ...

  9. jquery中ajax原生代码的分析模仿

    function ajax(obj){     var defaults = {         url: "#",         data: {},         type: ...

  10. Hihocoder #1938 最大权闭合子图模板

    这里的讲解很不错,适合作为入坑题: Hihocoder#1938 代码: #include<algorithm> #include<iostream> #include< ...