mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 20:34:12 +02:00
Run sqlite-utils query in a transaction so that DML is committed
The failing test I added in the previous commit now passes. Diff best viewed with whitespace ignored (git diff/show -w), as most of the change is indentation for the new "with" block.
This commit is contained in:
parent
52418cea80
commit
6a660d12a2
1 changed files with 19 additions and 18 deletions
|
|
@ -669,24 +669,25 @@ def drop_view(path, view):
|
||||||
def query(path, sql, nl, arrays, csv, no_headers, table, fmt, json_cols):
|
def query(path, sql, nl, arrays, csv, no_headers, table, fmt, json_cols):
|
||||||
"Execute SQL query and return the results as JSON"
|
"Execute SQL query and return the results as JSON"
|
||||||
db = sqlite_utils.Database(path)
|
db = sqlite_utils.Database(path)
|
||||||
cursor = db.conn.execute(sql)
|
with db.conn:
|
||||||
if cursor.description is None:
|
cursor = db.conn.execute(sql)
|
||||||
# This was an update/insert
|
if cursor.description is None:
|
||||||
headers = ["rows_affected"]
|
# This was an update/insert
|
||||||
cursor = [[cursor.rowcount]]
|
headers = ["rows_affected"]
|
||||||
else:
|
cursor = [[cursor.rowcount]]
|
||||||
headers = [c[0] for c in cursor.description]
|
else:
|
||||||
if table:
|
headers = [c[0] for c in cursor.description]
|
||||||
print(tabulate.tabulate(list(cursor), headers=headers, tablefmt=fmt))
|
if table:
|
||||||
elif csv:
|
print(tabulate.tabulate(list(cursor), headers=headers, tablefmt=fmt))
|
||||||
writer = csv_std.writer(sys.stdout)
|
elif csv:
|
||||||
if not no_headers:
|
writer = csv_std.writer(sys.stdout)
|
||||||
writer.writerow(headers)
|
if not no_headers:
|
||||||
for row in cursor:
|
writer.writerow(headers)
|
||||||
writer.writerow(row)
|
for row in cursor:
|
||||||
else:
|
writer.writerow(row)
|
||||||
for line in output_rows(cursor, headers, nl, arrays, json_cols):
|
else:
|
||||||
click.echo(line)
|
for line in output_rows(cursor, headers, nl, arrays, json_cols):
|
||||||
|
click.echo(line)
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue