【发布时间】:2017-10-24 23:39:19
【问题描述】:
我制作了以下简单的脚本:
#!/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 转储)
当我运行脚本时,我得到 No result output ,当我手动选择查询 SELECT * FROM people 时,我得到以下结果:
hello=# SELECT * FROM people;
id | name | surname
----+------+---------
19 | |
20 | |
(2 rows)
我想知道为什么 name 和 surname 列没有值。该脚本基于:http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html
【问题讨论】:
标签: postgresql python-2.7 sqlalchemy