python - 无法使用python在sqlite3中创建数据库

标签 python sqlite

我一直在尝试按照 sqlite-python 教程网站上的示例在磁盘(C: 驱动器)上的 sqlite3 中创建数据库,但不断收到错误“无法打开数据库文件”。如果我在项目文件夹中创建文件(我使用的是 Pycharm),则会创建数据库文件。我不明白为什么我无法在磁盘上创建它。我的代码如下:

import sqlite3
from sqlite3 import Error


def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by the db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)

    return None


def main():
    database_src="C:\sqlite\db\store.db"
    create_connection(database_src)

if __name__ == '__main__':
    main()

最佳答案

对于 Windows 风格的路径,您需要正确转义 \ 字符。

database_src="C:\\sqlite\\db\\store.db"

\\ 字符与 \s 不同。

关于python - 无法使用python在sqlite3中创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54766250/

相关文章:

PHP 和 SQlite : Login

android - 从 sqlite3 DB 中删除不在对象列表中的行

python - 加速 cython 代码

python - 属性错误 : 'Series' object has no attribute 'searchsorted' pandas

python - 在 Python BaseHTTPServer 中检测浏览器何时命中 "refresh"

mysql - mysql 或 sqlite 中的深度优先搜索 (DFS)

java - sqlite 数据库错误 : sqlite returned: error code = 1, msg = 没有这样的列:ph_number

python - Matplotlib autodatelocator 自定义日期格式?

python - 如何使用 TensorFlow 将值列表放入张量的给定位置

android - 在事务中使用多个方法