sqlite3 的路径有问题吗?



我使用lubuntu创建了我的第一个tkinter+sqlite应用程序,它运行得很好,但当我在windows上运行它时,我一直收到一个数据库未找到的错误。

这是我的代码:

class App():
    ...
class Data():
    def __init__(self, username=None, password=None, inst=None):
        self.serverlist = []
        self.username = username
        self.password = password
        self.inst = inst
        self.populate_serverlist()
        self.populate_attributes()
        print(self.username + self.password + self.inst)
    def populate_serverlist(self):
        ...
    def populate_attributes(self):
        ...
    def add_new_profile(self, username, password, inst):
        ...
    def get_profile(self):
        ...
    @staticmethod
    def run_query(sql, data=None, receive=False):
        conn = sqlite3.connect("profile.db")
        cursor = conn.cursor()
        if data:
            cursor.execute(sql, data)
        else:
            cursor.execute(sql)
        if receive:
            return cursor.fetchall()
        else:
            conn.commit()
        conn.close()
    u/staticmethod
    def first_timeDB():
        create_table = "CREATE TABLE profile (username text, password text, inst text)"
        Data.run_query(create_table)

if __name__ == '__main__':
    app = App()
    if not os.path.isfile("profile.db"):
        app.data.first_timeDB()
    app.mainloop()

我尝试将"profile.db"替换为完整路径('C:\User\Doc\profile.db'(,但仍然找不到。

然后我也尝试了这个技巧,但也没有成功,这是修改后的代码:

class Data():
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(BASE_DIR, "profile.db")
    ...
    def run_query(sql, data=None, receive=False):
        conn = sqlite3.connect(db_path)

我得到一个"NameError:name‘db_path’is not defined"错误(即使它按照我的main代码创建profile.db文件(。所以有人知道我做错了什么???

找到了答案。事实证明这与路径无关。方法"populate_attributes(("在创建表之前试图从表中获取值,因为该方法是由类Data的init((调用的。

在windows上运行DB时,DB路径应为双斜线\/

最新更新