我做了以下简单的脚本:
#!/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)
我想知道为什么 name
和 surname
列没有值。该脚本基于: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/