-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DataFrame to_sql fails with percentage symbol %
in column name with SQLAlchemy and PostgreSQL
#11896
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
Comments
Hi, I encountered a similar issue using DataFrame.read_sql_query to read data from SQL. INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.14.32-xxxx-grs-ipv6-64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8
pandas: 0.17.1
nose: 1.3.7
pip: 7.1.2
setuptools: 3.3
Cython: None
numpy: 1.10.4
scipy: 0.16.1
statsmodels: None
IPython: 4.0.1
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: 1.0.11
pymysql: 0.6.7.None
psycopg2: None
Jinja2: None |
For me this (rather old) issue was due to an outdated MySQLdb. For me, simply running the following fixed the issue:
Your mileage may vary. I hope that saves someone an hour or two. |
This issue seems to still be there with PostgreSQL. Here is a code sample to test this (you'll need to provide your engine and create a schema "python_tests" or set the schema to "public" if you don't mind adding a table there). As a workaround I will obviously change "%" to "percent". It is actually probably a bad idea to have "%" in a column name anyways so I'd be fine if an error was thrown immediatly instead of pandas trying to save then failing and then only throwing the error. The reason why I'm writing this is because in my case I was working on a table with tens of thousands of rows and jupyter crashed (the % sign was causing a great amount of spam as I saw by running my script with IPython afterwards). 609 conn.execute(self.table.insert(data))
610
611 def insert_data(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\base.py in execute(self, object, *multiparams, **params)
943 raise exc.ObjectNotExecutableError(object)
944 else:
--> 945 return meth(self, multiparams, params)
946
947 def _execute_function(self, func, multiparams, params):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\sql\elements.py in _execute_on_connection(self, connection, multiparams, params)
261 def _execute_on_connection(self, connection, multiparams, params):
262 if self.supports_execution:
--> 263 return connection._execute_clauseelement(self, multiparams, params)
264 else:
265 raise exc.ObjectNotExecutableError(self)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\base.py in _execute_clauseelement(self, elem, multiparams, params)
1051 compiled_sql,
1052 distilled_params,
-> 1053 compiled_sql, distilled_params
1054 )
1055 if self._has_events or self.engine._has_events:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1187 parameters,
1188 cursor,
-> 1189 context)
1190
1191 if self._has_events or self.engine._has_events:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\base.py in _handle_dbapi_exception(self, e, statement, parameters, cursor, context)
1400 util.raise_from_cause(
1401 sqlalchemy_exception,
-> 1402 exc_info
1403 )
1404 else:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\util\compat.py in raise_from_cause(exception, exc_info)
201 exc_type, exc_value, exc_tb = exc_info
202 cause = exc_value if exc_value is not exception else None
--> 203 reraise(type(exception), exception, tb=exc_tb, cause=cause)
204
205 if py3k:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\util\compat.py in reraise(tp, value, tb, cause)
184 value.__cause__ = cause
185 if value.__traceback__ is not tb:
--> 186 raise value.with_traceback(tb)
187 raise value
188
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\base.py in _execute_context(self, dialect, constructor, statement, parameters, *args)
1180 statement,
1181 parameters,
-> 1182 context)
1183 except BaseException as e:
1184 self._handle_dbapi_exception(
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sqlalchemy\engine\default.py in do_execute(self, cursor, statement, parameters, context)
468
469 def do_execute(self, cursor, statement, parameters, context=None):
--> 470 cursor.execute(statement, parameters)
471
472 def do_execute_no_params(self, cursor, statement, context=None):
ProgrammingError: (psycopg2.ProgrammingError) incomplete placeholder: '%(' without ')' [SQL: 'INSERT INTO python_tests.pg_table_with_percent ("Date", "Holiday Name", "%% worked", "Weekday") VALUES (%(Date_m0)s, %(Holiday Name_m0)s, %(% worked_m0)s, %(Weekday_m0)s)'] [parameters: {'Date_m0': datetime.datetime(2019, 1, 1, 0, 0), 'Holiday Name_m0': 'New Year', '% worked_m0': 0, 'Weekday_m0': 'Tuesday'}]
">
|
%
in column name with SQLAlchemy and MySQL%
in column name with SQLAlchemy and PostgreSQL
I encountered the same issue using pandas 1.14. |
The following call to
to_sql
fails due to the percentage (%) symbol in the column names in pandas 0.17.1.Backtrace:
Versions:
The text was updated successfully, but these errors were encountered: