[Tutor] make a sqlite3 database from an ordinary text file
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Jun 19 14:04:12 EDT 2022
On 19/06/2022 18:11, Manprit Singh wrote:
> cur.execute("create table workdata(Time INT, Pieces INT)")
This will fail the second time it runs. You should check
if the table already exists and either drop it or use
the existing table.
However, I rarely create tables from Python, it's generally
easier to create a sql file and run that directly using
the sqlite interpreter. You only need Python if the format
of the table has to be deduced from the data. and that then
leads to all sorts of follow-on issues over accessing names
of columns etc.
Once you have the table you can then use Python to load
the data, although in your case you could do that directly
from SQL too since it looks like a tab separated file.
As always use the most appropriate tool. If you can do it
directly in SQL there is no point in wrapping it in
Python. That just adds complexity and slows things
down.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list