1、Python连接MySql数据库
连接数据库前,请先确认以下事项:
        Ⅰ 在你的机子上已经安装了 Python MySQLdb 模块。
        Ⅱ 您已经创建了数据库 test
        Ⅲ 连接数据库test使用的用户名为 root,密码为 root,你可以可以自己设定或者直接使用root用户名及其密码。
[python] view plain copy
- # *===================================*
 - # * Created by Zhihua_w.
 - # * Author: Wei ZhiHua
 - # * Date: 2017/1/10 0003
 - # * Time: 下午 2:28
 - # * Project: PYTHON STUDY
 - # * Power: DATABASE
 - # *===================================*
 - import pymysql
 - # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 - db = pymysql.connect("localhost", "root", "root", "test")
 - # 使用 cursor() 方法创建一个游标对象 cursor
 - cursor = db.cursor()
 - # 使用 execute() 方法执行 SQL 查询
 - cursor.execute("SELECT VERSION()")
 - # 使用 fetchone() 方法获取单条数据.
 - data = cursor.fetchone()
 - print("Database version : %s " % data)
 - # 关闭数据库连接
 - db.close()
 
2、Python操作MySql数据库实现增删改查
① 数据库插入操作
[python] view plain copy
- # *===================================*
 - # * Created by Zhihua_w.
 - # * Author: Wei ZhiHua
 - # * Date: 2017/1/10 0004
 - # * Time: 下午 2:32
 - # * Project: PYTHON STUDY
 - # * Power: DATABASE
 - # *===================================*
 - import pymysql
 - # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 - db = pymysql.connect("localhost", "root", "root", "test")
 - # 使用 cursor() 方法创建一个游标对象 cursor
 - cursor = db.cursor()
 - # SQL 插入语句
 - sql = """INSERT INTO user(name)
 - VALUES ('Mac')"""
 - try:
 - # 执行sql语句
 - cursor.execute(sql)
 - # 提交到数据库执行
 - db.commit()
 - except:
 - # 如果发生错误则回滚
 - db.rollback()
 - # 关闭数据库连接
 - db.close()
 
② 数据库查询
[python] view plain copy
- # *===================================*
 - # * Created by Zhihua_w.
 - # * Author: Wei ZhiHua
 - # * Date: 2017/1/10 0005
 - # * Time: 下午 2:39
 - # * Project: PYTHON STUDY
 - # * Power: DATABASE
 - # *===================================*
 - import pymysql
 - # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 - db = pymysql.connect("localhost", "root", "root", "test")
 - # 使用 cursor() 方法创建一个游标对象 cursor
 - cursor = db.cursor()
 - # SQL 查询语句
 - sql = "SELECT * FROM user"
 - try:
 - # 执行SQL语句
 - cursor.execute(sql)
 - # 获取所有记录列表
 - results = cursor.fetchall()
 - for row in results:
 - 0]
 - 1]
 - # 打印结果
 - print("id=%s,name=%s" % \
 - (id, name))
 - except:
 - print("Error: unable to fecth data")
 - # 关闭数据库连接
 - db.close()
 
③ 数据库更新
[php] view plain copy
- # *===================================*
 - # * Created by Zhihua_w.
 - # * Author: Wei ZhiHua
 - # * Date: 2017/1/10 0005
 - # * Time: 下午 2:39
 - # * Project: PYTHON STUDY
 - # * Power: DATABASE
 - # *===================================*
 - import pymysql
 - # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 - db = pymysql.connect("localhost", "root", "root", "test")
 - # 使用 cursor() 方法创建一个游标对象 cursor
 - cursor = db.cursor()
 - # SQL 更新语句
 - sql = "UPDATE user SET name = 'Bob' WHERE id = 1"
 - try:
 - # 执行SQL语句
 - cursor.execute(sql)
 - # 提交到数据库执行
 - db.commit()
 - except:
 - # 发生错误时回滚
 - db.rollback()
 - # 关闭数据库连接
 - db.close()
 
④ 数据库删除
[php] view plain copy
- # *===================================*
 - # * Created by Zhihua_w.
 - # * Author: Wei ZhiHua
 - # * Date: 2017/1/10 0006
 - # * Time: 下午 2:49
 - # * Project: PYTHON STUDY
 - # * Power: DATABASE
 - # *===================================*
 - import pymysql
 - # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 - db = pymysql.connect("localhost", "root", "root", "test")
 - # 使用 cursor() 方法创建一个游标对象 cursor
 - cursor = db.cursor()
 - # SQL 删除语句
 - sql = "DELETE FROM user WHERE id = 1"
 - try:
 - # 执行SQL语句
 - cursor.execute(sql)
 - # 提交修改
 - db.commit()
 - except:
 - # 发生错误时回滚
 - db.rollback()
 - # 关闭数据库连接
 - db.close()
 










