【问题标题】:Python SqlAlchemy: Data are inserted with NULL values instead of specified valuesPython SqlAlchemy:使用 NULL 值而不是指定值插入数据
【发布时间】: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)

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

【问题讨论】:

    标签: postgresql python-2.7 sqlalchemy


    【解决方案1】:

    只需autoload=True 表替换行:

    people=Table('people',metadata)
    

    与:

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

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2011-05-13
      • 2022-07-28
      • 2017-08-04
      相关资源
      最近更新 更多