1

Here is my code (currently):

conn = sqlite3.connect(db)
conn.text_factory = str  #bugger 8-bit bytestrings
cur = conn.cursor()

reader = csv.reader(open(csvfile, "rU"), delimiter = '\t')
for Number, Name, Message, Datetime, Type in reader:

    # populate subscriber table
    if str(Number)[:1] == '1':
        tmpNumber = str(Number)[1:]
        Number = int(tmpNumber)
    cur.execute('INSERT OR IGNORE INTO subscriber (name, phone_number) VALUES (?,?)', (Name, Number))

cur.close()
conn.close()

Here is some sample data from the csv file (tab-delimited in the file):

Number       Name        Message         Date/Time               Type
16665551212  Jane Doe    message one     11/23/2011 6:34:44 AM   Incoming
16665551212  Jane Doe    message two     11/23/2011 4:53:21 PM   Incoming

The code executes without any errors, only nothing is written to the database. Why is this?

4

1 回答 1

4

You need to commit your changes: conn.commit()

If you do not want to use transactions at all, i.e. work in "autocommit" mode, use isolation_level=None in the connect() call. However, this is usually a bad idea.

于 2012-06-12T20:22:03.207 回答