mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 09:54:31 +02:00
sqlite-utils query can now run DML (#120)
* Failing test showing that DML in `sqlite-utils query` doesn't work * Run `sqlite-utils query` in a transaction so that DML is committed Thanks, @tsibley!
This commit is contained in:
parent
d0cdaaaf00
commit
f8277d0fb9
2 changed files with 22 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):
|
||||
"Execute SQL query and return the results as JSON"
|
||||
db = sqlite_utils.Database(path)
|
||||
cursor = db.conn.execute(sql)
|
||||
if cursor.description is None:
|
||||
# This was an update/insert
|
||||
headers = ["rows_affected"]
|
||||
cursor = [[cursor.rowcount]]
|
||||
else:
|
||||
headers = [c[0] for c in cursor.description]
|
||||
if table:
|
||||
print(tabulate.tabulate(list(cursor), headers=headers, tablefmt=fmt))
|
||||
elif csv:
|
||||
writer = csv_std.writer(sys.stdout)
|
||||
if not no_headers:
|
||||
writer.writerow(headers)
|
||||
for row in cursor:
|
||||
writer.writerow(row)
|
||||
else:
|
||||
for line in output_rows(cursor, headers, nl, arrays, json_cols):
|
||||
click.echo(line)
|
||||
with db.conn:
|
||||
cursor = db.conn.execute(sql)
|
||||
if cursor.description is None:
|
||||
# This was an update/insert
|
||||
headers = ["rows_affected"]
|
||||
cursor = [[cursor.rowcount]]
|
||||
else:
|
||||
headers = [c[0] for c in cursor.description]
|
||||
if table:
|
||||
print(tabulate.tabulate(list(cursor), headers=headers, tablefmt=fmt))
|
||||
elif csv:
|
||||
writer = csv_std.writer(sys.stdout)
|
||||
if not no_headers:
|
||||
writer.writerow(headers)
|
||||
for row in cursor:
|
||||
writer.writerow(row)
|
||||
else:
|
||||
for line in output_rows(cursor, headers, nl, arrays, json_cols):
|
||||
click.echo(line)
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue