pymysql模組學習

2022-08-22 02:42:09 字數 2099 閱讀 4817

data_ip = "192.168.34.128"

data_name = "lch"

data_pwd = "lch123"

database = "day35_work"

import pymysql #匯入模組

db = pymysql.connect(data_ip,data_name,data_pwd,database) #開啟資料庫連線

cursor = db.cursor() #建立乙個游標物件

create_book_sql =

"""create table book02(name char(16),owner char(16),press char(16),price int(5),date date)

"""cursor.execute(create_book_sql) #執行create_book_sql

import pymysql

db = pymysql.connect(data_ip,data_name,data_pwd,database) #開啟資料庫連線

cursor = db.cursor() #建立乙個游標物件

insert_book02_sql = """

insert into book02 values

("學python從開始到放棄","alex","人民大學出版社",50,20180701),

("學mysql從開始到放棄 ","egon","北京工業地雷出版社",60,20180603),

("學html從開始到放棄","alex","機械工業出版社",20,20180401),

("學css從開始到放棄","wusir","機械工業出版社",120,20180502),

("學js從開始到放棄","wusir","機械工業出版社",100,20180730)

"""try:

cursor.execute(insert_book02_sql) #執行語句

db.commit() #事物號提交

except:

db.rollback() #若報錯,事物號回滾

db.close() ##關閉資料庫連線

import pymysql

db = pymysql.connect(data_ip,data_name,data_pwd,database)

cousor = db.cursor()

delete_sql = "delete from book02 where price = 20"

try:

cousor.execute(delete_sql)

db.commit()

except:

db.rollback()

db.close()

import pymysql

db = pymysql.connect(data_ip,data_name,data_pwd,database)

cousor = db.cursor()

update_sql = "update book02 set price = 500 where price = 50"

try:

cousor.execute(update_sql)

db.commit()

except:

db.rollback()

db.close()

import pymysql

db = pymysql.connect(data_ip,data_name,data_pwd,database)

cursor = db.cursor()

sleetc_sql = "select * from book02"

try:

cursor.execute(sleetc_sql)

data = cursor.fetchall() #獲取所有資料

for i in data:

print(i)

except:

print("error")

db.close()

pymysql模組用法

pymsql是python中操作mysql的模組,其使用方法和py2的mysqldb幾乎相同。1 pip install pymysql import pymysql 新增資料 conn pymysql.connect host 127.0.0.1 port 3306,user root passw...

pymysql 模組介紹

pymysql模組是python與mysql進行互動的乙個模組。pymysql模組的安裝 pymysql模組的用法 import pymysql user input user strip pwd input pwd strip conn pymysql.connect host localhost...

PyMySQL模組的使用

pip3 install pymysqlimport pymysql 建立連線 conn pymysql.connect host 127.0.0.1 port 3306,user root password 123456 db db5 charset utf8 user input user pw...