Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@takhogan
Copy link
Contributor

@takhogan takhogan commented Apr 26, 2025

Hi! I've added .sql to the export formats and added tests

This is useful especially with the records library https://pypi.org/project/records/ which uses this library to create exports.

examples:

import tablib

with open('tests/files/founders.xlsx', 'rb') as f:
    data = tablib.Dataset().load(f, 'xlsx')

sql_output = data.export('sql', table='founders')
print(sql_output) 

"""
INSERT INTO "founders" VALUES ('John', 'Adams', 90);
INSERT INTO "founders" VALUES ('George', 'Washington', 67);
INSERT INTO "founders" VALUES ('Thomas', 'Jefferson', 50);
"""

ds = tablib.Dataset(title='tbl')
ds.headers = ['col_date', 'col_timestamp']
ds.append([
    dt.date(2020, 1, 2),
    dt.datetime(2020, 1, 2, 3, 4, 5)
])
sql = ds.export('sql')
print(sql)

"""
INSERT INTO "tbl" VALUES (DATE '2020-01-02', TIMESTAMP '2020-01-02 03:04:05');
"""

Copy link
Contributor

@claudep claudep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think this is a nice addition to this lib.

"""
tbl = table or getattr(dataset, 'title', None) or 'EXPORT_TABLE'
tbl_ident = cls._quote_ident(str(tbl))
tbl_ident = str(tbl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your rationale for not quoting table/column names? Typically, I think that for example on PostgreSQL uppercase names should be quoted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi when the table name is in quotes then you're not able to reference schema names like schema_name.table_name so "schema_name.table_name" doesn't work. Seems like you can have syntaxes like "Schema Name"."table name" as well. I think the user should just pass the quotes into the title if they require it instead of having it as default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe _quote_ident could smartly detect the "schema_name.table_name" case? In any case, having the default EXPORT_TABLE unquoted looks like it will hurt many.

Copy link
Contributor Author

@takhogan takhogan May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that's true, I'll change EXPORT_TABLE to be lowercase export_table.

I looked into it some more and it looks like by default MySQL doesn't support quotes for table names so it needs to be the plain table name to work with all formats. Seems like MySQL uses backticks `` instead of quotes so INSERT INTO "TBL" VALUES (1, 'value'); will raise a syntax error with MySQL

@claudep
Copy link
Contributor

claudep commented May 14, 2025

@hugovk Could you quickly make a final review?

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, a couple of suggestions.

Comment on lines +29 to +31
except Exception:
pass
return 'NULL'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What might trigger this? Can we have a more specific exception here? Can it be covered by tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'm not sure. It would have to be something obscure like a class that subclasses the float type and throws an exception in it's repr method

@claudep claudep merged commit 8dcb87f into jazzband:master May 20, 2025
22 checks passed
@claudep
Copy link
Contributor

claudep commented May 20, 2025

Thanks a lot for this contribution 😍

@hugovk hugovk added the changelog: Added For new features label Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Added For new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants