0

I try to insert some data to my PostgreSQl-DB with Python (psycopg2).

Every works fine with my SELECT-statement like:

cur = g.db.cursor()
cur.execute("""SELECT id FROM mydb where weight>%(weight)s and length<=%(length)s;""",{'weight':weight,'length':length})

Now I try to add some new data, the same way as above. The code is like

cur = g.db.cursor()
cur.execute("""INSERT INTO mydb (id, weight, length) VALUES (%(id)s, %(weight)s, %(length)s, );""",{'id':id,'weight':weight,'length':length})

But I don't see any new rows in pgadmin. Without any error message. I copy also the statement into pgadmin's SQL-Editor and everything works fine.

Has someone some tips to debug, trace down my error?

CanadaRunner
  • 65
  • 10
  • You should first check that a similar question has not yet been asked, either by searching on StackOverflow or on Google. A simple search immediately gave me two StackOverflow answers. – Eric O. Lebigot Apr 18 '15 at 11:37
  • @EOL :Can you add link? – Kishor Pawar Apr 18 '15 at 11:41
  • I search for approx 1.5h, but with too specific keywords. Works after I switched of my customized google search – CanadaRunner Apr 18 '15 at 11:44
  • I guess I got your mistake, you haven't put place holders for your values in VALUES() – Kishor Pawar Apr 18 '15 at 11:47
  • @EOL: Thx works perfect!, but why does select work and insert not? – CanadaRunner Apr 18 '15 at 12:06
  • @CanadaRunner: INSERT does not work because you must commit the changes before they are really made in the database (by definition of a transaction). Now, I guess that SELECT works because it does not cause the database to change, so there is no problem reading it (even inside a transaction). – Eric O. Lebigot Apr 18 '15 at 12:37
  • @KishorPawar The link is displayed in the "duplicate question" box on top. – Eric O. Lebigot Apr 18 '15 at 12:38
  • yes right, But CanadaRunner's problem is different. He needs to use commit as described in that question, but he is missing place holders too. – Kishor Pawar Apr 18 '15 at 12:40
  • @KishorPawar I do agree that placeholders should be used, but that would not solve CanadaRunner's question, which is that he "doesn't see any new rows in pgadmin". PS: If you want people to see your comments, you should refer to them ("@EOL"), otherwise they won't be notified (only the author of the answer or question is notified, by default). – Eric O. Lebigot Apr 20 '15 at 07:07
  • @EOL: oh, thatks for your tip. – Kishor Pawar Apr 20 '15 at 07:12

0 Answers0