繁体   English   中英

如何按升序显示我的Sql数据?

[英]How Do I Display My Sql Data In Ascending Order?

我运行程序并回答所有输入,然后插入sql查询,但是由于某种原因,当显示数据时并没有以升序显示,例如sql查询说链接到输出的照片: https:/ /imgur.com/a/1bWE3ux

我对结果的格式有所不同,但似乎什么也没做

# 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
                    display_choice_1 = "Cpus" # Changes The Value Of The Display Variable To Cpus
            elif display_choice == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
                    test(connection)
    tuple_insert = (display_choice)
    sql_query = ("SELECT * FROM ?") # 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 
    for i in results:
            print("Name: {0:1} Time: {0:1}".format(i[0],i[1])) # Formats The Results And Displays Them

def test(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_1 = "Manufacture"  
        elif column_selection == 2: # An If Statement That Does Something, Dependant On The Users Input/Answer
                column_selection_1 = "Name_"
        elif column_selection == 3: # An If Statement That Does Something, Dependant On The Users Input/Answer
                column_selection_1 = "Cost"
        elif column_selection == 4: # An If Statement That Does Something, Dependant On The Users Input/Answer
                column_selection_1 = "Speed_GHz"
        elif column_selection == 5: # An If Statement That Does Something, Dependant On The Users Input/Answer
                column_selection_1 = "Cores"
        elif column_selection == 6: # An If Statement That Does Something, Dependant On The Users Input/Answer
                column_selection_1 = "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_1,)) # 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("".format(i[0],i[1])) # 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, 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("Name: {0:1} Time: {0:1}".format(i[0],i[1])) # Formats The Results And Displays Them

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("".format(i[0],i[1])) # Formats The Results And Displays Them


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

它确实打印了一些东西,但是它不是列中的值,它只是打印了类似列名的5次。 我使用了订单数据功能,然后选择按升序对数据进行排序。

order by <column> asc|desc向查询添加order by <column> asc|desc

select id, name 
from mytable
order by name asc 

暂无
暂无

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

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