Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 我能';t在SQLAlchemy中插入或提交数据_Python_Sqlalchemy - Fatal编程技术网

Python 我能';t在SQLAlchemy中插入或提交数据

Python 我能';t在SQLAlchemy中插入或提交数据,python,sqlalchemy,Python,Sqlalchemy,我就是这么做的: from flaskblog import User, Post 我得到这个错误: Traceback (most recent call last): File "<input>", line 1, in <module> ImportError: cannot import name 'Post' db.session.add(user1) Traceback (most recent call last): File "<input&g

我就是这么做的:

from flaskblog import User, Post
我得到这个错误:

Traceback (most recent call last): 
File "<input>", line 1, in <module>
ImportError: cannot import name 'Post'
db.session.add(user1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'db' is not defined
如果我只导入用户

from flaskblog import User
我没有错

然后我创建
user1
以插入有关
user1的信息。

user1=User(username='rupak' ,email='rupak@demo.com', password='rupak123')
我尝试添加用户,但出现以下错误:

Traceback (most recent call last): 
File "<input>", line 1, in <module>
ImportError: cannot import name 'Post'
db.session.add(user1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'db' is not defined

你有很多问题

1) 阅读有关导入功能的文档

2) 类用户的语法不正确(我认为您只是在复制它时出错了)

3) db是一个数据库连接实例。您需要首先创建它


能否提供有关项目文件夹的更多信息?

主程序在哪里?这是您为User和Post创建类的文档,但是:您在哪里使用它们?您还试图执行一个
db.session.add(user1
),但是数据库的连接字符串在哪里?我需要有关您正在运行的程序的更多信息。您在文件结构中的什么位置查询用户?你能纠正压痕吗?你能发布项目结构吗?这将有助于调试为什么你不能导入你的类?我更改了我的程序。。我把整个节目都放在我的帖子里了