mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 18:04:32 +02:00
CLI now supports upsert/insert - closes #115
This commit is contained in:
parent
8eaac7c5f1
commit
03ee97d225
3 changed files with 32 additions and 3 deletions
|
|
@ -669,14 +669,19 @@ 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 = iter(db.conn.execute(sql))
|
||||
headers = [c[0] for c in cursor.description]
|
||||
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([c[0] for c in cursor.description])
|
||||
writer.writerow(headers)
|
||||
for row in cursor:
|
||||
writer.writerow(row)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue