Skip to content

BUG: Quote table names in to_sql #13206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
the4thv opened this issue May 17, 2016 · 10 comments
Closed

BUG: Quote table names in to_sql #13206

the4thv opened this issue May 17, 2016 · 10 comments
Labels
Enhancement IO SQL to_sql, read_sql, read_sql_query
Milestone

Comments

@the4thv
Copy link

the4thv commented May 17, 2016

xl.to_sql(con=conn, name='d1187b08-4943-4c8d-a7f6-6c06b7cb9509', flavor='mysql', if_exists='replace')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\core\generic.py", line 1165, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 571, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 1660, in to_sql
    dtype=dtype)
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 1417, in __init__
    super(SQLiteTable, self).__init__(*args, **kwargs)
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 669, in __init__
    self.table = self._create_table_setup()
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 1478, in _create_table_setup
    ',\n  '.join(create_tbl_stmts) + "\n)"]
  File "C:\Dev\Project\www\venv\project\lib\site-packages\pandas\io\sql.py", line 1363, in _get_valid_mysql_name
    raise ValueError("Invalid MySQL identifier '%s'" % uname)
ValueError: Invalid MySQL identifier '`d1187b08-4943-4c8d-a7f6-6c06b7cb9509`'

Expected Output

(no output, just successful write to db table)

output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.4.3.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 18.0.1
Cython: None
numpy: 1.11.0
scipy: None
statsmodels: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.4
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: 0.9.3
xlwt: 0.8.0
xlsxwriter: 0.7.3
lxml: None
bs4: 4.4.0
html5lib: 0.999999
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: 2.38.0
pandas_datareader: None

Proposed fix:

Line 1478 of pandas\io\sql.py, wrapping the escape(self.name) with ``:

create_stmts = ["CREATE TABLE " + escape(self.name) + " (\n" +
',\n '.join(create_tbl_stmts) + "\n)"]

@the4thv the4thv changed the title Allowing UUIDs as table names Allowing UUIDs as table names in .to_sql call May 17, 2016
@jorisvandenbossche
Copy link
Member

@the4thv that's indeed probably a good fix, however, note that the usage of the mysql flavor is deprecated and will probably be removed in the next release (0.19.0, but the fix could still be included in 0.18.2, but maybe not worth).
You should use an sqlalchemy engine instead for writing to mysql tables. Can you try that and see if this does work using an sqlalchemy engine?

@jorisvandenbossche jorisvandenbossche added the IO SQL to_sql, read_sql, read_sql_query label May 17, 2016
@the4thv
Copy link
Author

the4thv commented May 17, 2016

I can getting it working with sqlalchemy, thanks. 👍

@jorisvandenbossche
Copy link
Member

@the4thv I will leave this issue open as it is probably a problem for sqlite as well (and the support for this flavor without using sqlalchemy is to stay, only mysql will be removed)

PR with the fix is always welcome!

@jorisvandenbossche jorisvandenbossche changed the title Allowing UUIDs as table names in .to_sql call Quote table names in to_sql May 18, 2016
@the4thv
Copy link
Author

the4thv commented May 18, 2016

@jorisvandenbossche, I will give it a go at lunchtime.

@sudheesh001
Copy link

It looks like this issue is already fixed and good to close.

create_stmts = ["CREATE TABLE " + escape(self.name) + " (\n" +

@jorisvandenbossche
Copy link
Member

I don't think the proposed change in the top post is implemented. But in any case, best to add a test for this to check that it is actually solved.

Do you have a small reproducible example that shows it is working?

PRs always welcome to add a test case!

@jreback jreback added this to the 0.20.2 milestone May 23, 2017
@jreback jreback changed the title Quote table names in to_sql BUG: Quote table names in to_sql May 23, 2017
@jreback jreback modified the milestones: 0.20.2, 0.21.0 Jun 1, 2017
TomAugspurger pushed a commit that referenced this issue Jun 1, 2017
* Add test for bug #13206.

* Improve test by reading back the values from sql and comparing. Also fixes coding style violation.
@huguesv
Copy link
Contributor

huguesv commented Jun 2, 2017

I've added a test case in the PR referenced above that has now been merged. Was there anything else to do in order to close this issue?

@TomAugspurger
Copy link
Contributor

I believe so.

@jorisvandenbossche does that sound right? table names are quoted for sqlite, and everyone else should be using a sqlalchemy engine?

@jorisvandenbossche
Copy link
Member

Yes!

Kiv pushed a commit to Kiv/pandas that referenced this issue Jun 11, 2017
* Add test for bug pandas-dev#13206.

* Improve test by reading back the values from sql and comparing. Also fixes coding style violation.
stangirala pushed a commit to stangirala/pandas that referenced this issue Jun 11, 2017
* Add test for bug pandas-dev#13206.

* Improve test by reading back the values from sql and comparing. Also fixes coding style violation.
@gafortiby
Copy link

gafortiby commented Apr 30, 2019

This bug fix actually introduced a new bug for those of us using pandas with Netezza and jaydebeapi (which is treated like sqlite). What happens is that Netezza treats quotes as literal and injects these quotes into the table name, which then prevents other parts of the code from functioning properly.

EDIT: I found that if ALLCAPS are used for table names, it works fine even for Netezza and jaydebeapi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

No branches or pull requests

7 participants