无法从sqlite表中提取项目以便在Python

时间:2016-01-18 16:44:04

标签: python mysql sqlite

刚开始,我只是一个初学者。我所知道的很多,我在过去两天从互联网上学到了很多。现在问题。

我正在开发一个目标是在python中开发基于文本的琐事游戏的项目。我使用sqlite3数据库来存储问题的信息。数据库布局由三列组成,第一列包含ID号,第二列包含带有问题和答案选项的文本字符串,第三列包含对其相应问题的答案。

我可以轻松地创建列表并从中提取信息块,但是当我需要一次拉一行时问题就出现了。为了最清晰,请完整填写我的代码:

#imports necessary SQL connection files
import sqlite3
import sys

#Data to form the Question table
info = (
    ("Who invented the Golden Meme? A:Copericus  B:John Cena  C:Aristotle  D:Shie Lebeouf", "C"),
    ("What is the best chip flavor? A:Sour Cream & Onion  B:Salt & Vinegar  C:Barbecue  D:Moldy", "B"),
    ("Who will be prisident in 2017? A:Donald Trump  B:Bernie Sanders  C:Hillary Clinton  D:Ben Carson", "D"),
    ("Why? A:Becase he's smart and well educated  B:Because he's a doctor  C:Because once you go black you never go back  D:Because my IQ is less than 30", "C")
)


#Connects to the SQL database
con = sqlite3.connect('Questions.db')
cur = con.cursor()


#Deletes the Questions table if it already exists, then creates a new one containing all the data
with con:
    cur.execute('''DROP TABLE IF EXISTS Questions''')
    cur.execute('''CREATE TABLE Questions(ID INTEGER PRIMARY KEY, question TEXT, answer TEXT)''')
    cur.executemany('''INSERT INTO Questions(question, answer) VALUES(?, ?)''', info)
    con.commit()

#Prints instructions
def instructions():
    print()
    print("INSTRUCTIONS: When a question is displayed, type the letter that corresponds to the desired answer choice. Type   using capital letters only. If you answer a question wrong, you will recieve a Failure message and the game will  end. If you answer all questions correctly, you win.")
    print()
    print()

#Displays which question the player is on
def counter():
    global n
    print("Question#", n)
    n = n+1
    nextQuestion()

#Displays the next question and recieves the players answer choice
def nextQuestion():
    cur.execute('''SELECT question FROM Questions''')
    Q = cur.fetchone()
    if Q == None:
        print()
        print("Victory!")
        print()
        return False;
    else:
        print (Q)
    playerAnswer = str(input("Your Answer: "))
    answerValidation(playerAnswer)

#Determines is the answer is correct and, if so, restarts the process
def answerValidation(playerAnswer):
    cur.execute('''SELECT answer FROM Questions''')
    B = cur.fetchone()
    if playerAnswer == B:
        print()
        print("Correct!")
        print()
        counter()
    else:
        print()
        print ("You Failed!")
        print(B)
        return False

n = 1
instructions()
counter()

问题是我可以打印第一个问题,但之后没有任何问题。我的印象是cur.fetchone()假设要获取当前行行然后继续下一行,但是当代码运行第二遍时,它只会重新打印第一个问题。

这是专门针对该问题的部分。

def nextQuestion():
cur.execute('''SELECT question FROM Questions''')
Q = cur.fetchone()
if Q == None:
    print()
    print("Victory!")
    print()
    return False;
else:
    print (Q)

还有第二个问题。我还使用cur.fetchone()系统来提取相应的答案。它确实获取了正确的答案,虽然可能只是第一个答案,但它提取的答案仍然与表中的格式相同。我输入了一个打印B行,看看它给了我什么答案,结果是('C',)。我认为这就是我放入的任何答案总是错误的原因。即使我输入C,正确的答案,它仍然是不正确的,很可能是因为从表中提取的答案中有撇号,圆括号和彗星。

如果我更改代码以便playerAnswer in ['C','c']:它会将答案C计算为正确并重新运行程序再次提起第一个问题。

与答案问题有关的代码:

    playerAnswer = str(input("Your Answer: "))
    answerValidation(playerAnswer)

#Determines is the answer is correct and, if so, restarts the process
def answerValidation(playerAnswer):
    cur.execute('''SELECT answer FROM Questions''')
    B = cur.fetchone()
    if playerAnswer in ['C','c']:
        print()
        print("Correct!")
        print()
        counter()
    else:
        print()
        print ("You Failed!")
        print(B)
        return False

总结一下,有三个主要问题:

  1. 我无法让程序一次打印一个问题,只有在给出正确答案的情况下才能继续下一个问题。
  2. 我无法让该计划接受与其相应问题相对应的正确答案。
  3. 我无法让程序打印问题并回答其存储格式('X',)
  4. 之外的文字

    非常感谢任何解决方案,想法或帮助。

    还应该注意的是,我设计的程序按顺序完成问题(主要是因为我觉得它会更容易),但它不必以这种方式工作。如果有人能提供解决上述问题的解决方案,但随机选择问题,我也非常感谢。它必须是可扩展的,不能多次显示问题。

1 个答案:

答案 0 :(得分:1)

关于您的问题:

1./2。 fetchone()返回当前查询的下一行。

对于每个问题,您都会开始一个新查询。因此你的fetchone()    call始终是第一个问题。

您还应该在一个查询中获得问题和相应的答案: SELECT question, answer FROM questions。两个独立的方式 查询仅在您查询/使用问题ID时才有效。

第3 fetchone()将整行作为元组返回。要访问第一个字段,请使用 指数:

cur.execute('''SELECT answer FROM Questions''')
Q = cur.fetchone()
answer = Q[0]