简体   繁体   English

SQlite3没有显示在代码PYTHON的末尾

[英]SQlite3 isn't displaying at end of code PYTHON

createDb = sqlite3.connect("hhh.db")
queryCurs = createDb.cursor()

def createTable():
    queryCurs.execute(''' CREATE TABLE maths
    (id INTEGER PRIMARY KEY, userName TEXT, score INTERGER)''')

def addCust(userName,):
    queryCurs.execute("""INSERT INTO maths (userName, score)
    VALUES(?,?)""",(userName, score))

def main():
    createTable()
    addCust("\n, They scored TEXT, score ITERGER, out of 10 TEXT")
    createDb.commit()

    queryCurs.execute("SELECT * FROM maths")

    for i in queryCurs:
      print ("\n")
      for j in i:
          print (j)

queryCurs.close()

It does not display the information that i input into the table. 它不显示我输入到表中的信息。 The code just simply ends without printing the Database information. 代码只是简单地结束而不打印数据库信息。

Im am a novice so please explain fully 我是新手,请充分说明

Does this work ? 这行得通吗? I have moved the creation of the cursor into the main method. 我已经将光标的创建移到了main方法中。 In your implementation there is a chance that the cursor is closed before main is executed depending on the location of your main() method. 在您的实现中,根据main()方法的位置,有可能在执行main之前关闭光标。

def createTable():
    queryCurs.execute(''' CREATE TABLE maths
    (id INTEGER PRIMARY KEY, userName TEXT, score INTERGER)''')

def addCust(userName,):
    queryCurs.execute("""INSERT INTO maths (userName, score)
    VALUES(?,?)""",(userName, score))

def main():
    createTable()

    createDb = sqlite3.connect("hhh.db")
    queryCurs = createDb.cursor()

    addCust("\n, They scored TEXT, score ITERGER, out of 10 TEXT")
    createDb.commit()

    queryCurs.execute("SELECT * FROM maths")

    for i in queryCurs:
      print ("\n")
      for j in i:
          print (j)

    queryCurs.close()

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

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