Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用sqlalchemy将字典列表中的内容添加到表中?_Python_Sqlalchemy - Fatal编程技术网

Python 如何使用sqlalchemy将字典列表中的内容添加到表中?

Python 如何使用sqlalchemy将字典列表中的内容添加到表中?,python,sqlalchemy,Python,Sqlalchemy,我试图用sqalchemy将字典列表中的内容逐个添加到表中 posts = [ { 'title': 'Version 8.0 Coming Soon', 'content': 'As outlined in Ending Python 2 Support, 7.1.x will be the last version to support<br> Python 2.7 and 3.5. The next version will be 8.0 and will su

我试图用sqalchemy将字典列表中的内容逐个添加到表中

posts = [
{
    'title': 'Version 8.0 Coming Soon',
    'content': 'As outlined in Ending Python 2 Support, 7.1.x will be the last version to support<br> Python 2.7 and 3.5. The next version will be 8.0 and will support Python 3.6 and<br> newer.'
    
},
{
    'title': 'Install or Upgrade',
    'content': 'Install from PyPI with pip:<br> <div class="installation"><ul><li>Drop support for Python 3.4. This will be the last version to support Python 2.7 and 3.5.</li><li>Multiple fixes in low-level Windows compatibility code.</li></ul></div>'
   
},
{
    'title': 'Donate to Support Pallets',
    'content': 'The Pallets organization accepts donations as part of the non-profit Python<br> Software Foundation (PSF). Donations through the PSF support our efforts to<br> maintain the projects and grow the community.'
},
{
    'title': 'Version 2.0 Coming Soon',
    'content': 'As outlined in Ending Python 2 Support, 1.0.x will be the last version to support<br> Python 2.7 and 3.5. The next version will be 2.0 and will support Python 3.6 and<br> newer.'
}
错误:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: post.installation
插入

db.create_all()
for posts_in_blog in posts:
title_from_dic = posts_in_blog.get("title")
content_from_dic = posts_in_blog.get("content")
inserting_into_table = Post(title = title_from_dic, content = content_from_dic) 
db.session.add(inserting_into_table)
db.session.commit()


   
我希望我能找到解决办法
感谢您抽出时间

您需要将
id
autoincrement设置为true或提供其值。因为您没有提供id的值,所以出现了此错误


因此,将
id=db.Column(db.Integer,primary\u key=True)
更改为
id=db.Column(db.Integer,primary\u key=True,autoincrement=True)

即使在chenges:-->sqlalchemy.exc.IntegrityError:(sqlite3.IntegrityError)NOTNULL约束失败:post.installation[SQL:INSERT-INTO-post-post]之后,我仍然不幸地遇到了这个错误(标题,内容)值(?,)][参数:('Version 8.0即将推出',正如在结束对Python 2的支持中所述,7.1.x将是支持
Python 2.7和3.5的最后一个版本。下一个版本将是8.0,将支持Python 3.6和
更新版本]。(此错误的背景信息:sqlalche.me/e/13/gkpj)据我所知,如果我键入'try:db.session.add(在_表中插入_)db.session.commit(),则表中永远不会充满infor,IntegrityError:db.session.rollback()除外`错误消失了,但表仍然是空的死区刷新表?请查看
将_插入_表中
是否成为合适的表orm对象。否则,请尝试使用
db.create_all()
从fresh创建表,然后使用python orm在表中添加数据,如
x=Post(content=',title='')db.session add(x)db.session.commit()
并使用
Post.query.all()
Booh查看数据。此时,我不知道要在代码中更改什么。如果您告诉我在这些之后如何处理,这会帮我一个忙
db.create_all()
for posts_in_blog in posts:
title_from_dic = posts_in_blog.get("title")
content_from_dic = posts_in_blog.get("content")
inserting_into_table = Post(title = title_from_dic, content = content_from_dic) 
db.session.add(inserting_into_table)
db.session.commit()