0

I am attempting to load csv files into a table in a database using python.

csv_data = csv.reader(file('ppi.csv'))

for row in csv_data:
    cursor.execute('INSERT INTO ADKnowledgeBase.PPI(a,b) VALUES("row[0]","row[1]");')

Whenever I run this, I get a warning stating Incorrect Integer Value and inserts 0's into my table.

When I print row I get [int0,int1] and they are ints so it's pushing in the ints and not null values.

I've attempted to remove the quotes around "row[0]" and "row[1] and instead use VALUES(row[0],row[1]); but that just gives me a syntax error. I've also attempted using "%d" and %d

My table was made using:

CREATE TABLE IF NOT EXISTS ADKnowledgeBase.PPI(a int, b int);
6
  • "row[0]" is not any integer I've heard of. (Hint: Use placeholder values) Commented Apr 2, 2018 at 23:13
  • Possible duplicate of How to use variables in SQL statement in Python? Commented Apr 2, 2018 at 23:15
  • @tadman I tried using %s and %d but i encounter the same issue Commented Apr 2, 2018 at 23:15
  • @mcv Yes cursor.execute('INSERT INTO ADKnowledgeBase.PPI(a,b) VALUES("%d","%d");',(row[0],row[1])) and my error is something like Warning: Incorrect integer value: ''19217'' for column 'a' at row 1 I have also tried changing the datatype to a varchar in my table and instead of %d to do %s and I still have the issue Commented Apr 2, 2018 at 23:31
  • @mcv yes it does at the end before the cursor.close Commented Apr 2, 2018 at 23:46

1 Answer 1

1

So python mysql format query string always uses %s

cursor.execute("INSERT INTO ADKnowledgeBase.PPI(a,b) VALUES(%s,%s)", (row[0],row[1]))
Sign up to request clarification or add additional context in comments.

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.