简体   繁体   中英

Unable to create database in sqlite3 with python

I have been trying to create a database in sqlite3 on the disk (C: drive) following the example on the sqlite-python tutorial site, but keep getting the error "unable to open database file". If i create the file in my project folder (I'm using Pycharm), the database file is created. I don't understand why i couldn't create it on the disk. my code is as follow:

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()

For Windows-style pathing, you need to escape your \\ characters properly.

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

The \\\\ character is different than \\s .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM