1

I'm trying to insert data into my database and I get a MYSQL syntax error using this code:

import MySQLdb
db=MySQLdb.connect(host="localhost",user="root",passwd="",db="database")
cursor = db.cursor()
sql = "INSERT INTO table1('col1','col2') values ('val1','val2');"
cursor.execute(sql)
db.commit()
1
  • What was the syntax error that you got? Edit your question. Commented Apr 19, 2012 at 22:23

1 Answer 1

2

No quotes around the column names.

INSERT INTO table1(col1, col2) VALUES ('val1', 'val2');

You could use backticks around the column names, but not single quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean, can you paste the code that you're referring to? I see quotes around them.
@NoahR: he means "quotes not allowed around column names"

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.