简体   繁体   English

sqlite3 Select语句在Python中不起作用

[英]sqlite3 Select statement not working in Python

I have an sqlite3 database which has been created via the following CREATE statement in a .sql file: 我有一个sqlite3数据库,该数据库是通过.sql文件中的以下CREATE语句创建的:

create.sql

CREATE TABLE dogs (id INTEGER PRIMARY KEY, 
                   name TEXT, 
                   age INTEGER, 
                   gender CHAR(1), 
                   breed TEXT, 
                   temperament TEXT, 
                   hungry BOOLEAN);

And seeded with the following data via an INSERT statement in a .sql file: 并通过.sql文件中的INSERT语句将以下数据作为种子:

insert.sql

INSERT INTO dogs (name, age, gender, breed, temperament, hungry) VALUES
                 ("Snoopy", 3, "M", "beagle", "friendly", 1),
                 ("McGruff", 10, "M", "bloodhound", "aware", 0),
                 ("Scooby", 6, "M", "great dane", "hungry", 1),
                 ("Little Ann", 5, "F", "coonhound", "loyal", 0),
                 ("Pickles", 13, "F", "black lab", "mischievous", 1),
                 ("Clifford", 4, "M", "big red", "smiley", 1),
                 ("Lassie", 7, "F", "collie", "loving", 1),
                 ("Snowy", 8, "F", "fox terrier", "adventurous", 0),
                 (NULL, 4, "M", "golden retriever", "playful", 1);

I have a Python Jupyter notebook where I am performing the following actions to create and populate the database 'pets.db': 我有一个Python Jupyter笔记本,在其中执行以下操作来创建和填充数据库“ pets.db”:

import sqlite3
connection = sqlite3.connect('pets.db')

cursor = connection.cursor()

file = open("./create.sql", 'r')
create_sql = file.read()
cursor.executescript(create_sql)

file1 = open("./insert.sql", 'r')
insert_sql = file1.read()
cursor.executescript(insert_sql)

Then I have a file of Python functions titled 'selects.py' where I am storing SQL select statements. 然后,我有一个名为“ selects.py”的Python函数文件,用于存储SQL select语句。 The first one, shown below, is meant to return the name and breed of all female dogs in the table. 如下所示,第一个用于返回表中所有母狗的名称和品种。

def sql():
    return "SELECT name, breed FROM dogs WHERE gender = 'F';"

I then try to run the above function in Jupyter: 然后,我尝试在Jupyter中运行上述功能:

from selects import sql
cursor.execute(sql()).fetchall()

And I receive the following error: 而且我收到以下错误:

NameError: name 'f' is not defined

I've been trying for the past hour to format the select statement in a way which works in the Python file, but am coming up short each time. 在过去的一个小时中,我一直在尝试以一种可以在Python文件中使用的方式来格式化select语句,但是每次都会简短。 Is it the way that the SQL SELECT statement is written that is incorrect or is it something in the format of the Python code? 是SQL SELECT语句的编写方式不正确,还是采用Python代码格式?

edit: Here is the complete traceback: 编辑:这是完整的回溯:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-87-df843ecc4fb9> in <module>()
      1 from selects import sql
----> 2 cursor.execute(sql())
      3 results = cursor.fetchall()
      4 results

~/ds-practice/selects.py in sql()
      1 def sql():
----> 2     return "SELECT name, breed FROM dogs WHERE gender = 'F';"
      3 
      4 
      5 

NameError: name 'f' is not defined

It looks like your question is missing some key information. 您的问题似乎缺少一些关键信息。 The error NameError: name 'f' is not defined is a Python error, indicating that you're referring to something supposedly called f , which has not been defined yet. 错误NameError: name 'f' is not defined Python错误,表示您所指的是所谓的f ,尚未定义。

Executing the SQL you provided and running the below script from Python works without problems: 执行您提供的SQL并从Python运行以下脚本可以正常工作:

import sqlite3

connection = sqlite3.connect('pets.db')
cursor = connection.cursor()


def sql():
    return "SELECT name, breed FROM dogs WHERE gender = 'F';"


cursor.execute(sql()).fetchall()

With the error, you probably also receive a line number and module name where this error occurs. 出现此错误,您可能还会收到发生此错误的行号和模块名称。 You should probably look closely at how the exact spelling differs from the above. 您可能应该仔细查看确切的拼写与上面的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM