即使删除了数据库文件,表仍然存在

时间:2020-09-15 18:18:09

标签: python sql sqlite

每次学习SQL代码时,我都试图创建一个新的数据库,方法是在创建新数据库之前,先引用一个删除的文件(我知道{{ 1}},如果使用文件不起作用,可能会使用它),但是SQLite表示,只要它是我以前使用过的文件路径,该表就已经存在。例如,我使用了“ recipes3.db”路径,并且没有错误,但是随后我删除了该文件并再次使用它,并说该表已经存在。我已经重新启动了解释器并重置了连接,但没有任何操作释放文件。在预期目录中创建和删除文件。我不知道这些数据在哪里残留,以致我无法重用路径。

我已经进行了更多测试,并且可以:memory:然后sqlite3.connect("recipes2.db")进行操作,并且数据库中有一个具有该名称的表,就像我之前创建的一样,即使“ recipes2.db”文件没有t在运行命令之前或之后存在。与“ recipes5.db”一起使用,它没有看到任何这样的表,因为我没有使用过该路径。现在,该路径包含一个表。所以有些地方我不知道。

我的代码:

select * from recipes

编辑2: def myPath(s): return "C:\\Users\\Kevin\\Desktop\\Python\\" + s def loadDataIntoDatabase(database): sqliteClear(database, None) conn = sqlite3.connect(database) sqliteFile(conn, "recipe_tables_init.txt") cursor = conn.cursor() with open(myPath("recipe_list.txt"), "r") as file: for line in file: a = tuple(line.split(",")) cursor.execute("insert into recipes values (?, ?, ?, ?)", a) with open(myPath("recipe_item_list.txt"), "r") as file: for line in file: a = tuple(line.split(",")) cursor.execute("insert into recipe_items values (?, ?, ?, ?)", a) conn.commit() conn.close() def sqliteClear(filename, conn): if not conn: conn = sqlite3.connect(filename) conn.close() s = myPath(filename) if os.path.isfile(s): os.remove(s) def sqliteFile(conn, filename): cursor = conn.cursor() with open(myPath(filename), "r") as file: cursor.executescript(file.read()) cursor.close() 的内容:

recipe_table_init.txt

我仅用create table recipes ( recipe_name varchar (30) primary key, category varchar (30), energy_required integer not null, enabled_or_disabled character (1) not null, constraint ed_constraint check (enabled_or_disabled in ('e', 'd')) ); create table recipe_items ( recipe_name varchar (30) not null, item_name varchar (30) not null, input_or_output character (1) not null, item_amount integer not null, constraint io_constraint check (input_or_output in ('i', 'o')), constraint branch primary key (recipe_name, item_name, input_or_output), constraint name_fk foreign key (recipe_name) references recipes (name) on delete cascade ); 之类的简单名称调用loadDataIntoDatabase并遇到此问题。我已经调用了其他函数,但是即使只是在解释器的开头调用该函数(然后再次导致表重复)也会导致此问题,因此,除了在*.db当然。

编辑: 我知道this question,但不幸的是,这似乎与我的工作无关。

1 个答案:

答案 0 :(得分:1)

如前所述,对任何SQLite数据库引用使用绝对路径。使用相对路径,新数据库位于脚本文件所在的位置。甚至对原始字符串使用os.path.join来识别单个反斜杠。甚至考虑executemany

import os

def myPath(s): 
  db_path = r"C:\Users\Kevin\Desktop\Python"
  return os.path.join(db_path, s)

def loadDataIntoDatabase(database): 
   sqliteClear(myPath(database), None) 
   conn = sqlite3.connect(myPath(database))
   cursor = conn.cursor()

   sqliteFile(conn, "recipe_tables_init.txt") 

   with open(myPath("recipe_list.txt"), "r") as file: 
     rows = [line.split(",") for line in file]
     cursor.executemany("insert into recipes values (?, ?, ?, ?)", rows) 

   with open(myPath("recipe_item_list.txt"), "r") as file:
     rows = [line.split(",") for line in file] 
     cursor.executemany("insert into recipe_items values (?, ?, ?, ?)", rows)
             
   conn.commit()
 
   cursor.close()
   conn.close()