【Python3】pymysql模塊

【Python3】pymysql模塊

1. 什麼是 PyMySQL?

PyMySQL 是在 Python3.x 版本中用於鏈接 MySQL 服務器的一個庫,Python2中則使用mysqldb。python

PyMySQL 遵循 Python 數據庫 API v2.0 規範,幷包含了 pure-Python MySQL 客戶端庫。mysql

2. PyMySQL 安裝

pip3 install PyMySQL

3. 使用操做

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
  
# 建立鏈接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
# 建立遊標
cursor = conn.cursor()
  
# 執行SQL,並返回收影響行數
effect_row = cursor.execute("update hosts set host = '1.1.1.2'")
  
# 執行SQL,並返回受影響行數
#effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))
  
# 執行SQL,並返回受影響行數
#effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
  
  
# 提交,否則沒法保存新建或者修改的數據
conn.commit()
  
# 關閉遊標
cursor.close()
# 關閉鏈接
conn.close()

注:當使用字符串格式化時容易引發sql注入sql

sql = 'select * from userinfo where username="%s" and password="%s" ' %(user,pwd,)      #不推薦使用
sql = 'select * from userinfo where username=%s and password=%s',[user,pwd]    #推薦使用,mysql會自動格式化,避免SQL注入
import pymysql

user = input('請輸入用戶名:')
pwd = input('請輸入密碼:')

# 獲取數據
conn = pymysql.Connect(host='192.168.12.89',port=3306,user='root',password="123",database="s17day11db",charset='utf8')
cursor = conn.cursor()

v = cursor.execute('select * from userinfo where username=%s and password=%s',[user,pwd])
result = cursor.fetchone()
cursor.close()
conn.close()

print(result)
  • 獲取新建立數據自增ID數據庫

new_class_id = cursor.lastrowid # 獲取新增數據自增ID
複製代碼
import pymysql

# 獲取數據
conn = pymysql.Connect(host='192.168.12.89',port=3306,user='root',password="123",database="s17day11db",charset='utf8')
cursor = conn.cursor()

cursor.execute('insert into class(caption) values(%s)',['新班級'])
conn.commit()
new_class_id = cursor.lastrowid # 獲取新增數據自增ID

cursor.execute('insert into student(sname,gender,class_id) values(%s,%s,%s)',['李傑','男',new_class_id])
conn.commit()

cursor.close()
conn.close()
  • 查詢語句服務器

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
  
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor()
cursor.execute("select * from hosts")
  
# 獲取第一行數據
row_1 = cursor.fetchone()
  
# 獲取前n行數據
# row_2 = cursor.fetchmany(3)
# 獲取全部數據
# row_3 = cursor.fetchall()
  
conn.commit()
cursor.close()
conn.close()

注:
在fetch數據時按照順序進行,能夠使用cursor.scroll(num,mode)來移動遊標位置,如:fetch

  • cursor.scroll(1,mode='relative') # 相對當前位置移動code

  • cursor.scroll(2,mode='absolute') # 相對絕對位置移動ip

  • 更改fetch數據類型
    默認獲取的數據是元祖類型,若是想要或者字典類型的數據utf-8

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)字符串

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
  
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
  
# 遊標設置爲字典類型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
v = cursor.execute('SELECT * FROM score')
result = cursor.fetchall()
# result = cursor.fetchone()
# result = cursor.fetchmany()
print(result)



cursor.close()
conn.close()
相關文章
相關標籤/搜索