When I use query('insert into xxx'), it does not insert successfully.
Author: SeasonPanPanCreated Oct 6, 2024Updated May 25, 2026
database: sqlite (also tested in mysql)
create_sql = 'create table test(`id` INT PRIMARY KEY NOT NULL, `name` CHAR(32) NOT NULL)'
insert_sql = 'insert into test(id, name) values (1, "Jim")'
select_sql = 'select * from test;'
delete_sql = 'delete from test;'
db = records.Database('sqlite:///test.db')
db.query(insert_sql)
res = db.query(select_sql)
print(res.all())The output is "[]"
I read the source code, and find that if only I use db.transaction(), it can work well.
with db.transaction() as tx:
resi = tx.query(insert_sql)
print(resi)
res = db.query(select_sql)
print(res.all())So, is this a problem with my usage? Insert operation does not supported in query method?
I searched many blogs, and they write 'db.query(insert_sql)', no one writes 'with db.transaction() as tx', I'm confused.
Source: kennethreitz/records