python - 对 Sql 数据库运行命令时 Python 出现模块错误

标签 python

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

Traceback (most recent call last): File "c:/Users/War/Downloads/CPUS (2) (2).py", line 105, in create_entry() File "c:/Users/War/Downloads/CPUS (2) (2).py", line 95, in 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

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

最佳答案

在尝试向其中插入任何数据之前,请确保先CREATE TABLE。还要记住,您需要处于正确的架构中。通常,您可以这样访问该表:schema.table

关于python - 对 Sql 数据库运行命令时 Python 出现模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57131538/

相关文章:

python - 我如何在django中的请求之外访问当前用户

python - 如何在 Heroku 中运行命令行 python 工具

python - 导入错误 : No module named flask_restful

python - 如何以编程方式访问存储为静态文件的数据(加载 pickle 文件)?

python sqlite 字符串插入

python - 无法获取 Django 模板来打印格式化 JSON

python - 在一张图中绘制多个时间序列

python - 填充预定义的 pandas 数据框

python - 如何抓取带有无限滚动条的网站?

python - 我在分配任务之前得到推荐