csv2sqlite
csv2sqlite copied to clipboard
Initialization?
I'm experiencing an interesting issue. Basically, I have incorporated this module into a larger Python program (or more accurately, I use the "orchestrating" program to issue a command-line call to run this Python program), and I am creating four tables, which is done in a loop. The name I specify for the database file is typically of a file that has not yet been created, e.g. "test_database.db". Because of the way Python's sqlite3 library works, when a database file is connected to it will use the following logic:
-
If the database file exists and it's in the current path (or the full path has been specified), connect to it.
-
If the database file does not exist, create it and connect to it.
Now, for each run of my overall program, this Python program is run four times, one for each table. For the first run of this, the file will be created, obviously. So far so good, right?
The next step of my overall program is to run a SQL query on this newly created db file. In a separate function, a new connection is made to the database and then a query is executed on it. However, if the database file was just created in this run of the overall program, I will get an error saying that whatever table I was running a query on was not found, i.e.
sqlite3.OperationalError: no such table: <table_name>
However, if I run my overall program again, with the same table name, I will get the expected results. The thing is, for the purposes of this program I would like to delete the table file each time I run the program if it exists, so that it does not keep growing and growing with the same tables over and over again each time it is run.
I would like to find a way to run my overall program--which consists of creating a DB file and adding the tables to it using csv2sqlite and THEN running some SQL queries on it--in a single go without running into an error.
Has anyone else experienced anything like this? If so, how did you solve it?
Thanks in advance.
What I'm wondering is if there may be a memory leak somewhere in csv2sqlite. I know from experience that unusual behaviour can occur when streams aren't properly closed.
OK, I believe it may be a timing issue, actually! I'm now trying a 10-second sleep between making the database file from the CSV files and then querying it, and it seems to work correctly, although I did still once see that error again.
I'll play around with this some more, and update this accordingly.