-1

I am trying to enter data into MySQL table using MySQLdb but it is not inserting data into table. my code is :

      try:
            db = MySQLdb.Connect("127.0.0.1","root","root","bank")
            cursor = db.cursor()
            cursor.execute("SELECT max(account_number) from pybank")
            results = cursor.fetchall()
            for row in results:
                self.account_number = row[0]
            sql = "INSERT INTO pybank(account_number,user_name, user_age, user_dob, amount) VALUES(%d,'%s',%d,'%s',%d)" %(self.account_number + 1,self.user_name, self.user_age, self.user_dob, self.total_amount)
            print(sql)

            cr = db.cursor()
            cr.execute(sql)
            db.commit()
            print("Your have successfully created your account")
            self.getAccountDetails()
        except:
            db.rollback()
            print("Your account is not created !!!! ")
            print
            print("Please try again")
dinesh.kumar
  • 167
  • 1
  • 10

1 Answers1

2

You have mistype in SQL near VALUES, also you should use placeholders:

cursor = db.cursor()
cursor.execute("INSERT INTO pybank (account_number,user_name, user_age, user_dob, amount) VALUES (%s, %s, %s, %s, %s)", (self.account_number + 1, self.user_name, self.user_age, self.user_dob, self.total_amount))
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43