1:Python2------>Python3
MySQLdb -------> PyMySQL (MySQLdb驅(qū)動從2014年1月停止了維護)
2:安裝
https://pypi.python.org/pypi/PyMySQL
https://github.com/PyMySQL/PyMySQL
或者使用 pip install?PyMySQL?命令
pip show?PyMySQL?查看PyMySQL是否安裝成功
3:python 與 mysql 交互
????????import pymysql.cursors
????????# 連接MySQL數(shù)據(jù)庫
????????connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='198876', db='guest',
????????charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
????????# 通過cursor創(chuàng)建游標(biāo)
????????cursor = connection.cursor()
????????# 創(chuàng)建sql 語句,并執(zhí)行
????????sql =?"INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
????????params = ['gdsf', '215323']
????????count = cursor.execute(sql, params)
? ? ? ? result = cursor.fetchone()? #讀取查詢結(jié)果的一條數(shù)據(jù),result是一個(元祖),查詢機為空集的話result = None
? ? ? ? result1 =?cursor.fetchall() #?返回多個元組,即返回多個記錄((),()),如果沒有結(jié)果 則返回 ()
????????# 提交SQL
????????cursor.close()? ? # 關(guān)閉游標(biāo)
????????connection.commit()
4:數(shù)據(jù)庫增刪改查
查詢:select * from 表名;
插入:insert into 表名 values(...),(...)...;
修改:update 表名 set 列1=值1,列2=值2... where 條件
刪除:delete from 表名 where 條件
5:數(shù)據(jù)庫操作命令:
show databases;? ? ? use 數(shù)據(jù)庫名;? ? ?select database();? ? ?create database 數(shù)據(jù)庫名 charset=utf8;? ?drop database 數(shù)據(jù)庫名;
6:數(shù)據(jù)表操作:
查看當(dāng)前數(shù)據(jù)庫中所有表:show tables;? ?????????查看表結(jié)構(gòu):desc 表名;
創(chuàng)建表:create table 表名(列 類型 約束,...);
修改表-添加字段:alter table 表名 add 列名 類型約束;
修改表-修改字段:(重命名版)alter table 表名 change 原名 新名 類型及約束;
修改表-修改字段:(不重命名版)alter table 表名 modify 列名 類型及約束;
修改表-刪除字段:alter table 表名 drop 列名;
刪除表:drop table 表名;
查看表的創(chuàng)建語句:?show create table 表名;
數(shù)據(jù)庫備份:mysqldump –uroot –p 數(shù)據(jù)庫名 > python.sql;
數(shù)據(jù)庫恢復(fù):mysql -uroot –p 數(shù)據(jù)庫名 < python.sql
數(shù)據(jù)庫查詢:
(1)消除重復(fù)行:select distinct gender from students;
(2)比較運算符: =? ? >? ? >=? ?<? ?<=? ?!=? ?<>
(3)邏輯運算符: and? or? not
(4)模糊查詢: like? ?% 表示任意多個任意字符? _表示一個任意字符
(5)范圍查詢: in 非連續(xù)范圍? ?between 。。。 and。。。 表示連續(xù)區(qū)間
(6)空判斷 : 判空:is null? ?判非空: is? not? null
(7) 優(yōu)先級:優(yōu)先級由高到低的順序為:小括號,not,比較運算符,邏輯運算符;and比or先運算,如果同時出現(xiàn)并希望先算or,需要結(jié)合()使用
(8)聚合函數(shù):count(*)表示計算總行數(shù),括號中寫星與列名,結(jié)果是相同的;max(列)表示求此列的最大值;min(列)表示求此列的最小值;sum(列)表示求此列的和;avg(列)表示求此列的平均值
(9)按照字段分組,表示此字段相同的數(shù)據(jù)會被放到一個組中;分組后,分組的依據(jù)列會顯示在結(jié)果集中,其他列不會顯示在結(jié)果集中;可以對分組后的數(shù)據(jù)進行統(tǒng)計,做聚合運算select gender as 性別, count(*) from students? group by gender;
(10)分組后篩選:having后面的條件運算符與where的相同;where是對from后面指定的表進行數(shù)據(jù)篩選,屬于對原始數(shù)據(jù)的篩選;having是對group by的結(jié)果進行篩選
(11)排序:將行數(shù)據(jù)按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推;默認(rèn)按照列值從小到大排列;asc從小到大排列,即升序;desc從大到小排序,即降序、
(12)獲取部分行:limit start,count? ? ? 從start開始,獲取count條數(shù)據(jù);start索引從0開始
(13)分頁:每頁顯示m條數(shù)據(jù),當(dāng)前顯示第n頁;求總頁數(shù):此段邏輯后面會在python中實現(xiàn);查詢總條數(shù)p1;使用p1除以m得到p2;如果整除則p2為總數(shù)頁;如果不整除則p2+1為總頁數(shù);求第n頁的數(shù)據(jù)
(14)inner? join? on
(15)子查詢:查詢學(xué)生與班級對應(yīng)的信息select * from? (select stu.*,cls.name as clsname from students as stu? inner join classes as cls on stu.clsid=cls.id)? as t1;
(16)總結(jié):from 表名 ;where ....;group by ...;select distinct *;having ...;order by ..;.limit start,count
(17)創(chuàng)建賬戶并且授權(quán):grant 權(quán)限列表 on 數(shù)據(jù)庫 to '用戶名'@'訪問主機' identified by '密碼';
(18)使用root登錄:?mysql -uroot -p