1、数据库操作
1.1准备工作:
1、安装一个mysql数据库软件
2、创建一个数据库,test
3、在python的环境安装一个模块:
MySQLdb
- 1
1.2 安装mysql的连接包
第一种:工具安装
Python3.x版本:Pip install mysqlclient
Python2.x 版本:pip install mysql-python
错误
在win7-64bit的环境下会有错误:unable to find vcvarsall.bat
解决方案:
基本等于误解
第二种:手动安装
(1)先wheel
需要下载安装包:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
根据对应的python版本下载安装包
Pip instal xxxxxxx.whl
(2)安装mysqlclient
下载地址:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
下载后的内容:
选择版本:
在网上有很多安装mysql的操作方法,简单概述就是:
Python2.x 安装mysql-python (如果是Python2.x就安装mysql-python)
Python3.x 安装mysqlclient (如果是Python3.x就安装mysqlclient)
安装步骤:
1.2.1 首先要在cmd下安装wheel包:
D:\baiduDownload\software\python>pip install wheel
- 1
1.2.2 接着进入*.whl安装包所在的目录,用cd命令
D:\baiduDownload\software\python>cd /D D:/baiduDownload/software/python
- 1
1.2.3 使用 pip install *.whl安装mysqlclient
D:\baiduDownload\software\python>pip install mysqlclient
- 1
2、编写python操作数据的代码
import MySQLdb# 打开数据库连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# 使用execute方法执行SQL语句cursor.execute("SELECT VERSION()")# 使用 fetchone() 方法获取一条数据库。data = cursor.fetchone()print("Database version : %s " % data)# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
运行结果:
3、python创建数据库表
import MySQLdb# 打开数据库连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")#使用cursor()方法获取操作游标cursor = db.cursor()# 如果数据表已经存在使用 execute() 方法删除表。cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")# 创建数据表SQL语句sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT)"""cursor.execute(sql)# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
4、插入数据
import MySQLdb#打开数据库连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# SQL 插入语句sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""try: # 执行sql语句 cursor.execute(sql) # 提交到数据库执行 db.commit()except: # Rollback in case there is any error db.rollback()# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
上面的代码可以写成:
import MySQLdb#打开数据连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# SQL 插入语句sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ ('Mac', 'Mohan', 20, 'M', 2000)try: # 执行sql语句 cursor.execute(sql) # 提交到数据库执行 db.commit()except: # 发生错误时回滚 db.rollback()# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
5、查询数据
Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。
fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
fetchall():接收全部的返回结果行.
rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
import MySQLdb#打开数据库连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# SQL 查询语句sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000)try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # 打印结果 print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ))except: print("Error: unable to fecth data")# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
执行后的结果:
6 更新数据库内容
import MySQLdbdb = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# SQL 更新语句sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')try: # 执行SQL语句 cursor.execute(sql) # 提交到数据库执行 db.commit()except: # 发生错误时回滚 db.rollback()# 关闭数据库连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
执行后前的结果是:
更新后的结果:
7 数据库删除操作
删除作用于删除数据表中的数据
删除数据表EMPLOYEE中AGE大于20的所有数据:
import MySQLdb# 打开数据库连接db = MySQLdb.connect("192.168.106.100","root","123456","bigdata")# 使用cursor()方法获取操作游标cursor = db.cursor()# SQL 删除语句sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)try: # 执行SQL语句 cursor.execute(sql) # 提交修改 db.commit()except: # 发生错误时回滚 db.rollback()# 关闭连接db.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
数据删除前的操作:
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow