使用SQLAlchemy插入数据时,在提供其他值

时间:2016-11-24 13:45:34

标签: python sqlalchemy

我无法覆盖列默认值。我为subjectcan_retry插入了不同值的行。但是,当我检查表时,它仍然是MathFalse。我在这里做错了什么?

from sqlalchemy import *

db = create_engine('sqlite:///myapp-quizzes.db')
metadata = MetaData(db)

quizzes = Table("quizzes", metadata,
    Column("id", Integer, primary_key = True),
    Column("subject", String, default = "Math"),
    Column("description", String),
    Column("answer_a", String),
    Column("answer_b", String),
    Column("answer_c", String),
    Column("answer_d", String),
    Column("can_retry", Boolean, default = False)
)
quizzes.create()

i = quizzes.insert()
i.execute({
    "subject": "Biology",
    "description": "Where is the heart?",
    "answer_a": "In the head",
    "answer_b": "In the arm",
    "answer_c": "In the gut",
    "answer_d": "Dunno",
    "can_retry": True
})

1 个答案:

答案 0 :(得分:0)

列表中的词典必须具有完全相同的键。当然这适用于多个带字典的插入。

quizz_list = [
  { 
     ...
     'subject': 'Math',
     'can_retry': False
  },
  {
     ...
     'subject': 'Biology',
     'can_retry': True
  }
]