Python SQL Insert script

I’m creating a Python script to insert some records in a table, but have the following problem:

 INSERT INTO  orders VALUES (7656940929251, "ADIDAS | KID'S STAN SMITH")
,(242345235233, 'ADIDAS | CLASSIC BACKPACK')

I get the error: “Invalid column name ‘ADIDAS | KID’S STAN SMITH’

How can I fix this with Python?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Let’s say this is your tuple:

tuples_v3s = ((7656941224163, 'ADIDAS | CLASSIC BACKPACK'),
             (7656941256931, 'ADIDAS | CLASSIC BACKPACK | LEGEND INK MULTICOLOUR'),
             (7656940929251, "ADIDAS | KID'S STAN SMITH"))
for tuples_v3 in tuples_v3s:
  print(f"""INSERT INTO  orders VALUES ({tuples_v3[0]},'{tuples_v3[1].replace("'","''")}' ) """)

#INSERT INTO  orders VALUES (7656941224163,'ADIDAS | CLASSIC BACKPACK' ) 
#INSERT INTO  orders VALUES (7656941256931,'ADIDAS | CLASSIC BACKPACK | LEGEND INK MULTICOLOUR' ) 
#INSERT INTO  orders VALUES (7656940929251,'ADIDAS | KID''S STAN SMITH' )

Method 2

If you are using psycopg2, Please update your query as below

insert_query = "INSERT INTO products VALUES (7656941224163, %s), 
                        (7656941256931, %s),
                        (7656940929251, %s)"
cur.execute(insert_query, ("ADIDAS | CLASSIC BACKPACK", "ADIDAS | CLASSIC BACKPACK | LEGEND INK MULTICOLOUR", "ADIDAS | KID'S STAN SMITH"))


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x