【发布时间】:2017-09-17 09:32:15
【问题描述】:
我正在尝试将单个值插入到具有两列的 Postgres 表中。第一列是一个自动递增的主键,而第二列应该是一个变量。我做了一些研究,发现了以下资源TypeError: not all arguments converted during string formatting python,How to set auto increment primary key in PostgreSQL? 这个Psycopg2 string formatting with variable names for type creation。
import psycopg2
try:
conn = psycopg2.connect("dbname=postgres user=postgres password=actuarial")
print("database created successfully.")
except psycopg2.OperationalError as ex:
print("Connection failed: {0}".format(ex))
cur = conn.cursor()
def create_client_table():
cur.execute("CREATE TABLE ClientTable (ClientID SERIAL PRIMARY KEY, Name varchar);")
print('student table created successfully')
create_client_table()
def client_add():
name = input("Enter the names of the student:")
cur.execute("INSERT INTO CleintTable VALUES (%s)", (name));
cur.execute("SELECT * FROM StudentTable")
rows = cur.fetchall()
print(rows)
client_add()
conn.close()
运行上面的代码时,我收到以下错误。请帮我确定我哪里出错了。
cur.execute("INSERT INTO CleintTable VALUES (%s)", (name));
TypeError: not all arguments converted during string formatting
【问题讨论】:
标签: postgresql python-3.x psycopg2