kennethreitz / records

SQL for Humans™
https://pypi.python.org/pypi/records/
ISC License
7.16k stars 574 forks source link

When I use query('insert into xxx'), it does not insert successfully. #224

Open SeasonPanPan opened 3 weeks ago

SeasonPanPan commented 3 weeks ago

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.