0

this code runs without error but no records are added to the database

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 : ")```
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52

2 Answers2

0

You are not committing the changes to the database. Add the commit & close calls to the script before exiting.

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)
    c=input("do u want to edd entries in the book record? y/n : ")
cmd.commit()
cmd.close()
rdas
  • 20,604
  • 6
  • 33
  • 46
0

You need to execute cmd.commit() after all insert statements.

See Inserting Data Using Connector/Python

showdev
  • 28,454
  • 37
  • 55
  • 73
iii
  • 102
  • 4