本文主要介绍在python中如何使用MySQL数据库。python
apt-get install mysql-server
mysql
安装完成以后在命令行中输入:mysql -uroot -proot
,看是否可以成功登入MySQL命令行,若是可以成功登入,则说明安装成功。sql
下载MSI安装包mysql-installer-community-5.7.19.0.msi安装,官网地址:https://dev.mysql.com/downloads/installer/数据库
注:注意下载页面这样一句话:Note: MySQL Installer is 32 bit, but will install both 32 bit and 64 bit binaries.bash
也就是说32位版本和64位版本的安装包是二合一的。服务器
下载以后打开一路Next安装便可,只需在有一步设置帐号的时候设置好MySQL的登陆用户名和密码便可(好比我设置的用户名/密码是:root/root)。函数
安装完成以后在命令行中输入:mysql -uroot -proot
,看是否可以成功登入MySQL命令行,若是可以成功登入,则说明安装成功;若是提示找不到mysql命令,则还须要手工把mysql.exe的路径添加到环境变量,好比个人路径是:C:\Program Files\MySQL\MySQL Server 5.7\bin
fetch
pip install MySQL-python
ui
Windows下直接用pip install MySQL-python
命令安装会报错:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h':No such file or directory命令行
解决方法:
在命令行中输入python
命令打开python控制台,查看本地安装的python是32位的仍是64位的,而后根据本地python的位数来下载以下两个安装包的其中之一:
32位:MySQL-python-1.2.5.win32-py2.7.exe,下载地址:https://pan.baidu.com/s/1qYa5H4w
64位:MySQL-python-1.2.5.win-amd64-py2.7.exe,下载地址:https://pan.baidu.com/s/1qYa5H4w
在安装时可能还会遇到问题:python version 2.7 required which was not found in the registry
解决方法:下载register.py文件,并在命令行中用python命令执行该脚本便可,下载地址:https://pan.baidu.com/s/1mihz25M
进入python交互式环境,输入:import MySQLdb
,若是没有报错,则代表安装mysql及MySQL-python成功。
若是平常使用的是virtualenv环境,由于virtualenv是相对独立的环境,因此还须要单独安装MySQL-python。
pip install MySQL-python
在主环境安装好MySQL-python后,进入python安装目录(如:C:\Python27)下的lib\site-packages目录下,找到以下四组文件/文件夹:
MySQL_python-1.2.5-py2.7.egg-info
(文件夹)
MySQLdb
(文件夹)
_mysql_exceptions.py/_mysql_exceptions.pyc/_mysql_exceptions.pyo
(文件)
_mysql.pyd
(文件)
而后把以上几个文件/文件夹都复制到virtualenv安装目录的lib\site-packages目录下。而后在virtualenv环境下进入pyton交互式环境,输入import MySQLdb
验证是否成功。
mysql -uroot -proot
如下命令都是在登陆mysql后的mysql命令行中执行的:
查看当前有哪些数据库:show databases;
建立一个新的名为info的数据库:create database info;
切换到info数据库:use info;
建立一个表person:create table person(id int not null auto_increment primary key,name varchar(32),age int);
查看当前有哪些表:show tables;
往person表中插入一些数据:
insert into person(name,age) values('Tom',18); insert into person(name,age) values('John',23); insert into person(name,age) values('Amy',15);
select * from person;
mysql> select * from person; +----+------+------+ | id | name | age | +----+------+------+ | 1 | Tom | 18 | | 2 | John | 23 | | 3 | Amy | 15 | +----+------+------+ 3 rows in set (0.00 sec)
导入包:
import MySQLdb
获取数据库链接对象:
conn = MySQLdb.connect(user = 'root',passwd = 'root',host = '127.0.0.1')
注:MySQLdb.connect()函数能够接收的经常使用的几个参数:
host:链接的服务器主机名,默认为本机(localhost)
user:数据库用户名,默认为当前用户
passwd:用户登陆密码,无默认值
db:链接的数据库名,无默认值
read_default_file:使用指定的mysql配置文件
port:链接端口,默认为3306
connect_timeout:链接超时时间,单位为秒
获取游标(至关于一个指针):
cur = conn.cursor()
设置当前数据库为info:
conn.select_db('info')
注:不建议在python中操做数据库建立表。
假如向person表中插入数据:
sql = 'insert into person(name,age) values("Zhangsan",34)' # 组装sql cur.execute(sql) # 执行sql conn.commit() # 提交,若是没有这句,更改不会生效 cur.close() conn.close() # 用完以后最好关闭游标和链接对象
此时去mysql中,进入info数据库,查询:select * from person;
,结果以下:
mysql> select * from person; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | Tom | 18 | | 2 | John | 23 | | 3 | Amy | 15 | | 4 | Zhangsan | 34 | +----+----------+------+ 4 rows in set (0.00 sec)
发现成功插入一条数据。
使用占位符和列表:
sql = 'insert into person(name,age) values(%s,%s)' cur.execute(sql,('Lisi',23)) # 插入一条数据 persons = [('Wangwu',32),('Zhaoliu',12),('Tianqi',45)] cur.executemany(sql,persons) # 插入多条 conn.commit() # 提交
此时去mysql中,进入info数据库,查询:select * from person;
,结果以下:
mysql> select * from person; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | Tom | 18 | | 2 | John | 23 | | 3 | Amy | 15 | | 4 | Zhangsan | 34 | | 5 | Lisi | 23 | | 6 | Wangwu | 32 | | 7 | Zhaoliu | 12 | | 8 | Tianqi | 45 | +----+----------+------+ 8 rows in set (0.07 sec)
sql = 'delete from person where name = "Tianqi"' cur.execute(sql) conn.commit()
mysql> select * from person; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | Tom | 18 | | 2 | John | 23 | | 3 | Amy | 15 | | 4 | Zhangsan | 34 | | 5 | Lisi | 23 | | 6 | Wangwu | 32 | | 7 | Zhaoliu | 12 | +----+----------+------+ 7 rows in set (0.00 sec)
sql = 'update person set age = 88 where name = "Zhaoliu"' cur.execute(sql) conn.commit()
mysql> select * from person; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | Tom | 18 | | 2 | John | 23 | | 3 | Amy | 15 | | 4 | Zhangsan | 34 | | 5 | Lisi | 23 | | 6 | Wangwu | 32 | | 7 | Zhaoliu | 88 | +----+----------+------+ 7 rows in set (0.00 sec)
sql = 'select * from person' cur.execute(sql) # 取查到的全部结果,并把游标移到结尾 print '【Output 1】' print cur.fetchall() # 把游标移到开头 cur.scroll(0,'absolute') # 取查到的前n条数据,并把游标移到第n+1位置 print '【Output 2】' print cur.fetchmany(3) # 把游标移到开头 cur.scroll(0,'absolute') # 取查到的一条数据,并把游标向后移动一位 print '【Output 3】' print cur.fetchone() print cur.fetchone()
【Output 1】 ((1L, 'Tom', 18L), (2L, 'John', 23L), (3L, 'Amy', 15L), (4L, 'Zhangsan', 34L), (5L, 'Lisi', 23L), (6L, 'Wangwu', 32L), (7L, 'Zhaoliu', 88L)) 【Output 2】 ((1L, 'Tom', 18L), (2L, 'John', 23L), (3L, 'Amy', 15L)) 【Output 3】 (1L, 'Tom', 18L) (2L, 'John', 23L)