首页 > 解决方案 > 向 Sql 数据库运行命令时 Python 中的模块错误

问题描述

每次我运行代码完成问题并执行时,Visual Studio 都会引发模块错误。这是打印的错误

Traceback(最近一次调用最后):文件“c:/Users/War/Downloads/CPUS (2) (2).py”,第 105 行,在 create_entry() 文件“c:/Users/War/Downloads/CPUS ( 2) (2).py",第 95 行,在 create_entry c.execute(sql_query,tuple_insert) sqlite3.OperationalError: no such table: Cpus

我查看了我编写的其他程序,但我似乎没有发现语句及其位置有问题

import sqlite3
with sqlite3.connect("Cpus.db") as connection:
        print(connection)
        c = connection.cursor()

def create_entry():
    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? ")
        append_table_cpu = input("What Is The Name Of Your Cpu You Are Adding? ")
        while True:    
                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
                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
        while True:    
                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
                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
        while True:    
                try:
                        append_table_cores = int(input("How Many Cores Does The Cpu You Are Adding Have? "))
                except:
                        print("Please Type A Number")         
                        continue
                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
        while True:    
                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
                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
        tuple_insert = (append_table_manu, append_table_cpu, append_table_cost, append_table_speed, append_table_cores, append_table_threads)
        sql_query = ("INSERT INTO Cpus (Manufacture,Name_,Cost,Speed_GHz,Cores,Threads) VALUES (?,?,?,?,?,?)")
        c.execute(sql_query,tuple_insert)
        results = c.fetchall()
        print(tuple_insert)
        for i in results:
                print("".format(i[0],i[1]))


while True: #Puts Everything In A Loop     
        option_1 = int(input("What Would You Like To Do To The Cpu Database, 1) Make An Entry, 2) Read The Data Or 3) Delete Data. Type 1, 2, ,3 "))
        if option_1 == 1:
                create_entry()   
        break

由于这个错误,我还没有完全测试过这个程序,所以我不希望它能够完全工作。如果发现任何其他错误,请同时注意它们...

标签: python

解决方案


CREATE TABLE在尝试将任何数据插入其中之前,请确保这样做。还要记住,您需要使用正确的模式。您通常像这样访问表:schema.table.


推荐阅读