Good question, note, read_sql is a wrapper around 'read_sql_table and read_sql_query. Reading through the source, a ValueError is consistently thrown inside the parent and the helper functions. So you can safely catch a ValueError and handle appropriately. (Do have a look at the source)

Answer from parsethis on Stack Overflow
🌐
GitHub
github.com › pandas-dev › pandas › issues › 7730
read_sql function eats informative error messages. · Issue #7730 · pandas-dev/pandas
July 11, 2014 - Consider the following code. import pandas as pd import psycopg2 bad_query = 'select bad_column from my_table;' conn = psycopg2.connect(...) pd.read_sql(bad_query, conn) Running the above in my environment produces a DatabaseError except...
Author   chrisgrimm
Discussions

pandas.read_sql() fails when the select is not the sole part of the query
Code Sample, a copy-pastable example if possible import pandas as pd import pyodbc import sqlalchemy as sa import urllib query = """ drop table if exists #MyTempTable; select 1 ID , 'A' Letter into #MyTempTable union select 2, 'B' union ... More on github.com
🌐 github.com
3
December 2, 2019
read_sql says wrong syntax when pandas read_sql can work with the same syntax
read_sql says wrong syntax when pandas read_sql can work with the same syntax More on github.com
🌐 github.com
14
June 18, 2019
python - Handling errors produced by incorrect queries using psycopg2 and pandas - Stack Overflow
I have a function that queries a postgres server using psycopg2 and returns a pandas dataframe back. There are two major issues that can cause the function to fail. 1) the server is down - This g... More on stackoverflow.com
🌐 stackoverflow.com
python - to_sql pandas data frame into SQL server error: DatabaseError - Stack Overflow
While trying to write a pandas' dataframe into sql-server, I get this error: DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.errors.DatabaseError.html
pandas.errors.DatabaseError — pandas ain documentation
Error is raised when executing SQL with bad syntax or SQL that throws an error. Raised by pandas.read_sql() when a bad SQL statement is passed in.
🌐
Like Geeks
likegeeks.com › home › python › pandas › pandas read_sql() common errors & solutions
Pandas read_sql() Common Errors & Solutions
These errors indicate that there is a problem with the database server or the connection details provided are incorrect. Here’s an example where you attempt to connect to a database to retrieve information: from sqlalchemy import create_engine import pandas as pd database_uri = 'postgresql://username:password@localhost:5432/telecom_db' engine = create_engine(database_uri) query = 'SELECT * FROM customers LIMIT 5;' try: df = pd.read_sql(query, engine) print(df) except Exception as e: print(f'Error: {e}')
🌐
GitHub
github.com › pandas-dev › pandas › issues › 29974
pandas.read_sql() fails when the select is not the sole part of the query · Issue #29974 · pandas-dev/pandas
December 2, 2019 - There was an error while loading. Please reload this page. ... import pandas as pd import pyodbc import sqlalchemy as sa import urllib query = """ drop table if exists #MyTempTable; select 1 ID , 'A' Letter into #MyTempTable union select 2, 'B' union select 3, 'C' union select 4, 'D' union select 5, 'E' ; select * from #MyTempTable """ connection_string = urllib.parse.quote_plus( 'DRIVER={SQL Server Native Client 11.0};\ SERVER={0},{1};\ trusted_Connection=yes\ ;encrypt=yes;\ trustServerCertificate=yes;'.format(server, port) ) eng = sa.create_engine('mssql+pyodbc:///?odbc_connect={0}'.format(connection_string)) df = pd.read_sql(query, con=eng)
Author   djrscally
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.errors.DatabaseError.html
pandas.errors.DatabaseError — pandas 3.0.1 documentation
Error is raised when executing SQL with bad syntax or SQL that throws an error. Raised by pandas.read_sql() when a bad SQL statement is passed in.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.read_sql.html
pandas.read_sql — pandas 2.2.3 documentation - PyData |
Read data from SQL via either a SQL query or a SQL tablename. When using a SQLite database only SQL queries are accepted, providing only the SQL tablename will result in an error.
Find elsewhere
🌐
GitHub
github.com › pandas-dev › pandas › issues › 7826
read_sql chokes on mysql when using labels with queries due to unnecessary quoting · Issue #7826 · pandas-dev/pandas
engine=create_engine('mysql://{username}:{password}@{host}/{database}?charset=utf8'.format(**db)) pandas.io.sql.read_sql('SELECT onlinetransactions.id FROM onlinetransactions LIMIT 1', engine) #Does what you'd expect pandas.io.sql.read_sql('SELECT onlinetransactions.id as firstid FROM onlinetransactions LIMIT 1', engine) #Fails · The error you get back is: C:\Anaconda\lib\site-packages\pandas\io\sql.pyc in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns) 421 coerce_float=coerce_float, parse_dates=parse_dates) 422 --> 423 if pandas_sql.has_table(sql): 424 pandas_sql.me
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_sql.html
pandas.read_sql — pandas 3.0.1 documentation - PyData |
Read data from SQL via either a SQL query or a SQL tablename. When using a SQLite database only SQL queries are accepted, providing only the SQL tablename will result in an error.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 3745
BUG: handle no rows returns from a sql query · Issue #3745 · pandas-dev/pandas
June 3, 2013 - In [1]: import pandas as pd In [2]: import sqlite3 In [3]: import sqlalchemy In [4]: pd.__version__ Out[4]: '0.14.1-474-g744daa7' In [5]: con = sqlite3.connect(':memory:') In [6]: engine = sqlalchemy.create_engine('sqlite:///:memory:') In [7]: df = pd.DataFrame({'a':[1,2,3], 'b':[2,3,4]}) In [8]: df.to_sql('test', engine, index=False, if_exists='replace') In [9]: df.to_sql('test', con, index=False, if_exists='replace') In [10]: pd.read_sql_query("SELECT * FROM test WHERE a < 0", engine) Out[10]: Empty DataFrame Columns: [a, b] Index: [] In [11]: pd.read_sql_query("SELECT * FROM test WHERE a < 0", con) Out[11]: Empty DataFrame Columns: [a, b] Index: []
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.read_sql.html
pandas.read_sql — pandas 3.0.0rc0+27.g47fea804d6 documentation
Read data from SQL via either a SQL query or a SQL tablename. When using a SQLite database only SQL queries are accepted, providing only the SQL tablename will result in an error.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.20 › generated › pandas.read_sql_query.html
pandas.read_sql_query — pandas 0.20.3 documentation
Comparison with SQL · Comparison with SAS · API Reference · Internals · Release Notes · Enter search terms or a module, class or function name. pandas.read_sql_query(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, chunksize=None)[source]¶ ·
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_sql_query.html
pandas.read_sql_query — pandas 3.0.1 documentation
Read SQL query into a DataFrame. Returns a DataFrame corresponding to the result set of the query string. Optionally provide an index_col parameter to use one of the columns as the index, otherwise default integer index will be used. ... SQL query to be executed. conSQLAlchemy connectable, ...
🌐
MyScale
myscale.com › blog › mastering-pandas-read-sql-query-step-by-step-guide
Mastering pandas read_sql_query: Your Ultimate Guide
Regularly reviewing and debugging your queries can help identify and rectify these issues promptly. Despite meticulous planning, you may encounter challenges while using pandas read_sql_query.
🌐
Reddit
reddit.com › r/learnpython › pandas.to_sql() error
r/learnpython on Reddit: Pandas.to_sql() error
January 17, 2019 -

