1

I am not able to enter a record in mysql database.

import mysql.connector

mydb = mysql.connector.connect(
    host = "localhost",
    user = "root",
    password = "root@123",
    database = "testdb",
    )

print(mydb)

my_cursor = mydb.cursor()
sql_query = ("INSERT INTO users (name,email,age) VALUES (%s, %s, %s)")
record1 = ('Tim','Tim123',45)
records = [("Ankit","ankit345",30),
    ("shubhra","shubhra567",29),
    ("anita","anit789",55),]
my_cursor.execute(sql_query,records)
mydb.commit()



mydb = mysql.connector.connect(
    host = "localhost",
    user = "root",
    password = "root@123",
    database = "testdb",
    )

print(mydb)

my_cursor = mydb.cursor()
sql_query = ("INSERT INTO users (name,email,age) VALUES (%s %s %s)")
record1 = ('Tim','Tim123',45)
my_cursor.execute(sql_query,record1)
mydb.commit()

Administrator@DESKTOP-60U78C6 MINGW64 /c/mysql $ python db1.py Traceback (most recent call last): File "db1.py", line 15, in my_cursor.execute(sql_query,record1) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 569, in execute self._handle_result(self._connection.cmd_query(stmt)) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 553, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 442, in _handle_result raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '45)' at line 1

1
  • Looks like you need commas between the three %s Commented Jun 30, 2019 at 20:12

1 Answer 1

1

You missing commas between the values:

sql_query = ("INSERT INTO users (name,email,age) VALUES (%s, %s, %s)")
# Here ----------------------------------------------------^---^
Sign up to request clarification or add additional context in comments.

1 Comment

I have added code for multiple records. It is not working. What is the error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.