数据仍未插入SQLITE DB?

时间:2017-01-25 02:20:57

标签: python sqlite

    import sqlite3
conn = sqlite3.connect('./sampledb.db')
print("Opened database successfully")
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS person (name TEXT, number INTEGER)')
print("Created table");
cur.execute('INSERT INTO person (name, number) VALUES (\'sam\', 15 )')
print('Inserted data')
cur.close()

我正在使用python; 这将创建数据库表,甚至在INSERT 之后打印

 print('Inserted data')

但是仍然没有数据,出了什么问题?

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须提交更改才能反映在数据库中。

import sqlite3
conn = sqlite3.connect('./sampledb.db')
print("Opened database successfully")
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS person (name TEXT, number INTEGER)')
print("Created table");
cur.execute('INSERT INTO person (name, number) VALUES (\'sam\', 15 )')
print('Inserted data')
conn.commit()
cur.close()