Inserting data to table psycopg2 with no duplicates
<p>I am new to psycopg2. I have to insert data into the table with no duplicates. So, first I created a temporary table where I dumped all the data. And then, I check and add the data to the actual table.
Here is the code till now:</p>

<pre><code>for eachline in content:
       pmid ,first_name, last_name,initial,article_title,journal,language = eachline.split("\t")
       cur.execute ("INSERT INTO AUTHOR_PMID(pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
                           SELECT DISTINCT (pmid, Author_lastname, Author_firstname, Author_initial,Article_title) 
                                 FROM AUTHOR_PMID WHERE NOT EXISTS (SELECT "X" FROM AUTHOR_pmid_temp 
                                     WHERE 
                                        AUTHOR_pmid_temp.pmid = AUTHOR_PMID.pmid
                                        AND AUTHOR_pmid_temp.Author_lastname = AUTHOR_PMID.Author_lastname 
                                        AND AUTHOR_pmid_temp.Author_firstname = AUTHOR_PMID.Author_firstname 
                                        AND AUTHOR_pmid_temp.Author_initial = AUTHOR_PMID.Author_initial 
                                        AND AUTHOR_pmid_temp.Article_title = AUTHOR_PMID.Article_title);")
   con.commit()


error: syntax error.
</code></pre>

<p>Where am i going wrong?</p>

<p>Try inserting query with triple quotes instead of single like below</p>

<pre><code>for eachline in content:
       pmid ,first_name, last_name,initial,article_title,journal,language = eachline.split("\t")
       cur.execute ("""INSERT INTO AUTHOR_PMID(pmid, Author_lastname, Author_firstname, Author_initial,Article_title)
                           SELECT DISTINCT (pmid, Author_lastname, Author_firstname, Author_initial,Article_title) 
                                 FROM AUTHOR_PMID WHERE NOT EXISTS (SELECT "X" FROM AUTHOR_pmid_temp 
                                     WHERE 
                                        AUTHOR_pmid_temp.pmid = AUTHOR_PMID.pmid
                                        AND AUTHOR_pmid_temp.Author_lastname = AUTHOR_PMID.Author_lastname 
                                        AND AUTHOR_pmid_temp.Author_firstname = AUTHOR_PMID.Author_firstname 
                                        AND AUTHOR_pmid_temp.Author_initial = AUTHOR_PMID.Author_initial 
                                        AND AUTHOR_pmid_temp.Article_title = AUTHOR_PMID.Article_title);""")
   con.commit()
</code></pre>

<p>For more info, please <a href="http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries" rel="nofollow">check here</a> !!!</p>

python psycopg2