Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 使用where的SQLAlchemy筛选仅适用于一列_Python_Sqlite_Sqlalchemy - Fatal编程技术网

Python 使用where的SQLAlchemy筛选仅适用于一列

Python 使用where的SQLAlchemy筛选仅适用于一列,python,sqlite,sqlalchemy,Python,Sqlite,Sqlalchemy,我正在使用SQL Alchemy访问SQLite数据库。然而,我在where()过滤器中遇到了一些奇怪的行为。该表定义如下: from sqlalchemy import Column, MetaData, String, Table, select, Integer metadata = MetaData() self._table = Table( self._table_name, metadata, Column("email", Strin

我正在使用SQL Alchemy访问SQLite数据库。然而,我在where()过滤器中遇到了一些奇怪的行为。该表定义如下:

from sqlalchemy import Column, MetaData, String, Table, select, Integer

metadata = MetaData()
self._table = Table(
    self._table_name,
    metadata,
    Column("email", String(64), primary_key=True, nullable=False),
    Column("name", String(64), nullable=False),
    Column("password", String(64), nullable=False),
)
我使用以下命令插入一行:

query = self._table.insert().values(
            name=user.name, email=user.email, password=user.password
        )
session.execute(query)
session.commit()
现在,当我尝试使用以下代码获取find the row时,它返回一个空列表:

query = select([self._table]).where(self._table.c.email == "toto@toto.com")
print("Compiled Query ", query)
data = self._execute_select(query)
print("Found Data: ", data)
上述代码给出了输出:

Columns are:  ImmutableColumnCollection(user.name, user.email, user.password)
Compiled Query  SELECT "user".name, "user".email, "user".password
FROM "user"
WHERE "user".email = :email_1
Found Data:  []
如果我将其稍微更改为使用“name”而不是“email”,它似乎可以按预期工作: (只更改了第二行!)

上述代码的输出为:

Columns are:  ImmutableColumnCollection(user.email, user.name, user.password)
Compiled Query  SELECT "user".email, "user".name, "user".password
FROM "user"
WHERE "user".name = :name_1
Found Data:  [('tototo', 'pawan', 'toto@toto.com')]
在这两行之间不执行任何其他操作,因此数据始终存在于数据库中。我注意到将“name”更改为“password”,将值更改为“tototo”也找不到任何数据。所以本质上,我只能用“名字”来搜索


请帮助我理解此错误。

上一个示例中有一个
用户名
列不在表定义中-请确保您共享的代码正确。如果您提供的样本数据能够可靠地再现这种行为,这也会有所帮助。@snakecharmerb抱歉。应该是“名字”。我现在已经更新了。可靠地再现上述行为的示例数据还提供为name=“pawan”、password=“tototo”、email=”toto@toto.com“对我有用。也许
execute\u select
正在做什么?或者某个地方有一个看不见的角色?如果没有全面的查询,很难确定。您是否在数据库shell中执行了此查询?@snakecharmerb谢谢您的帮助。我真的发现了这个错误,我真是太蠢了……当我进入数据库时,电子邮件和密码已经互换了。我在执行测试时发现了这个问题
Columns are:  ImmutableColumnCollection(user.email, user.name, user.password)
Compiled Query  SELECT "user".email, "user".name, "user".password
FROM "user"
WHERE "user".name = :name_1
Found Data:  [('tototo', 'pawan', 'toto@toto.com')]