Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:16:57+00:00 2026-05-26T05:16:57+00:00

I am having trouble in formatting the list for insertion using psycopg. Here is

  • 0

I am having trouble in formatting the list for insertion using psycopg.
Here is a sample of code i am trying to do.
Basically I am just reading data from one table and trying to insert it into another table.

Code:

cur.execute("""select data from \"Table1\" where lat=-20.004189 and lon=-63.848004""")
rows = cur.fetchall()
print rows
cur.execute("""INSERT INTO \"%s\" (data) VALUES (ARRAY%s)""" % (args.tableName,rows)))

The result returned by first select query is like this:

[([6193, 3975, 4960, 5286, 3380, 970, 3328, 3173, 2897, 2457, 2443, 2674, 2172, 2740, 3738, 4907, 3691, 4234, 3651, 3215],)]

When I try to insert this into another table I get the following format error.

   cur.execute(cur.mogrify("""INSERT INTO \"%s\" (data) VALUES (%s)""" % (args.tableName,rows)))
psycopg2.ProgrammingError: syntax error at or near "["
LINE 1: INSERT INTO "DUMMY1km" (data) VALUES ([([6193, 3975, 4960, 5...

I tried cur.mogrify, but it does not seem to help.

Please let me know if anyone has a work around for this issue.

Thanks
Adi

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:16:58+00:00Added an answer on May 26, 2026 at 5:16 am

    I don’t think mogrify is needed here. Use executemany and pass rows as the second argument.

    cur.executemany(
        """INSERT INTO "%s" (data) VALUES (%%s)""" % (args.tableName),rows)
    

    Using parametrized arguments helps prevent SQL injection.

    The table name can not be parametrized, so we do have to use string interpolation to place the table name in the SQL query. %%s gets escapes the percent sign and becomes %s after string interpolation.


    By the way, (as a_horse_with_no_name has already pointed out) you can use the INSERT INTO … SELECT form of INSERT to perform both SQL queries as one:

    cur.execute(
        """INSERT INTO %s (data)
           SELECT data FROM Table1
           WHERE lat=-20.004189 AND lon=-63.848004""" % (args.tableName))
    

    Per the question in the comments, if there are multiple fields, then the SQL becomes:

    cur.executemany(
        """INSERT INTO {t} (lat,lon,data1,data2) 
           VALUES (%s,%s,%s,%s)""".format(t=args.tableName),rows)
    

    (If you use the format method, then you don’t have to escape all the other %ss.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble formatting a datetime.timedelta object. Here's what I'm trying to do: I
I just started learning Java and I'm having trouble formatting string. In the problem
I am having trouble on formatting my json. Basically I have this script structure,
I am using the FullCalendar script and am having trouble formatting the date/time of
I am having trouble using srand. i am trying to generate a random number
I'm having trouble trying to export saved data from CoreData to a CSV file.
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Im having trouble using a .NET COM in vb6, It compiles ok and I
I having trouble in dividing the HTML frames. I have been using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.