用python进行插入数据库,sqlite3显示找不到表

发布于 2022-09-13 00:19:37 字数 2489 浏览 16 评论 0

import sqlite3


class Book:
    def __init__(self,name,author,quantity):
        self.__name=name
        self.__author=author
        self.__quantity=int(quantity)
    def setName(self,name):
        print("Please enter the name")
        self.__name=name
    def setAuthor(self,author):
        print("Please enter the author")
        self.__author=author
    def setQuantity(self,quantity):
        print("Please enter the quantity")
        self.__quantity=quantity
    def show(self):
        print(self.__name,self.__author,self.__quantity)

class Computerbook(Book):
    __computerField='python programming'
    def __init__(self,name,author,quantity,computerField):
        self.__name=name
        self.__author=author
        self.__quantity=int(quantity)
        self.__computerField=computerField
    def setName(self, name):
        super(Computerbook,self).setName(name)
    def setAuthor(self, author):
        super(Computerbook,self).setAuthor(author)
    def setQuantity(self, quantity):
        super(Computerbook,self).setQuantity(quantity)
    def show(self):
        print(self.__name,self.__author,self.__quantity,self.__computerField)
    def setComputerField(self,computerField):
        self.__computerField=computerField
    def insertDB(self):
        print('1')
        conn = sqlite3.connect(db_file)
        cur = conn.cursor()
        sql = 'insert into ComputerbookTable (name,author,quantity,computerField) values(?,?,?,?)'
        data = (self.__name,self.__author,self.__quantity,self.__computerField)
        cur.execute(sql,data)
        conn.commit()
        cur.close()
        conn.close()

db_file='.\ComputerbookDB.db'
while True:
    list1 = input().split(' ')
    if len(list1)==1:
        print('结束插入数据库')
        break
    elif len(list1)==3:
        x=Book(list1[0],list1[1],list1[2])
        x.show()
    elif len(list1)==4:
        x=Computerbook(list1[0],list1[1],list1[2],list1[3])
        x.insertDB()
        print('2')

执行他会报错,py文件与db文件在同一目录下,且db文件内已有ComputerbookTable这个表

Traceback (most recent call last):
  File "d:\搬砖File\Python\期末作业\Program2\2.py", line 60, in <module>
    x.insertDB()
  File "d:\搬砖File\Python\期末作业\Program2\2.py", line 44, in insertDB
    cur.execute(sql,data)
sqlite3.OperationalError: no such table: ComputerbookTable

c92a815c55d744ff41624294304e43c.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2022-09-20 00:19:37

报错信息都说没有这个表 你却说有 可能是你运行时的当前路径问题,你先cd到db文件路径下 再运行代码

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文