#!/usr/bin/python #-*-coding:utf-8-*- import psycopg2 # 数据库连接参数 conn = psycopg2.connect(database="newdb", user="admin", password="admin", host="192.168.0.43", port="5432") cur = conn.cursor() cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num integer,data varchar);") cur.execute("INSERT INTO test(num, data)VALUES(%s, %s)", (1, 'aaa')) cur.execute("INSERT INTO test(num, data)VALUES(%s, %s)", (2, 'bbb')) cur.execute("INSERT INTO test(num, data)VALUES(%s, %s)", (3, 'ccc')) cur.execute("SELECT * FROM test;") rows = cur.fetchall() print(rows) for i in rows: print(i) conn.commit() cur.close() conn.close()