尝试在每个函数的底部显示结果时出现此错误。我在互联网上浏览了很久,似乎已经解决了,我的显示列数据功能也不能完全正常工作,我不知道为什么它只显示列名几次,有人可以帮忙吗?
# Created By Dante 1/07/19, This Program Will Access A Cpu Database And Will Allow You To Create Entries, Read Data And Sort Relevant Data.
#
import sqlite3 # Imports Sql Into The Program
with sqlite3.connect("Cpus.db") as connection: # Verifing Connection To The Database
print(connection)
c = connection.cursor() # Defines C As The Connection To The Cursor
def read_data(connection):
c = connection.cursor()
columns = ("Manufacture, Name_, Cost, Speed_GHz, Cores, Threads") # Columns Variable That Holds Column Names
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
display_choice = int(input("How Would You Like To Display The Data? \nType 1 For All Data \nType 2 For Column Data ")) # Asking For Input
except:
print("Please Type 1 Or 2")
continue # Continues At The Beginning Of The Loops Until Valid Input
if display_choice > 2: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type A Valid Input Number")
elif display_choice < 1: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type A Valid Input Number")
elif display_choice == 1: # An If Statement That Does Something, Dependant On The Users Input/Answer
c.execute("SELECT * FROM Cpus")
elif display_choice == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
read_column_data(connection)
results = c.fetchall() # Takes The Fetch All Results Command And Turns It Into A Simple Results Variable Making It Callable
for i in results:
print(i[0])
print(i[1])
#print("Name: {0:1} Time: {0:1}".format(i[0],i[1])) # Formats The Results And Displays Them
def read_column_data(connection):
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
columns = ("Manufacture, Name_, Cost, Speed_GHz, Cores, Threads") # Columns Variable That Holds Column Names
column_selection = int(input("What Column Would You To Display Data From? \nType 1 For Manufacture \nType 2 For Name_ \nType 3 For Cost \nType 4 For Speed_GHz \nType 5 For Cores \nType 6 For Threads ")) # Asking For Input
break # Breaks Out Of The While True Loop
except:
print("Please Type 1 Or 2")
continue # Continues At The Beginning Of The Loops Until Valid Input
if column_selection > 6: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type A Valid Input Number")
elif column_selection < 1: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type A Valid Input Number")
elif column_selection == 1: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Manufacture"
elif column_selection == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Name_"
elif column_selection == 3: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Cost"
elif column_selection == 4: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Speed_GHz"
elif column_selection == 5: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Cores"
elif column_selection == 6: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_selection = "Threads"
sql_query = ("SELECT ? FROM Cpus") # Sql Query Held As A String So That It Can Be Executed To The Database When Required
#tuple_insert = (column_selection_1)
c.execute(sql_query,(column_selection,)) # Executes The Sql Query And The Tuple To The Cursor That Is Defined As C
results = c.fetchall() # Takes The Fetch All Results Command And Turns It Into A Simple Results Variable Making It Callable
for i in results: # For Loop For Displaying Data
print("Manufacture: {0:1}Name_: {0:1}Cost: {}Speed_GHz: {}Cores: {}Threads: {}".format(i[1],i[2],i[3],i[4],i[5],i[6])) # Formats The Results And Displays Them
def order_data(connection):
c = connection.cursor()
columns = ("Manufacture, Name_, Cost, Speed_GHz, Cores, Threads") # Columns Variable That Holds Column Names
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
print(columns)
column_select = int(input("What Column Would You Like To Order Data From?\nType 1 For Manufacture\nType 2 For Name_\nType 3 For Cost\nType 4 For Speed_GHz\nType 5 For Cores\nType 6 For Cores ")) # Asking For Input
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if column_select > 6: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type The Number Of The Column You Want To Order Cpu Data From")
elif column_select < 1: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type The Number Of The Column You Want To Order Cpu Data From")
else:
break # Breaks Out Of The While True Loop
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
order_method = int(input("How Would You Like To Order The Data? \nType 1 For Descending \nType 2 For Ascending "))
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if order_method > 2: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type 1 Or 2 To Order The Data? ")
elif order_method < 1: # An If Statement That Prints A Reply That Makes The User Enter A Valid Reply
print("Please Type 1 Or 2 To Order The Data? ")
else:
break # Breaks Out Of The While True Loop
if column_select == 1: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Manufacture"
elif column_select == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Name_"
elif column_select == 3: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Cost"
elif column_select == 4: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Speed_GHz"
elif column_select == 5: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Cores"
elif column_select == 6: # An If Statement That Does Something, Dependant On The Users Input/Answer
column_select = "Threads"
elif order_method == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
order_method = "ASCE"
elif order_method == 1: # An If Statement That Does Something, Dependant On The Users Input/Answer
order_method = "DESC"
sql_query = ("SELECT ? FROM Cpus ORDER BY ?,? ") # Sql Query Held As A String So That It Can Be Executed To The Database When Required
tuple_insert = (column_select, column_select, order_method)
c.execute(sql_query,tuple_insert) # Executes The Sql Query And The Tuple To The Cursor That Is Defined As C
results = c.fetchall() # Takes The Fetch All Results Command And Turns It Into A Simple Results Variable Making It Callable
for i in results: # For Loop For Displaying Data
#print(i)
print("Manufacture: {0:1}Name_: {0:1}Cost: {}Speed_GHz: {}Cores: {}Threads: {}".format(i[0],i[1],i[2],i[3],i[4],i[5]))
def create_entry(): # This Function Creates An Entry To The Database
with sqlite3.connect("Cpus.db") as connection:
c = connection.cursor()
append_table_manu = input("What Is The Manufacture Name Of The Cpu You Are Adding? ") # Asking For Input
append_table_cpu = input("What Is The Name Of Your Cpu You Are Adding? ") # Asking For Input
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
append_table_cost = int(input("How Much Does The Cpu You Are Adding Cost? (E.g $99)"))
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if append_table_cost > 100000:
print("Please Type The Actual Cost Of Your Cpu")
elif append_table_cost < 0:
print("Please Type The Actual Cost Of Your Cpu")
else:
break # Breaks Out Of The While True Loop
while True: # Puts Everything Below In A Loop Until It Hits A Break
try:
append_table_speed = int(input("What Is The Speed Of The Cpu That You Are Adding?, (E.g 2.4) "))
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if append_table_speed > 7:
print("Please Type The Actual Speed Of Your Cpu")
elif append_table_speed < 0:
print("Please Type The Actual Speed Of Your Cpu")
else:
break # Breaks Out Of The While True Loop
while True: #Puts Everything Below In A Loop Until It Hits A Break
try:
append_table_cores = int(input("How Many Cores Does The Cpu You Are Adding Have? "))
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if append_table_cores > 16:
print("Please Type The Actual Core Amount Of Your Cpu")
elif append_table_cores < 0:
print("Please Type The Actual Core Amount Of Your Cpu")
else:
break # Breaks Out Of The While True Loop
while True: # Puts Everything Below In A Loop Until It Hits A Break
try:
append_table_threads = int(input("How Many Threads Does The Cpu That You Are Adding Have?, (E.g 99) "))
except:
print("Please Type A Number")
continue # Continues At The Beginning Of The Loops Until Valid Input
if append_table_threads > 10000:
print("Please Type The Actual Thread Amount Of Your Cpu")
elif append_table_threads < 0:
print("Please Type The Actual Thread Amount Of Your Cpu")
else:
break # Breaks Out Of The While True Loop
tuple_insert = (append_table_manu, append_table_cpu, append_table_cost, append_table_speed, append_table_cores, append_table_threads) # This Is Value Holder That Is Used To Insert Input Value Into THe Sql Query
sql_query = ("INSERT INTO Cpus (Manufacture,Name_,Cost,Speed_GHz,Cores,Threads) VALUES (?,?,?,?,?,?)") # Sql Query Held As A String So That It Can Be Executed To The Database When Required
c.execute(sql_query,tuple_insert) # Executes The Sql Query And The Tuple To The Cursor That Is Defined As C
results = c.fetchall() # Takes The Fetch All Results Command And Turns It Into A Simple Results Variable Making It Callable
print(tuple_insert)
for i in results: # For Loop For Displaying Data
print("Manufacture: {0:1}Name_: {0:1}Cost: {}Speed_GHz: {}Cores: {}Threads: {}".format(i[0],i[1],i[2],i[3],i[4],i[5]))
while True: # Puts Everything Below In A Loop Until It Hits A Break
option_1 = int(input("What Would You Like To Do To The Cpu Database? \nType 1 For Creating An Entry \nType 2 For Ordering Data\nType 3 For Displaying Relevant Data ")) #Asks The User For Input And Stores The Value
if option_1 == 1: # An If Statement That Does Something, Dependant On The Users Input/Answer
create_entry() # Runs The Create Entry Function If The User Inputs 1 Into The First Question
elif option_1 == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
order_data(connection) # Runs The Create Entry Function If The User Inputs 2 Into The First Question
elif option_1 == 3: # An If Statement That Does Something, Dependant On The Users Input/Answer
read_data(connection) # Runs The Create Entry Function If The User Inputs 3 Into The First Question
break # Breaks Out Of The While True Loop
我只是使Tuple Index超出范围,并且我尝试重新排列数字,但似乎没有任何作用
答案 0 :(得分:0)
read_column_data
中错误的最直接原因是因为查询sql_query = ("SELECT ? FROM Cpus")
从Cpus表中选择了一个列,但试图显示六个此处的列:
print("Manufacture: {0:1}Name_: {0:1}Cost: {}Speed_GHz: {}Cores: {}Threads: {}".format(i[1],i[2],i[3],i[4],i[5],i[6])) # Formats The Results And Displays Them
[在order_data
,sql_query = ("SELECT ? FROM Cpus ORDER BY ?,? ")
中也有类似的问题]
但是有一个最主要的缺陷:这个sql_query = ("SELECT ? FROM Cpus")
并没有返回您想要或期望的结果。不能将参数替换用于列名(或表名)。因此,在这种情况下,它将返回列名称的字符串文字。
更正这些问题自然会发现其他问题。建议您从选择验证结果开始,然后继续显示它们。
您可能想学习Row Objects in the python doc。这可能有助于解决read_column_data问题,因为您可以按键访问结果。
您将不得不以不同的方式在order_data
中构造查询,因为您不能对ORDER BY子句使用参数替换。
祝你好运!