2

I have this list I am trying to insert into my database. I have tried many other methods I found on Google or here but none seems to work.

results = ['match number 1','match number 2','match number 3']
query = "INSERT INTO round1 (details) VALUES ("
query = query+"'"+results+"')"
x = conn.cursor()
x.execute(query)
conn.commit()

I keep getting this error

Type Error: cannot concatenate 'str' and 'list' objects

Can anyone tell me what I am doing wrong?

2
  • Are you inserting results as one column, as multiple columns or as multiple rows? Your query suggests you are inserting just one column, and I am guessing you want multiple rows. Commented Dec 9, 2013 at 17:46
  • yes in one column as multiple rows Commented Dec 9, 2013 at 17:55

2 Answers 2

5

Don't use concatenation, use SQL parameters:

query = "INSERT INTO round1 (details) VALUES (%s)"
c = conn.cursor()
c.executemany(query, [(r,) for r in results])

Your code tried to concatenate a whole list with a string; you can only concatenate strings to strings. But to insert multiple rows for each value, you need to run an INSERT statement per entry in results.

Using SQL parameters here has several advantages:

  • You leave quoting the value up to the database, avoiding SQL injection attacks.
  • The database can reuse the query plan for the SQL statement, optimizing database performance.
  • You can use cursor.executemany() to run the same query with different values, all with one call.

The cursor.executemany() call here takes each 'row' found in the second argument, taking the values from each row to run a separate INSERT statement.

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

4 Comments

only started learning python about 2 weeks ago, i am getting this when i used your code "ProgrammingError: (1064, "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 '%s)' at line 1")"
What database adapter are you using here? I was assuming MySQLdb, which uses %s as parameters syntax, but different adapters have different styles. See modulename.paramstyle and the documentation.
it is mysqldb from lxml import html import requests import MySQLdb import itertools
Then doublecheck your syntax; as posted in my answer the query would work.
0

i keep getting this error Type Error: cannot concatenate 'str' and 'list' objects can anyone tell me what i am doing wrong ?

First of all, what you're doing wrong is trying to concatenate str and list objects. You can't do that: you can only concatenate a string with a string.

Secondly, use prepared statements, instead of string manipulation to communicate with your database. It will be less code for you, and more secure.

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.