Fixing SQLite Database is Locked Error when using jupyter notebook
初学sql,使用jupyter notebook写入门的SQL语句修改sqlite下的database文档,调试以下代码时:
path = '../code3/mbox-short.txt'
db = 'sql1.db'
fh = open(path)
conn = sqlite3.connect(db)
cur = conn.cursor()
for line in fh:
if not line.startswith('From: '): continue
pieces = line.split()
email = pieces[1]
cur.execute('SELECT count FROM Counts WHERE email = ?', (email,))#read the record
#? to avoid sql injection;#','make it a one item tuple
row = cur.fetchone()#the information we get from the database, if not in, row will be none
if row is None:
cur.execute('INSERT INTO Counts (email, count) VALUES (?,1)', (email,))
else:
cur.execute('UPDATE Counts SET count = count + 1 WHERE email = ?', (email,))#update is better
conn.commit(