Hello

        It's probably trivial but I'm having a difficult time using Python
to import a tab-delimited file into SQLite because some columns might
be empty, so the INSERT string should contain NULL instead of "NULL".

Does someone have working code handy that can parse through each line,
check each column, and if found empty, build the right INSERT?

Here's my newbie, yucckie, non-working code:

======
import csv

cr = csv.reader(open("test.tsv","rb"),dialect='excel-tab')

for row in cr:
        #INSERT INTO mytable (col1,col2) VALUES (NULL,"My string")

        sql = "INSERT INTO mytable (col1,col2) VALUES ("
        for col in row:
                if col=="":
                        sql = sql + "NULL"
                else:
                        sql = sql + col
                if not last col:
                        sql = sql + ","
                else:
                        #remove trailing comma
======

Thank you for any tip.

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to