Python中pymysql第三方庫的基本操作

2021-10-12 13:51:52 字數 1022 閱讀 4740

'''

**資料庫基本操作:(studengts為資料庫名稱,score為表名,";"此處不是分隔符要輸入)**

1.建立資料庫:creat database students;

2.使用/開啟資料庫:use students;

3.在資料庫中建立表:creat table score;

5.向表中新增元素:insert into score values("字串型別加引號", , ,);

6.在表中篩選\查詢資訊:select * from score (*為篩選全部內容,也可定義單一引數);

**pymysql模組使用:**

1.安裝第三方庫pymysql:pip install pymysql

2.import pymysql as ps

4.用curcor建立游標:cur = con.curcor()

5.pymysql操作:

1.插入語句:sql1 = "insert into score values("字串型別加引號", , ,)"

2.查詢語句:sql2 = "select * from score where 《判斷條件》"

3.更新語句:sql3 = "update score set 《需更新的內容》 where 《更新條件》"

4.刪除語句:sql4 = "delete * from score (*為刪除全部內容,也可定義單一引數)"

5.迴圈語句:

1.查詢一行:for row in cur:

print(cur.rownumber)

2.查詢字段:for ele in row:

print(ele)

6.用execute執行:cur.execute(需執行的語句1,2,,3)

7.提交資料庫執行:con.commit()

8.撤銷資料執行:con.rollback()

9.關閉游標:cur.close()

10.關閉資料庫:con.close()

'''

Python中pymysql通過字典插入資料

今天參加面試被大佬要求寫一段資料庫介面實操 首先在寫字板上開頭 def insert table,data 這裡你知道table和data,其中data是乙個字典,寫插入資料庫的 想了一下,想到的一些方法被否定了,後來被允許查資料後找到方法一 參考 和 import pymysql def inse...

Python使用pymysql鏈結mysql資料庫

先安裝pymysql如下圖 author pythontab.com 可有可無 匯入pymysql的包 import pymysql try 獲取乙個資料庫連線,注意如果是utf 8型別的,需要制定資料庫 conn pymysql.connect host localhost user root p...

python 常用模組之 pymysql

demo 樣例,暫時先寫查詢的,增刪改後續再新增 無論是增刪改查,都需要先建立資料庫連線,建立游標 import pymysql 建立乙個mysql的conn,返回connetion物件 conn pymysql.connect host localhost user root passwd p s...