import ksycopg2
database = "TEST"
user = "SYSTEM"
password = "123456"
host = "127.0.0.1"
port = "54321"
if __name__ == "__main__":
try:
conn = ksycopg2.connect("dbname={} user={} password={} host={} port={}"
.format(database, user, password, host, port))
cur = conn.cursor()
cur.execute('drop table if exists test_ksy')
cur.execute('create table test_ksy(id integer, name TEXT)')
cur.execute("insert into test_ksy values(%s, %s)", (1, "John"))
cur.execute("insert into test_ksy values(%s, %s)", (2, '中文测试文字'))
cur.execute("insert into test_ksy values(%s, %s)", (3, '!@#¥%……'))
conn.commit()
cur.execute("select * from test_ksy")
rows = cur.fetchall()
for row in rows:
print(cell)
cur.close()
conn.commit()
conn.close()
except Exception, e:
print(e)
print("Fail")
else:
print("Success")