日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

lr mysql 增删改查_Python对MySQL进行增删查改

發布時間:2025/4/16 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lr mysql 增删改查_Python对MySQL进行增删查改 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#增刪查改

from Practice_Recode.UserTest.User importUserimportpymysqldefopenDb():globaldb, cursor

db= pymysql.connect("localhost", "root", "test1234", "pythontest", charset='utf8')

cursor=db.cursor()defcloseDb():

db.close()#按照用戶id查詢用戶記錄(輸出相應內容,并返回查到的user對象)

defserarchDb(id):

openDb()

sql= "select * from user where id =" +str(id)

rst=cursor.execute(sql)if rst ==0:#print("查找失敗")

returnNoneelse:#print("查找成功")

data =cursor.fetchone()#print(data)

user1 = User(data[0], data[1], data[2], int(data[3]), data[4], data[5])returnuser1

closeDb()#按照用戶id刪除用戶記錄

defdeleteDb(id):

openDb()

sql= "delete from user where id =" +str(id)

rst=cursor.execute(sql)if rst ==0:print("刪除失敗")else:print("刪除成功")

closeDb()#新增用戶

definsertDb(user1):

openDb()

sql= "insert into user values('%d','%s','%s','%d','%s','%s')" %(

user1.id, user1.username, user1.birth_data, user1.money, user1.father, user1.mother)#"INSERT INTO mytb(title,keywd) VALUES('%s','%s')"%(x,y)

cursor.execute(sql)

db.commit()

closeDb()#更新用戶信息

defupdateDb(user1):

openDb()

sql= "update user set username = '%s', money='%d' where id='%d'" %(user1.username, user1.money, user1.id)#update user set username='C', money=9999 where id=5;

rst =cursor.execute(sql)if rst ==0:print("更新失敗")else:print("更新成功")

closeDb()#測試數據#testuser = serarchDb(5)#testuser.set_username('C')#testuser.set_money(9082)## print(testuser.id, testuser.username, testuser.money, testuser.father, testuser.mother)#updateDb(testuser)

#user1 = User(5, "c", "1111-03-11", 10000, father='A', mother='a')#insertDb(user1)#user2 = User(0, "d", "1111-03-11", 10000, 'A', 'a') # 自增鍵id設置為0,新增時即可實現自增#insertDb(user2)

#user2 = User(1, "A", "1111-03-11", 10000, father=None, mother=None)#user3 = User(2, "a", "1111-03-11", 10000, father=None, mother=None)#user1 = User(3, "B", "1111-03-11", 10000, user2, user3)#user1.dayin()#user1.father.dayin()

總結

以上是生活随笔為你收集整理的lr mysql 增删改查_Python对MySQL进行增删查改的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。