1、pymysql模塊 1、說明: 想在python代碼中鏈接上mysql數據庫,就須要使用pymysql模塊, pymysql是在 Python3.x 版本中用於鏈接 MySQL 服務器的一個庫,在Python2中則使用mysqldb。 Django中也可使用PyMySQL鏈接MySQL數據庫。 2、 安裝 在cmd窗口執行命令行:pip install pymysql # 注意:若是電腦上安裝了兩個版本的python解釋器,則應該明確pip的版本,pip2仍是pip3。 補充 pip -V --> 查看當前pip的版本 pip list --> 查看當前python解釋器環境中安裝的第三方包和版本(安裝完成後可用此命令查看是否安裝成功) 3、注意事項 應該肯定你有一個MySQL數據庫,而且已經啓動 應該肯定你有能夠鏈接該數據庫的用戶名和密碼 應該肯定你有一個有權限操做的database 4、使用步驟 1. 導入pymysql模塊 import pymysql 2. 鏈接database conn = pymysql.connect( host='127.0.0.1', # 數據庫的IP地址 port=3306, # 數據庫的端口 user='root', # 用戶名 password='123abc' # 密碼 database='test', # 具體的一個數據庫(文件夾) charset='urf8' # 編碼方式,注意:charset='utf8',不要寫成'utf-8' ) 3. 獲取光標對象 cursor = conn.cursor() 4. 執行SQL語句 cursor.execute('select * from userinfo;') 5. 關閉 1. 關閉光標 cursor.close() 2. 關閉鏈接 conn.close() 6.例子 import pymysql # 獲取用戶輸入 name = input('請輸入用戶名:') pwd = input('請輸入密碼:') # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, # mysql默認端口3306 user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標 cursor = conn.cursor() # sql語句 sql = "select * from userinfo where username='%s' and password='%s';" % (name, pwd) print(sql) # 執行sql語句 ret = cursor.execute(sql) print(ret) # ret是受影響的記錄數量,若不爲空,則表明查到相應的記錄 # 關閉 cursor.close() conn.close() # 結果 if ret: print('登錄成功') else: print('登陸失敗') 結果: 請輸入用戶名:ming 請輸入密碼:112233 select * from userinfo where username='ming' and password='112233' 1 登錄成功 5、 SQL注入問題 1. 什麼是SQL注入? 用戶輸入的內容有惡意的SQL語句,後端拿到用戶輸入的內容不作檢測直接作字符串拼接,獲得一個和預期不一致的SQL語句 例如:上面例子的代碼徹底不變,可是我這樣輸入: 請輸入用戶名:ming' -- 請輸入密碼:12345 select * from userinfo where username='ming' -- ' and password='12345'; 1 登錄成功 結果是登陸成功了,爲何? 這是由於我在輸入用戶名的時候,輸入的是ming' -- 在sql語句中,--表明註釋的意思,也就是說 select * from userinfo where username='ming' -- ' and password='12345'; 這句sql語句至關於 select * from userinfo where username='ming' 那確定能登陸成功啊,由於我數據庫中就是有這個用戶,至關於找用戶名,有這個用戶就成功,並無去匹配密碼, 這就是惡意注入的問題。 2. 如何解決SQL注入? 對用戶輸入的內容作檢測,有引號怎麼處理,註釋怎麼處理,過程很麻煩,可是, pymysql內置了這種檢測,咱們能夠直接使用,因此咱們不該該本身拼接sql語句,而是讓pymysql幫咱們拼接sql語句。 cursor.execute(sql, [name, pwd]) # 讓pymysql模塊幫咱們拼接sql語句,執行SQL語句 例子: import pymysql # 獲取用戶輸入 name = input('請輸入用戶名:') pwd = input('請輸入密碼:') # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, # mysql默認端口3306 user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標 cursor = conn.cursor() # sql語句:按照pymysql模塊的寫法定義好佔位符,pymysql只有%s一種佔位符 sql = "select * from userinfo where username=%s and password=%s;" print(sql) # 執行sql語句 ret = cursor.execute(sql,[name,pwd]) # 讓pymysql模塊幫咱們拼接sql語句,執行SQL語句 print(ret) # ret是受影響的記錄數量,若不爲空,則表明查到相應的記錄 # 關閉 cursor.close() conn.close() # 結果 if ret: print('登錄成功') else: print('登陸失敗') 結果1: 請輸入用戶名:ming' -- 請輸入密碼:12345 select * from userinfo where username=%s and password=%s; 0 登陸失敗 結果2: 請輸入用戶名:ming 請輸入密碼:112233 select * from userinfo where username=%s and password=%s; 1 登錄成功 6、pymysql的使用 涉及到修改數據庫內容的時候,必定要提交:conn.commit() 1. 增、刪、改 import pymysql # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標 cursor = conn.cursor() # 獲得SQL語句 sql1 = "insert into userinfo(username, password) values (%s,%s);" # 增 sql2 = "delete from userinfo where username=%s;" # 刪 sql3 = "update userinfo set password=%s where username=%s;" # 改 # 使用光標對象執行SQL語句 cursor.execute(sql1, ['sb', '456']) # 讓pymysql模塊幫咱們拼接sql語句,執行SQL語句 # 涉及到修改數據庫內容,必定要提交 conn.commit() cursor.execute(sql2, ['ming']) conn.commit() cursor.execute(sql3, ['123', 'sb']) conn.commit() # 關閉 cursor.close() conn.close() 2. 查 1. 返回的數據類型 1. 默認返回的元組,且每一個元素也是用元組 2. 能夠設置爲返回的是列表,列表中的每一個元素是字典 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 2. 經常使用的方法 1. fetchall() 返回全部查詢到的記錄 2. fetchone() 返回一條查詢到的記錄 3. fetchmany(size) 返回size條查詢到的記錄 4.cursor.scroll(1, mode="absolute") 光標按絕對位置移動 5.cursor.scroll(1, mode="relative") 光標按照相對位置(當前位置)移動 例子1: import pymysql # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標 cursor = conn.cursor() # 獲得SQL語句 sql = "select * from userinfo;" # 使用光標對象執行SQL語句 cursor.execute(sql) # 沒有參數則不用拼接,直接執行 ret = cursor.fetchall() print(ret) # 關閉 cursor.close() conn.close() 結果1: (('hong', 123), ('sb', 123), ('ming', 456), ('dong', 789)) 例子2: import pymysql # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標,設置查詢結果爲列表且元素爲字典 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 獲得SQL語句 sql = "select * from userinfo;" # 使用光標對象執行SQL語句 cursor.execute(sql) # 沒有參數則不用拼接,直接執行 # 查詢全部 # ret = cursor.fetchall() # print(ret) # 查詢單條記錄 # ret = cursor.fetchone() # print(ret) # ret = cursor.fetchone() # print(ret) # 查詢指定數量的數據 ret = cursor.fetchmany(2) print(ret) # [{'username': 'hong', 'password': 123}, {'username': 'sb', 'password': 123}] print(cursor.fetchone()) # {'username': 'ming', 'password': 456} print(cursor.fetchone()) # {'username': 'dong', 'password': 789} # 移動光標 cursor.scroll(-1, mode='relative') # 相對位置,基於光標當前位置移動 print(cursor.fetchone()) # {'username': 'dong', 'password': 789} cursor.scroll(0, mode='absolute') # 絕對位置,你讓光標移動到哪裏就到哪裏 print(cursor.fetchone()) # {'username': 'hong', 'password': 123} # 關閉 cursor.close() conn.close() 3.批量增(插入) import pymysql # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123abc', database='test', charset='utf8' ) # 獲取光標 cursor = conn.cursor() # 獲得SQL語句 sql = "insert into userinfo(username, password) values (%s,%s);" # 增 # 數據 data = [("a", 18), ("b", 19), ("c", 20)] # 使用光標對象執行SQL語句 cursor.executemany(sql, data) # 批量增使用executemany # 涉及到修改數據庫內容,必定要提交 conn.commit() # 關閉 cursor.close() conn.close() 5、 conn.rollback():在執行增刪改操做時,若是不想提交前面的操做,可使用 rollback() 回滾取消操做。 cursor.lastrowid:獲取插入數據的ID(關聯操做時會用到) # 導入pymysql模塊 import pymysql # 鏈接數據庫,獲得一個鏈接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123abc', database='test', charset='utf8' ) # 獲得一個能夠執行SQL語句的光標對象 cursor = conn.cursor() # sql語句 sql = "insert into userinfo(username, password) values (%s,%s);" try: # 執行SQL語句 cursor.execute(sql, ['xiazi', 58]) # 提交 conn.commit() # 提交以後,獲取剛插入的數據的ID last_id = cursor.lastrowid except Exception as e: # 有異常,回滾取消操做 conn.rollback() cursor.close() conn.close()