使用Python创建到sqlite数据库的连接

cgvd09ve  于 2023-06-06  发布在  SQLite
关注(0)|答案(2)|浏览(197)

我试图使用Python创建到sqlite数据库的连接,我不知道为什么它不工作。我已经下载了Sqlite浏览器。下面列出了我的代码。
我知道一旦你使用conn=sqlite3.connect("Address.db"),它应该隐式地创建一个新的数据库,如果当前不存在的话。然而,一旦我保存文件,我找不到相应的数据库文件。

from tkinter import *

from PIL import ImageTk,Image

import sqlite3

root=Tk()

root.title("Hello")

root.geometry("400x400")

conn=sqlite3.connect(path+"Address.db")   #also tried using just ("Address.db")
try:
        c=conn.cursor()

        c.execute('''CREATE TABLE contacts (     
        fName TEXT,
        lName text,
        address text,
        city text,
        state text,
        zipCode integer);''')
        print ('table created successfully')
except:
        print ('error in operation')  
        conn.rollback()

conn.close()
root.mainloop()
3duebb1j

3duebb1j1#

试试这个,应该会给予你SQLite版本和你工作脚本的路径。

import sqlite3
from sqlite3 import Error
import os

conn = sqlite3.connect("Address.db")
cwd = os.getcwd()
print("pysqlite version", sqlite3.version)
print("sqlite version", sqlite3.sqlite_version)
print("path", cwd)
conn.close()
gv8xihay

gv8xihay2#

也许你正在使用一个IDE,而你的代码是从一个与你期望的不同的目录运行的。
尝试添加此行,以便在访问数据库之前立即打印工作目录:

import os
print(f'Directory: {os.getcwd()}')

相关问题