小编pir*_*iro的帖子

psycopg2"TypeError:在字符串格式化期间不是所有参数都被转换"

我正在尝试将二进制数据(漩涡哈希)插入到PG表中并收到错误:

TypeError: not all arguments converted during string formatting 
Run Code Online (Sandbox Code Playgroud)

码:

cur.execute("""
    INSERT INTO
        sessions
        (identity_hash, posted_on)
    VALUES
        (%s, NOW())
""", identity_hash) 
Run Code Online (Sandbox Code Playgroud)

我尝试在插入之前将conn.Binary("identity_hash")添加到变量,但得到相同的错误.

identity_hash列是bytea.

有任何想法吗?

python postgresql psycopg2

10
推荐指数
3
解决办法
1万
查看次数

Psycopg2报告pg_hba.conf错误

尝试使用PostgreSQL和Psycopg2时,我遇到了一个奇怪的情况.出于某种原因,每次我尝试通过python连接到postgre数据库时,都会收到以下错误:

psycopg2.OperationalError: FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL on
FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL off
Run Code Online (Sandbox Code Playgroud)

当然,我检查了pg_hba.conf以查看问题所在,但据我所知,所有内容似乎都配置正确:

pg_hba.conf的:

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
Run Code Online (Sandbox Code Playgroud)

另外,我发现我可以通过psql连接到数据库,正如我所期望的那样:

$ psql -U steve -h 127.0.0.1
...
steve=>
Run Code Online (Sandbox Code Playgroud)

任何人都对这里可能发生的事情有任何想法?提前致谢!

python postgresql psycopg2

10
推荐指数
1
解决办法
2万
查看次数

从 SQLAlchemy 子查询中选择整个实体

如果我在 SQLAlchemy 查询中指定映射类(~= 数据库表),则返回的行将包含这些类的实例:

q = sess.query(table1, table2, table3.string_column)
q.first()
==> ( <instance of table1>,
      <instance of table2>,
      'string' )
Run Code Online (Sandbox Code Playgroud)

但是,如果我从子查询中选择,则返回的行包含单个列而不是类实例:

q = sess.query(table1, table2, table3.string_column)

q2 = sess.query( q.subquery() )
q2.first()
==> ( col1_of_table1, col2_of_table1, ...,
      col2_of_table2, col2_of_table2, ...,
      'string' )
Run Code Online (Sandbox Code Playgroud)

有没有办法指定我要将子查询中的行保留为映射类的实例?

如果不加入映射类的新实例,我无法弄清楚如何做到这一点。该corresponding_column方法允许我从子查询中引用特定的,但我无法弄清楚如何从子查询中引用完整的实体。我试过玩,select_from但它没有给我正确的行为。

有什么建议?

sqlalchemy python-2.7

7
推荐指数
1
解决办法
1768
查看次数

标签 统计

postgresql ×2

psycopg2 ×2

python ×2

python-2.7 ×1

sqlalchemy ×1