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

2024-01-17

我正在 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") with (".myTable.db") and ("./myTable.db")但同样的问题。请建议。


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

Ubuntu

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

Windows

chmod 775 /FolderOfSQLiteDBfile/

Refer : sqlite 错误无法打开数据库文件 https://serverfault.com/questions/57596/why-do-i-get-sqlite-error-unable-to-open-database-file

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python sqlite3.connect - 无法打开数据库文件 的相关文章

随机推荐