一,安装MySQL
这里小编就不详细介绍了,要是有不会安装的可以参考下面博客
http://www.cnblogs.com/wj-1314/p/7573242.html
二,安装MySQL-python
要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块。
下载地址:https://pypi.python.org/pypi/MySQL-python/
下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:
>>python setup.py install
然后安装pymysql
pip install pymysql
三,测试pymysql模块
测试非常简单,检查MySQLdb 模块是否可以正常导入。

没有报错提示MySQLdb模块找不到,说明安装OK
四,mysql 的基本操作
mysql> show databases; // 查看当前所有的数据库 +--------------------+ | Database | +--------------------+ | information_schema | | csvt | | csvt04 | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.18 sec) mysql> use test; //作用与test数据库 Database changed mysql> show tables; //查看test库下面的表 Empty set (0.00 sec) //创建user表,name 和password 两个字段 mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec) //向user表内插入若干条数据 mysql> insert into user values(‘Tom‘,‘1321‘); Query OK, 1 row affected (0.05 sec) mysql> insert into user values(‘Alen‘,‘7875‘); Query OK, 1 row affected (0.08 sec) mysql> insert into user values(‘Jack‘,‘7455‘); Query OK, 1 row affected (0.04 sec) //查看user表的数据 mysql> select * from user; +------+----------+ | name | password | +------+----------+ | Tom | 1321 | | Alen | 7875 | | Jack | 7455 | +------+----------+ rows in set (0.01 sec) //删除name 等于Jack的数据 mysql> delete from user where name = ‘Jack‘; Query OK, 1 rows affected (0.06 sec) //修改name等于Alen 的password 为 1111 mysql> update user set password=‘1111‘ where name = ‘Alen‘; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 //查看表内容 mysql> select * from user; +--------+----------+ | name | password | +--------+----------+ | Tom | 1321 | | Alen | 1111 | +--------+----------+ rows in set (0.00 sec)
五,python 操作mysql数据库基础
1,执行SQL,具体语句如下:
#coding=utf-8 import MySQLdb
# 打开数据库连接
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
# 使用cursor()方法获取操作游标
cur = conn.cursor()
#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
#插入一条数据
#cur.execute("insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)")
#修改查询条件的数据
#cur.execute("update student set class=‘3 year 1 class‘ where name = ‘Tom‘")
#删除查询条件的数据
#cur.execute("delete from student where age=‘9‘")
# 关闭游标
cur.close()
# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭数据库连接
conn.close()
2,获取新创建数据自增ID
import pymysql
conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)
cursor = conn.cursor()
cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
conn.commit()
cursor.close()
conn.close()
# 获取最新自增ID
new_id = cursor.lastrowid
3、获取查询数据
import pymysql
conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)
cursor = conn.cursor()
cursor.execute("select * from hosts")
# 获取第一行数据
row_1 = cursor.fetchone()
# 获取前n行数据
# row_2 = cursor.fetchmany(3)
# 获取所有数据
# row_3 = cursor.fetchall()
conn.commit()
cursor.close()
conn.close()
fetchone()方法可以帮助我们获得表中的数据,可是每次执行cur.fetchone() 获得的数据都不一样,换句话说我没执行一次,游标会从表中的第一条数据移动到下一条数据的位置,所以,我再次执行的时候得到的是第二条数据。
注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
- cursor.scroll(1,mode=‘relative‘) # 相对当前位置移动
- cursor.scroll(2,mode=‘absolute‘) # 相对绝对位置移动
4、fetch数据类型
关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:
import pymysql
conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, passwd=‘123‘, db=‘t1‘)
# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute("call p1()")
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
5,插入数据
通过上面execute()方法中写入纯的sql语句来插入数据并不方便。如:
>>>cur.execute("insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)")
我要想插入新的数据,必须要对这条语句中的值做修改。我们可以做如下修改:
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor()
#插入一条数据
sqli="insert into student values(%s,%s,%s,%s)"
cur.execute(sqli,(‘3‘,‘Huhu‘,‘2 year 1 class‘,‘7‘))
cur.close()
conn.commit()
conn.close()
假如要一次向数据表中插入多条值呢?
executemany()方法可以一次插入多条值,执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数。
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor()
#一次插入多条记录
sqli="insert into student values(%s,%s,%s,%s)"
cur.executemany(sqli,[
(‘3‘,‘Tom‘,‘1 year 1 class‘,‘6‘),
(‘3‘,‘Jack‘,‘2 year 1 class‘,‘7‘),
(‘3‘,‘Yaheng‘,‘2 year 2 class‘,‘7‘),
])
cur.close()
conn.commit()
conn.close()
六,巩固练习
练习题:
参考表结构:
用户类型
用户信息
权限
用户类型&权限
功能:
# 登陆、注册、找回密码
# 用户管理
# 用户类型
# 权限管理
# 分配权限
特别的:程序仅一个可执行文件