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
SQLite SELECT语句在Python中失败_Python_Sqlite - Fatal编程技术网

SQLite SELECT语句在Python中失败

SQLite SELECT语句在Python中失败,python,sqlite,Python,Sqlite,我一直在努力创建一个基本的应用程序,它可以查询我拥有的记录数据库,以建议我接下来应该听什么。我当前的草稿要求用户输入他们正在收听的当前专辑,然后查询数据库以返回相关流派以及该流派中的其他艺术家 我用来两次返回艺术家和流派作品的c.execute和c.fetchone()命令。当我试图设置reclist=c.fetchall()时,代码失败 整个过程中最糟糕的部分是,我让代码运行了几分钟,然后回去修补一些东西。我想我已经学到了做“无害”改变的教训 数据库的架构: sqlite> .schem

我一直在努力创建一个基本的应用程序,它可以查询我拥有的记录数据库,以建议我接下来应该听什么。我当前的草稿要求用户输入他们正在收听的当前专辑,然后查询数据库以返回相关流派以及该流派中的其他艺术家

我用来两次返回艺术家和流派作品的
c.execute
c.fetchone()
命令。当我试图设置
reclist=c.fetchall()
时,代码失败

整个过程中最糟糕的部分是,我让代码运行了几分钟,然后回去修补一些东西。我想我已经学到了做“无害”改变的教训

数据库的架构:

sqlite> .schema
CREATE TABLE album (
id INTEGER PRIMARY KEY,
album TEXT
);
CREATE TABLE genre (
id INTEGER PRIMARY KEY,
genre TEXT
);
CREATE TABLE artist (
id INTEGER PRIMARY KEY,
artist TEXT
);
CREATE TABLE aag (
artist_id INTEGER,
album_id INTEGER,
genre_id INTEGER
);
完整的Python代码如下:

import sqlite3
import sys

conn = sqlite3.connect('aag_database.db')
c = conn.cursor()

print "Are you listening to a record right now?"
uresponse1 = raw_input("[y / N]")

#collect query information from user
if uresponse1 == "N":
    sys.exit()

print "What is the name of the record are you listening to?"
uresponse2 = raw_input("> ")

print "Let me see what I know about %r" % (uresponse2)

#query record database
c.execute('''Select artist.artist FROM 
    artist, album, aag WHERE 
    artist.id=aag.artist_id AND
    album.id=aag.album_id AND
    album.album=?''', (uresponse2,))
artist_return = str(c.fetchone())

if artist_return == "None":
    print "I guess I don't know anything about that record."
    sys.exit()

#confirm match
print "Oh do you mean %r by %r" % (uresponse2, artist_return)

uresponse3 = raw_input("[y / N]")

if uresponse3 == "N":
    sys.exit()

#query record database for genre of user input
c.execute('''Select genre.genre FROM 
    genre, album, aag WHERE 
    genre.id=aag.genre_id AND 
    album.id=aag.album_id AND 
    album=?''', (uresponse2,))
genre_return = str(c.fetchone())

if genre_return == "None":
    print "I guess I don't know anything about that genre."
    sys.exit()

#confirm match
print "I would classify that album as %r music" % (genre_return)

print "If you like that you may also enjoy these records:"

#query record database on genre
c.execute('''Select artist, album FROM 
    genre, album, artist, aag WHERE 
    genre.id=aag.genre_id AND 
    album.id=aag.album_id AND 
    artist.id=aag.artist_id AND
    genre.genre=?''', (genre_return,))
reclist = c.fetchall()

print reclist

我多次查看代码,并尝试设置变量
genre\u return=c.fetchone()[0]
。这似乎是通过sqlite而不是之前传递的元组来传递实际的表值

我已经通过将
genre\u return
设置为基于
genre.genre
以及
genre.id
的选择来测试此方法。在这两种情况下,查询都成功运行

我无法解释为什么
album\u return=str(c.fetchone())
代码可以成功地查询数据库(作为元组),而我的
genre\u return
无法查询,但我的问题似乎已经解决


这是一次相当不错的学习经历。希望这能帮助其他尝试在python中使用sqlite的人。

以及使用修订控制(如
git
)的课程!返回了什么错误消息/stacktrace?没有错误消息,这是令人沮丧的一部分。当我打印reclist时,我的值为None。不过,我一直在使用的测试也有其他几位同类型的艺术家。更令人沮丧的是,当我在sqlite3中执行最终查询时,它会起作用。fetch one将集合的第一个元素作为元组返回,因此[0]获取元组的第一个元素。感谢您的解释!