简体   繁体   English

为什么没有写入数据库?

[英]Why is nothing being written to the database?

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): 以下是来自csv文件的一些示例数据(在文件中以制表符分隔):

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? 为什么是这样?

You need to commit your changes: conn.commit() 您需要提交更改: conn.commit()

If you do not want to use transactions at all, ie work in "autocommit" mode, use isolation_level=None in the connect() call. 如果您根本不想使用事务,即在“自动提交”模式下工作,请在connect()调用中使用isolation_level=None However, this is usually a bad idea. 但是,这通常是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM