我正在使用下面的方法try 将一条记录插入postgresql数据库表,但它不起作用.我没有发现任何错误,但表中没有记录.我需要promise 吗?我使用的是与Bitnami djangostack安装一起安装的postgresql数据库.

import psycopg2

try:
    conn = psycopg2.connect("dbname='djangostack' user='bitnami' host='localhost' password='password'")
except:
    print "Cannot connect to db"

cur = conn.cursor()

try:
    cur.execute("""insert into cnet values ('r', 's', 'e', 'c', 'w', 's', 'i', 'd', 't')""")
except:
    print "Cannot insert"

推荐答案

如果不想将每个条目提交到数据库,可以添加以下行:

conn.autocommit = True

因此,生成的代码将是:

import psycopg2

try:
    conn = psycopg2.connect("dbname='djangostack' user='bitnami' host='localhost' password='password'")
    conn.autocommit = True
except:
    print "Cannot connect to db"

cur = conn.cursor()

try:
    cur.execute("""insert into cnet values ('r', 's', 'e', 'c', 'w', 's', 'i', 'd', 't')""")
except:
    print "Cannot insert"

Postgresql相关问答推荐

哪种数据类型是除法的结果?

如何在Postgres中对分区表使用Hibernate验证?

PostgreSQL存储过程中不存在游标

如何在postgres中测试锁定

使用 select 在带有特殊字符的字符串中查找数据

PostgreSQL - 列出模式中的所有唯一约束

Regexp_replace 行为会改变,如果它用空白字符串或 null 替换字符串数组中的字符串

用于生产的 Rails 性能调优?

如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点?

PostgreSQL - jsonb_each

PostgreSQL ORDER BY 问题 - 自然排序

'active' 标志与否?

为什么 PostgreSQL 数组访问在 C 中比在 PL/pgSQL 中快得多?

错误:CASE types character varying and numeric cannot be matched

我不明白 postgresql 的 nextval() 是如何工作的,有人可以解释一下吗?

如何在 PostgreSQL 中插入多行

设置 Phoenix 框架和 Ecto 以使用 UUID:如何插入生成的值?

获取 psycopg2 count(*) 结果数

PG::ConnectionBad FATAL:role "Myname" does not exist

GRANT SELECT 特权使用一个语句对所有序列