2017-05-24 99 views
0

我已做出了如下的简单脚本指定的值:的Python SQLAlchemy的:数据插入NULL值,而不是

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

from sqlalchemy import * 

db=create_engine('postgresql://username:[email protected]/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

回答

0

只是autoload=True表:

people=Table('people',metadata) 

有了:

people=Table('people',metadata,autoload=True) 
剧本的基础上
相关问题