(請依OS與PY版本自行選擇)
#! /usr/bin/env python
import psycopg2
try:
conn = psycopg2.connect(host="127.0.0.1", port="5432", database="dbName", user="userName", password="userPwd")
cur = conn.cursor()
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, name varchar);")
cur.execute("INSERT INTO test (name) VALUES (%s)",("Lyon Lin"))
cur.execute("SELECT * FROM test;")
cur.fetchone()
conn.commit()
print "OK"
except:
print "I am unable to connect to the database"
finally:
cur.close()
conn.close()
沒有留言:
張貼留言