複習: 1. MySQL:文件管理的軟件 2. 三部分: - 服務端 - SQL語句 - 客戶端 3. 客戶端: - MySQL - navicat 4. 受權操做: - 用戶操做 - 受權操做 5. SQL語句 - 數據庫操做 - 建立數據庫要指定字符編碼: create database xx default charset=utf8; - 刪除數據庫: drop database xx; - 數據表操做 - 列 - 數字 整數 小數 - 字符串 - 時間 datatime - 二進制 - 其餘:引擎、字符編碼、起始值 - 主鍵索引 - 惟一索引 - 外鍵 - 一對多 - 一對一 - 多對多 PS:數據放在硬盤上 - 數據行操做 - 增 - 刪 - 改 - 查 - in not in - between and - limit - group by having - order by -desc -asc - like '%a' - left join xx on 關係 - inner join - 臨時表 select * from(select * from tb1 where id<10) as B; PS:笛卡爾積 a:三條數據 1 2 3 select * from a as a1,a as a2; 1 一、1 二、1 三、2 一、2 二、2 三、3 一、3 二、3 3
一. 練習題講解python
1. 去重:distinct -- 效率不高 SELECT distinct student.sid,student.sname from score LEFT JOIN student ON score.student_id=student.sid where score.num<60;
二. pymysql模塊:
pip3 install pymysql -i https://pypi.douban.com/simple
python模塊:對數據庫進行操做(SQL語句)mysql
import pymysql ''' user = input('username:') pwd = input('possword:') conn = pymysql.connect(host='localhost', user='root', password='lpg123456', database='db1') # 打開數據庫 cursor = conn.cursor() # 遊標 # 鏈接數據庫成功 sql = "select * from userinfo where username = %(u)s and password = %(p)s " cursor.execute(sql, {'u': user, 'p': pwd}) result = cursor.fetchone() cursor.close() conn.close() if result: print('登陸成功') else: print('登陸失敗') ''' # 增 刪 改 ''' conn = pymysql.connect(host='localhost',user='root',password='lpg123456',database='db1') cursor = conn.cursor() sql = "insert into userinfo(username, password) values('egon','123456')" cursor.execute(sql) conn.commit() # 提交 增 刪 改 須要用commit cursor.close() conn.close() ''' ''' user = 'wusir' pwd = '123456' conn = pymysql.connect(host='localhost',user='root',password='lpg123456',database='db1') cursor = conn.cursor() # sql = "insert into userinfo(username, password) values(%s,%s)" # cursor.execute(sql,[user,pwd]) # 增長多個 sql = "insert into userinfo(username, password) values(%s,%s)" # r:受影響的行數 r = cursor.executemany(sql,[('laoyao','sb'),('taibai','SB')]) # 只適用於insert conn.commit() # 提交 增 刪 改 須要用commit cursor.close() conn.close() ''' # 查 ''' conn = pymysql.connect(host='localhost',user='root',password='lpg123456',database='db1') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 默認cursor爲None,改爲以字典形式打印 sql = "select * from userinfo limit 10" # r:受影響的行數 cursor.execute(sql) # result = cursor.fetchone() # 一次取一個 # print(result) # # result = cursor.fetchone() # print(result) # # result = cursor.fetchone() # print(result) # result = cursor.fetchmany(4) # 一次取多個 # print(result) result = cursor.fetchall() # 一次取全部 print(result) # [{'id': 1, 'username': 'alex', 'password': '123456'}, {'id': 2, 'username': 'egon', 'password': '123456'}, {'id': 5, 'username': 'wusir', 'password': '123456'}, {'id': 6, 'username': 'laoyao', 'password': 'sb'}, {'id': 7, 'username': 'taibai', 'password': 'SB'}] cursor.close() conn.close() ''' # 新插入數據的自增ID :cursor.lastrowid conn = pymysql.connect(host='localhost',user='root',password='lpg123456',database='db1') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "insert into userinfo(username,password) values('asdsafd','467467')" cursor.execute(sql) conn.commit() print(cursor.lastrowid) cursor.close() conn.close()
- 鏈接、關閉(遊標) - execute() --> SQL注入問題 - 增刪改 : conn.commit() - fetchone() - fetchmany() - fetchall() - 獲取插入數據的自增ID:lastrowid