diff --git a/changelog.md b/changelog.md index 2c433c35..49eb62d6 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ Features Bug Fixes -------- * Better respect case when `keyword_casing` is `auto`. +* Fix error when selecting from an empty table. * Let favorite queries contain special commands. * Render binary values more consistently as hex literals. diff --git a/mycli/main.py b/mycli/main.py index 9514f613..060c3a83 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1422,8 +1422,11 @@ def get_col_type(col) -> type: col_type = FIELD_TYPES.get(col[1], str) return col_type if type(col_type) is type else str - column_types = [get_col_type(tup) for tup in cur.description] - colalign = [numeric_alignment if x in (int, float, Decimal) else 'left' for x in column_types] + if cur.rowcount > 0: + column_types = [get_col_type(tup) for tup in cur.description] + colalign = [numeric_alignment if x in (int, float, Decimal) else 'left' for x in column_types] + else: + column_types, colalign = [], [] if max_width is not None and isinstance(cur, Cursor): cur = list(cur) diff --git a/test/test_main.py b/test/test_main.py index 22ab2c99..451277a4 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -40,6 +40,20 @@ ] +@dbtest +def test_select_from_empty_table(executor): + run(executor, """create table t1(id int)""") + sql = "select * from t1" + runner = CliRunner() + result = runner.invoke(cli, args=CLI_ARGS + ["-t"], input=sql) + expected = dedent("""\ + +----+ + | id | + +----+ + +----+""") + assert expected in result.output + + def test_is_valid_connection_scheme_valid(executor, capsys): is_valid, scheme = is_valid_connection_scheme("mysql://test@localhost:3306/dev") assert is_valid