本文主要介紹在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)