python的sqlite3临时表_Python sqlite3.OperationalError:没有这样的表:

在尝试将学生信息存储到sqlite3数据库时遇到问题,代码在其他程序中正常工作,但在添加学生到PupilPremiumTable时出现'sqlite3.OperationalError: no such table: PupilPremiumTable'的错误。错误发生在查询RecordID的最大值的行。已确认数据库文件存在且表格已创建。解决方案是使用绝对路径而非相对路径来连接数据库文件,以确保正确定位到包含表格的数据库。
摘要由CSDN通过智能技术生成

I am trying to store data about pupils at a school. I've done a few tables before, such as one for passwords and Teachers which I will later bring together in one program.

I have pretty much copied the create table function from one of these and changed the values to for the Pupil's information. It works fine on the other programs but I keep getting:

sqlite3.OperationalError: no such table: PupilPremiumTable

when I try to add a pupil to the table, it occurs on the line:

cursor.execute("select MAX(RecordID) from PupilPremiumTable")

I look in the folder and there is a file called PupilPremiumTable.db and the table has already been created before, so I don't know why it isn't working.

Here is some of my code, if you need more feel free to tell me so, as I said it worked before so I have no clue why it isn't working or even what isn't working:

with sqlite3.connect("PupilPremiumTable.db") as db:

cursor = db.cursor()

cursor.execute("select MAX(RecordID) from PupilPremiumTable")

Value = cursor.fetchone()

Value = str('.'.join(str(x) for x in Value))

if Value == "None":

Value = int(0)

else:

Value = int('.'.join(str(x) for x in Value))

if Value == 'None,':

Value = 0

TeacherID = Value + 1

print("This RecordID is: ",RecordID)

解决方案

You are assuming that the current working directory is the same as the directory your script lives in. It is not an assumption you can make. Your script is opening a new database in a different directory, one that is empty.

Use an absolute path for your database file. You can base it on the absolute path of your script:

import os.path

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

db_path = os.path.join(BASE_DIR, "PupilPremiumTable.db")

with sqlite3.connect(db_path) as db:

You can verify what the current working directory is with os.getcwd() if you want to figure out where instead you are opening the new database file; you probably want to clean up the extra file you created there.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值