gpt4 book ai didi

Mysql与python的连接

转载 作者:行者123 更新时间:2023-11-29 15:36:14 25 4
gpt4 key购买 nike

此代码运行没有错误,但没有记录添加到数据库

mydb=sql.connect(host="localhost",user="root",passwd="")
cmd=mydb.cursor()
cmd.execute("create database if not exists library")
cmd.execute("use library")
cmd.execute("create table if not exists class_12 (roll_no int(2) not null,name varchar(30) not null,book_issued varchar(50),book_no int(6) not null)")
c=input("do u want to edd entries in the book record? y/n : ")
while c=="y":
print("please supply the following details ")
r=int(input("roll number of the student"))
n=str(input("enter the name of the student"))
bn=str(input("enter the book name"))
BN=int(input("Enter BOOK number : "))
inp=("insert into class_12(roll_no,name,book_issued,book_no) values(%s,%s,%s,%s)")
val=(r,n,bn,BN)
cmd.execute(inp,val)
cmd.execute("commit")
c=input("do u want to edd entries in the book record? y/n : ")```

最佳答案

您需要在所有插入语句之后执行cmd.commit()

参见Inserting Data Using Connector/Python

关于Mysql与python的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com