Python sqlite error: ValueError: database parameter must be string or APSW Connection object

Question:

I am trying to create, edit and read from a sqlite file using a python script. My code is a server client model with the client writing to the database on receiving a command from the server.

Each command from the server is received on a separate thread to provide for parallel operation.

The client never restarts unless the system reboots but the server program is launched when needed by the user.

Now my problem arises because sqlite for python is not thread safe. So I have a consumer queue to the database for all write operations.

I cannot provide the code because it is really long and is very har to decouple and provide a complete working copy.

But a snippet of the code where the error is :

def writedata(self, _arg1, _arg2, _arg3):      
     # self.sql_report is the fully qualified path of sqlite file
    db = sqlite3.connect(self.sql_report)
    c = db.cursor()  
    res = c.execute("Select id from State")
    listRowId = []
    for element in res.fetchall():
        listRowId.append(element[0])
    self.currentState = max(listRowId)  

    sql = "INSERT INTO Analysis (type, reason, Component_id, State_id) VALUES (?, ?, ifnull((SELECT id from `Component` WHERE name = ? LIMIT 1), 1), ?)"
    # call to the write queue.

    Report.strReference.writeToDb(sql, [(_arg1, _arg3, _arg2, self.currentState)])

The error I am receiving is

File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "/usr/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "/nameoffile", line 357, in nameofmethod
Report().writedata("test","text","analysis")
File "./nameofscript/Report.py", line 81, in writedata
ValueError: database parameter must be string or APSW Connection object

line 81: here is:

 #first line of the snippet code pasted above
 db = sqlite3.connect(self.sql_report)

I don’t know why this error comes up. One point to be noted though is that this error comes up only after the server is run for a few times.

Asked By: HighonH

||

Answers:

The error is exactly what it says. You are passing self.sql_report as the string database filename to use, but at the time of making the call it is not a string.

You’ll need to find out what it really is, which is standard Python debugging. Use whatever you normally use for that. Here is also a suggestion that will print what it is and drop into an interactive debugger so you can examine further.


try:
db = sqlite3.connect(self.sql_report)
except ValueError:
print (repr(self.sql_report))
import pdb; pdb.set_trace()
raise

Answered By: Roger Binns
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.