使用psycopg2在Postgres数据库中插入

我正在使用psycopg2将数据插入Postgresql数据库表中 库,但是插入命令出现错误。谁能帮我解决这个问题?

def t_b():  
    table = 'create table {} (Title serial,Link char(50),logo_link 
    char(50),Description Text)'.format(name)  
    cur.execute(table)  
    print("Table created :",name)  

def insertion(name,data):  
    data_s = ",".join(data)
    ins = 'insert into {} (Title,Link,logo_link,Description)values({})'.format(name,data_s)  
    cur.execute(ins)  
    conn.commit()  

def main():  
    t_b("ONE")  
    dd = ('abc','123','INsyustriesz','<htt:ps>' )  
    insertion("ONE",dd)  

if __name__ == '__main__':  
    main()
    cur.execute()  

并显示错误消息:

psycopg2.errors.SyntaxError: syntax error at or near ":"  
LINE 1: ... logo_link,Description) values(abc,123,INsyustriesz,htt:ps)

并且未插入数据

liangpenghui 回答:使用psycopg2在Postgres数据库中插入

发生错误是因为您没有在查询的values部分中包含单引号。您在dd中使用了单引号,但是单引号是为了帮助Python将其解释为字符串。我不建议通过data_sjoin中的所有内容串在一起,而是实际上在VALUES部分中指定每个元素:

    ins = 'insert into {} (Title,Link,logo_link,Description)values({},{},{})'.format((name + data))  
本文链接:https://www.f2er.com/3157193.html

大家都在问