0

i want to insert the data into a PostgreSQL database but when i try to insert data is not stored in the database and error is not generated so I am unable to find out what the error is, please help

#!/usr/bin/python

import psycopg2
import psycopg2.extras
import sys


con = None

try:

    con = psycopg2.connect("dbname='mydb' user='merlin' host='192.168.0.104'  password='merlin123'") 
    cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor)

    cursor.execute("SELECT * FROM table1")

    rows = cursor.fetchall()


except psycopg2.DatabaseError, e:
    print 'Error %s' % e    


finally:

    if con:
        con.close()

try:
   con = psycopg2.connect("dbname='mydb2' user='merlin' host='192.168.0.104'") 
   cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
   query = "INSERT INTO table3 VALUES ('%s', '%s', '%d' , %s)" % (raw[0][0], raw[0][1],raw[0][2],0) 
    #while raw!=None:

   cursor.execute(query)
   con.commit()

except psycopg2.DatabaseError, e:
    print 'Error %s' % e    

finally:

    if con:
        con.close()
    sys.exit(1)

Can anyone help solving the error?

4
  • Could you please explain the problem in more detail as well as your code?! Commented Aug 20, 2015 at 10:43
  • @Cleb why you ask question, OP not do any English. Commented Aug 20, 2015 at 10:56
  • 1
    @wswld Hey, lets try to be nice to new users. It's one thing if they're being wilfully unhelpful, but really, I'd think a link to the "how to ask" help page, a close-vote as "unclear what you're asking" and a quick word of advice would be more useful. Additionally, unless you speak at least two languages fluently you should go easy with the language criticism. It's easy if you grow up with it. Commented Aug 20, 2015 at 14:17
  • Actually, English is not my native language, but your point is well taken. Commented Aug 20, 2015 at 14:24

1 Answer 1

1

Your problem not seeing the exception stack trace is because you are doing sys.exit(1) in the finally clause -- so you are exiting before python has a chance to display the error.

I would simplify your script a lot (comment out the unnecessary portions) and then gradually add in the complexity you need if/when you need it.

BTW I think The error will be that raw is not defined

Sign up to request clarification or add additional context in comments.

1 Comment

Actually error because of commit() function ...after creating the connection we need to commit connection so con.commit()..thats ittt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.