Python PyMySQL模組讀寫MySQL資料

2021-08-15 08:44:29 字數 1306 閱讀 5930

安裝

pip install pymysql
pymysql 操作和mysqldb 類似,可參考

python程式設計:mysqldb模組對資料庫的基本增刪改查操作

import pymysql  

# 連線

conn = pymysql.connect(host=

"127.0.0.1"

, port=

3306

, user=

"root"

, passwd=

"123456"

,db=

"test"

)# 建立游標

cursor = conn.cursor(

)# 執行sql,並返回受影響行數

# rows = cursor.execute("insert into student(name, age, register_date, gender) "

# "values ('xiaoming', 23, '2018-12-30', 'm')")

# print(rows)

data =[(

"student1",23

,"2018-01-31"

,"m"),

("student2",24

,"2018-02-27"

,"m"),

("student3",28

,"2018-03-31"

,"f"),

("student4",26

,"2018-04-30"

,"m"),

]# 多條插入一起執行

# rows = cursor.executemany("insert into student(name, age, register_date, gender) "

# "values (%s, %s, %s, %s)",data)

# print(rows)

# 讀取資料

cursor.execute(

"select * from student"

)print

(cursor.fetchall())

# 提交事務

conn.commit(

)# 關閉游標

cursor.close(

)# 關閉連線

conn.close(

)

Python Pymysql模組基本使用

5.防止sql注入 使用pip工具安裝 window下 cmd pip install pymysql linux下 terminal sudo pip3 install pymysql import pymysql 建立mysql資料庫連線 conn pymysql.connext 引數 host...

python pymysql基礎使用

方法 conn.基本使用 import pymysql conn pymysql.connect host 你的資料庫位址 user 使用者名稱 password 密碼 database 資料庫名 charset utf8 cursor conn.cursor sql create table us...

Python,pymysql簡單使用。

基本操作 1 匯入pymysql import pymysql 2 連線資料庫 conn pymysql.connect host localhost user root passwd root db ere charset utf8 務必注意各等號前面的內容!charset引數可避免中文亂碼 3 ...