gpt4 book ai didi

python sqlite3.connect - 无法打开数据库文件

转载 作者:行者123 更新时间:2023-12-01 01:59:01 24 4
gpt4 key购买 nike

我正在 Mac 上运行 Python3 测试简单的 sql 数据库。我有以下代码

import sqlite3

# connecting to the database
connection = sqlite3.connect("myTable.db")
crsr = connection.cursor()

# SQL command to create a table in the database
sql_command = """CREATE TABLE emp (
staff_number INTEGER PRIMARY KEY,
fname VARCHAR(20),
lname VARCHAR(30),
gender CHAR(1),
joining DATE);"""

# execute the statement
crsr.execute(sql_command)

# SQL command to insert the data in the table
sql_command = """INSERT INTO emp VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");"""
crsr.execute(sql_command)

crsr.execute(sql_command)
connection.commit()
connection.close()

当我运行此代码时,出现错误:

Traceback (most recent call last):
File "test.py", line 8, in <module>
connection = sqlite3.connect("myTable.db")
sqlite3.OperationalError: unable to open database file

我错过了什么?我尝试将 ("myTable.db") 替换为 (".myTable.db")("./myTable.db")但同样的问题。请建议。

最佳答案

更改 SQLite 数据库文件的权限。

Ubuntu

sudo chmod 775 /FolderOfSQLiteDBfile/
sudo chmod 664 /FolderOfSQLiteDBfile/sqlite.db

窗口

chmod 775 /FolderOfSQLiteDBfile/

引用:sqlite-error-unable-to-open-database-file

关于python sqlite3.connect - 无法打开数据库文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49886483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com