SQLite Forum

Variable input for sqlite3.connect() Python
Login

Variable input for sqlite3.connect() Python

(1) By Clebreak on 2021-02-22 21:15:30 [source]

Hello everybody,

I'm trying to add a variable to the input of the command sqlite.connect() in python but it didn't work.

For example :

database = 'test.tooldb'

data = sqlite3.connect(database)

I have got an error : "unable to open database file". If I replace database by 'test.tooldb',it works.

Is it possible to do that with a variable ?

Thanks in advice for your help.

Clément

(2) By Larry Brasfield (larrybr) on 2021-02-22 21:34:00 in reply to 1 [link] [source]

Yes, it is possible. To wit: >>> import sqlite3 >>> myDb = '/Tmp/trash.sdb' >>> conn = sqlite3.connect(myDb) >>> How does your code differ from the above (beside name choices)?

(6) By Clebreak on 2021-02-22 22:43:16 in reply to 2 [link] [source]

Thanks for your reply !

I forget to mentioned that I get the variable form a text file with open('text.txt','r')

(7) By Keith Medcalf (kmedcalf) on 2021-02-23 00:56:16 in reply to 6 [link] [source]

Are you sure that the variable contents so read are an acceptable filename and does not contain extraneous characters not permitted in a filename by your Operating System?

(8) By Clebreak on 2021-02-23 07:42:15 in reply to 7 [link] [source]

Hi,

Here is what the txt file contain :

database.tooldb

I use these commands to read the txt file :

f=open('text.txt','r')

path=f.readline()

conn = sqlite3.connect(path)

(9.1) By Larry Brasfield (larrybr) on 2021-02-23 16:07:46 edited from 9.0 in reply to 8 [link] [source]

The readline() method leaves a newline on the end of its returned string. If you make your last line read thusly: conn = sqlite3.connect(path.strip()) , your present trouble will be past.

I should add that Keith put his finger on the problem, asking:

Are you sure that the variable contents so read are an acceptable filename and does not contain extraneous characters not permitted in a filename by your Operating System?

I merely took that inquiry to its logical conclusion.

(10) By Clebreak on 2021-02-23 09:28:35 in reply to 9.0 [link] [source]

It works ! Thanks for your help

Have a nice day

Clément

(3.1) By Keith Medcalf (kmedcalf) on 2021-02-22 22:00:17 edited from 3.0 in reply to 1 [link] [source]

You did not mention the version of Python nor of pysqlite2 (sqlite3 wrapper) nor the operating system on which you made this observation. You will note, however, that the parameter name for the database is "database".

So, if you want to use the local variable name "database" to be the referenced database parameter, then perhaps you should be using properly qualified names, as in:

data = sqlite3.connect(database=database)

You will note that your original (positional) statement

data = sqlite3.connect(database)

works using the same (as distributed) version 2.6.0 of pysqlite2 (sqlite3 wrapper) on Python 2.7.18, 3.7.8, 3.8.7; and, 3.9.1 on Windows 10.

(4) By Larry Brasfield (larrybr) on 2021-02-22 21:51:42 in reply to 3.0 [link] [source]

data = sqlite3.connect(database=database)

While this may be confusing to to some human readers, the Python parser has no trouble with it. The first "database" names a parameter; the second provides a value for that parameter.

I guess I don't see what the OP did wrong or how you have seen that.

(5) By Keith Medcalf (kmedcalf) on 2021-02-22 21:58:30 in reply to 4 [link] [source]

I do not see what is wrong either, but as for finding out the parameter names:

>>> import sqlite3
>>> help(sqlite3.connect)
Help on built-in function connect in module _sqlite3:

connect(...)
    connect(database[, timeout, detect_types, isolation_level,
            check_same_thread, factory, cached_statements, uri])

    Opens a connection to the SQLite database file *database*. You can use
    ":memory:" to open a database connection to a database that resides in
    RAM instead of on disk.