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

Bad_Down
  • 12
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

回复
阅读 323
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