Python: Insert into a postGreSQL table using python by passing parameters
<p>I have two lists:</p>

<pre><code>columns = ['id','name','address']
data = ['1,'John','LA'','2,'Jessica','TX'']
</code></pre>

<p>I have a table schema with the same columns as above and I am unable to insert data into the table using psycopg2 connections when I pass the values and column names as parameters.</p>

<pre><code>cursor_r.execute("insert into test %s values %s ",tuple(column_names),tuple(values)))
</code></pre>

<p>I get the error as </p>

<pre><code>psycopg2.ProgrammingError: syntax error at or near "'id'"
</code></pre>

<p>I really am unable to figure what I'm doing wrong and would really appreciate any kind of help. Thank you in advance for trying to help me out.
EDIT: I tried typing in this query:</p>

<pre><code>cursor_r.execute("insert into test (%s) values (%s) ", ((x for x in column_names),(str(y).replace("(","").replace(")","") for y in values)))
</code></pre>

<p>It now gives me the error that it cannot adapt to a type 'generator'</p>

<p>EDIT 2: I'm now getting the error that 'generator' object doesn't support indexing. Someone help me out</p>

<p><code>INSERT</code> needs columns and values in <code>()</code> so you need <code>INSERT INTO test (%s) VALUES (%s)</code></p>

<p>see: <a href="http://www.w3schools.com/sql/sql_insert.asp" rel="nofollow">http://www.w3schools.com/sql/sql_insert.asp</a></p>

python sql insert psycopg2