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

时间:2018-04-17 19:57:45

标签: python sqlite

我在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")但同样的问题。请建议。

1 个答案:

答案 0 :(得分:1)

更改SQLite db文件的权限。

Ubuntu的

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

chmod 775 /FolderOfSQLiteDBfile/

参考:sqlite-error-unable-to-open-database-file