createDataBase1 = sqlite3.connect('PythonDatabase_1.db')
queryCurs1 = createDataBase1.cursor()
def createtable():
queryCurs1.execute("""CREATE TABLE Scores
(id INTEGER PRIMARY KEY, Name TEXT, Class TEXT, Score INTERGER)""")
def addPupil1(Name, Class, Score):
queryCurs1.execute("""INSERT INTO Scores (Name, Class, Score)
VALUES(?,?,?)""",(Name, Class, Score,))
def main1():
createtable1()
addPupil1
(Name, Class, Score)
createDataBase1.commit()
queryCurs1.execute("SELECT * FROM Scores ORDER BY Score DESC")
title = ["id","Name", "Class", "Score"]
k = 0
for i in queryCurs1:
print "\n"
for j in i:
print title[k],
print j
if k < 3: k += 1
else: k = 0
queryCurs1.close()
if __name__ == '__main__':main()
这用于打印结果现在它没有什么问题? 它用于以列表格式显示结果,前面的标题现在没有错误
答案 0 :(得分:0)
我冒昧地改变你的代码。检查它是否适合您。我更喜欢使用PEP 8代码样式。我还将一个通用记录插入数据库(将其更改为您需要的任何内容)。
另一方面,我还更改了表的create语句,因为一旦创建了表(运行命令两次),它就会显示一个错误,因为表已经创建了。
代码如下所示:
import sqlite3
created_db = sqlite3.connect('PythonDatabase_1.db')
cursor = created_db.cursor()
def create_table():
cursor.execute("""CREATE TABLE IF NOT EXISTS Scores
(id INTEGER PRIMARY KEY, Name TEXT, Class TEXT, Score INTERGER)""")
def add_pupil(name, klass, score):
cursor.execute("""INSERT INTO Scores (Name, Class, Score)
VALUES(?,?,?)""",(name, klass, score,))
def main():
create_table()
name = "John Doe"
klass = "Math"
score = 100
add_pupil(name, klass, score)
created_db.commit()
cursor.execute("SELECT * FROM Scores ORDER BY Score DESC")
title = ["id","Name", "Class", "Score"]
k = 0
for i in cursor:
print "\n"
for j in i:
print title[k],
print j
if k < 3: k += 1
else: k = 0
cursor.close()
if __name__ == '__main__':
main()
希望它有所帮助,