postgresql - Python SqlAlchemy : Data are inserted with NULL values instead of specified values

标签 postgresql python-2.7 sqlalchemy

我做了以下简单的脚本:

#!/usr/bin/env python
# coding=utf-8
# -*- Mode: python; c-basic-offset: 4 -*-

from sqlalchemy import *

db=create_engine('postgresql://username:password@127.0.0.1/hello')
db.echo = False
metadata = MetaData(db)

people=Table('people',metadata)

# Insert Data
stmt=people.insert()
stmt.execute(name="Dimitrios",surname="Desyllas")
stmt.execute({'name':"Dionysis",'surname':"Arsenis"})

#Select Data
stmt=people.select()
results=stmt.execute().fetchone()

for result in results:
    print "Result:"
    print result

使用以下 postgresql 表:

hello=# \d people
                                 Table "public.people"
 Column  |         Type          |                      Modifiers                      
---------+-----------------------+-----------------------------------------------------
 id      | integer               | not null default nextval('people_id_seq'::regclass)
 name    | character varying(60) | 
 surname | character varying(60) | 

(从 psql 转储)

当我运行脚本时,我没有得到结果输出,当我手动选择查询 SELECT * FROM people 时,我得到以下结果:

hello=# SELECT * FROM people;
 id | name | surname 
----+------+---------
 19 |      | 
 20 |      | 
(2 rows)

我想知道为什么 namesurname 列没有值。该脚本基于:http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

最佳答案

只需 autoload=True 替换行即可:

people=Table('people',metadata)

与:

people=Table('people',metadata,autoload=True)

关于postgresql - Python SqlAlchemy : Data are inserted with NULL values instead of specified values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44161559/

相关文章:

postgresql - 可以在不使用 C 编写函数的情况下在 postgres 中创建自定义类型吗?

正则表达式清理带有空格的 id 并从字符串中提取

python - 使用 PyQt 动态将项目设置为 QML ListModel

python - 用棉花糖序列化日期时间的简短方法

python - 在 SQLAlchemy 中过滤时访问关系的 backref

mysql - 根据最高时间戳选择每个主机名的利用率百分比

sql - Postgres jsonb列从字符串与通配符匹配的数组中选择

Python 2 - 解密 Java 的 PBEWithMD5AndDES

python-2.7 - Python-网络x : Neighbours with certain weight

python - 是否有 SqlAlchemy 数据库不可知的 FROM_UNIXTIME() 函数?