0

I have the following quesry, where the variables are arrays...

c.execute("INSERT into userData values=(%s,%s,%s,%s,%s,%s)",
            t[i],k[0],k[1],k[2],user[i],total)

This gives a syntax error.

I have also tried this:

a = "INSERT INTO userData VALUES ('"+t[i]+"','"+k[0]+"','"+k[1]+"','"+k[2]+"',
  '"+USER+"','"+total+"')"
c.execute(a)
conn.commit()

This does not update to the database, though there are no errors. Note: c - cursor, conn - connection.

2
  • What kind of syntax error do you get exactly? Commented Feb 26, 2016 at 9:57
  • Try this c.excecute("INSERT into userData values(%s,%s,%s,%s,%s,%s)",( t[i],k[0],k[1],k[2],user[i],total)) it is beacause the second parameter to execute model is tuple of arguments and there is no need for = Commented Feb 26, 2016 at 9:57

2 Answers 2

1

cursor.execute() expects the query parameters as a tuple. Try this:

cursor.execute("INSERT INTO userData VALUES (%s, %s, %s, %s, %s, %s)",
               (t[i], k[0], k[1], k[2], user[i], total))

Don't use + or the like for constructing SQL queries, as this can lead to SQL-injection vulnerabilities in your code.

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

Comments

0

Maybe try with the following syntax:

INSERT INTO table(col1,col2,...)VALUES(val1,val2,...)

Comments

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.