如何在SQLite3中检查文件是否存在

问题描述 投票:0回答:1

对于我的项目,我需要创建一个数据库,其中一个要求是它必须检查文件是否存在,我已经尝试过,但是当我尝试时我的代码不起作用。我看过很多教程,但没有一个起作用,请帮助我(我更喜欢正确的代码而不是仅仅文本,因为每当我尝试它时它都不起作用)。主要问题是创建数据库后选择不起作用,您无法检查变量是否起作用。我也很乐意接受任何其他提示或建议。 这是我的代码:

import sqlite3
from replit import clear
import time as t

bd = sqlite3.connect('Builds.db')

cursor = bd.cursor()

cursor.execute('''
    CREATE TABLE Builds (
        Build TEXT,
        Weapon TEXT,
        Aspect TEXT,
        Keepsakes TEXT,
        Arcana TEXT,
        Boons TEXT,
        Hammers TEXT
    )
''')


data = [
    ('Pan Snipe', ' Sister Blades', ' Aspect of Pan',' Keepsakes: Transcendant Embryo, Aromatic Phial, Knuckle Bones or Luckier Tooth', ' Arcana Cards: None', ' Boons: Poseidon- Wave Flourish (Heroic), Hera- Engangment Ring (Rare), Artemis- Death Warrant', ' Hammers: Spiral Knives and Dancing Knives')
]


cursor.executemany('INSERT INTO Builds (Build, Weapon, Aspect, Keepsakes, Arcana, Boons, Hammers) VALUES (?, ?, ?, ?, ?, ?, ?)', data)

while True:
    clear()
    selection = input("""
    1) View list of builds
    2) Add to build list
    3) Search for name of build
    4) Delete build from build lsit
    5) Quit

    Enter your selection: """)

    if selection == "1":
        cursor.execute('SELECT * FROM Builds')
        for row in cursor.fetchall():
            print(row)
            t.sleep(15)
    elif selection == "2":
        build_name = input("Build name: ")
        weapon = input("Enter Weapon: ")
        aspect = input("Enter Aspect: ")
        keepsake = input("Enter Keepsakes: ")
        arcana = input("Enter Arcana: ")
        boons = input("Enter Boons: ")
        hammers = input("Enter Hammers: ")
        print("")
        new_data = (None, build_name, weapon, aspect, keepsake, arcana, boons, hammers)
        cursor.execute('INSERT INTO Names VALUES (?, ?, ?, ?, ?, ?, ?, ?)', new_data)
        bd.commit()
    elif selection == "3":
        search_term = input("Enter build to search: ")
        print("")
        cursor.execute('SELECT * FROM Builds WHERE Build = ?', (search_term,))
        for row in cursor.fetchall():
            print(row)
            t.sleep(15)
    elif selection == "4":
        id_to_delete = input("Enter build to delete: ")
        print("")
        cursor.execute('DELETE FROM Builds WHERE Buils = ?', (id_to_delete,))
        bd.commit()
    elif selection == "5":
        break
    else:
        print("")
        print("Incorrect selection, please try again.")
        print("")


bd.close()

我尝试使用许多其他在线教程,但没有一个起作用

python database sqlite sqlite3-python
1个回答
0
投票

您可以使用

pathlib.Path
检查文件是否存在。

from pathlib import Path

if __name__ == "__main__":
    path = Path("path/to/some/file.sqlite")
    if path.is_file():
        # do your thing
        print("Database exists and is a file.")

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.