Hi guys.

I am having an weird error that I have no idea how to solve.

I have a huge data-set and I am trying to upload it to sql using pandas. I can't read the entire file so I am reading it in chunks. My idea is to use a for loop to go through all the chunk and append them to the sql.

Here my code:

import numpy as np

import pandas as pd

import sqlalchemy as sql

# CREATE SQL ALCHEMY OBJCET

connect_string = 'mysql://....'

sql_engine = sql.create_engine(connect_string)

# Load Training set

train_chunks = pd.read_csv("train.csv",

chunksize=10000,

low_memory=False)

for train in train_chunks:

# ADD TO SQL

train.to_sql(train, con=sql_engine, if_exists='append', index=False)

And I get the error:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

I have done this before (without the chunks) and never got this error. I have been trying to figure out for an entire afternoon now and no luck.

Can anybody save me?

Thanks!

🌐
GitHub
github.com › pandas-dev › pandas › issues › 49413
ENH: Clearer error messaging when pd.DataFrame.to_sql() receives an empty string for name param · Issue #49413 · pandas-dev/pandas
... def to_sql(self, name, ...): if not name or name == "": raise ValueError("name parameter must be a non-empty string.") ... could help quite a lot! I think this should be something that is pretty easy to handle with existing native python functionality, but I'm happy to clarify things further if people want 😄 · No response · StefanieSenger · Error ReportingIncorrect or improved errors from pandasIncorrect or improved errors from pandas ·