How to handle .to_sql if dataframe is empty
I am collecting data points from google books, converting the data to a dataframe, then ingesting into mysql database.
For each datapoint, I will create a dataframe, ingest into a staging table, fetch from that staging table into a main table, then drop the staging table.
Sometimes, some batches of books will have adaptations. Sometimes, they wont. If they won't, the dataframe that will store title to adaptation mapping have nothing populating them, since some titles don't have adaptations. Say, I have a a dataframe like so
adaptation
that looks like
title | adaptation
|
Where it's empty. Then, I try to create a staging table like so, using pandas .to_sql
method
adaptation.to_sql(name='adaptation_staging', con=mysql_conn, if_exists='append', index=False),
I'll get a SQL syntax error like so, if dataframe is empty, I think:
(mysql.connector.errors.ProgrammingError) 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2
[SQL:
CREATE TABLE adaptation_staging (
)
]
(Background on this error at: http://sqlalche.me/e/13/f405)
So, how can I handle this case? Presumedly, if the adaptation dataframe is empty, we won't even want to create this staging table, since nothing will be inserted into the main adaptation table. So maybe wrap in a try: except:
?
Thoughts? I can clarify if need be. Thanks!
Solution 1:
Check if dataframe is empty, if it is empty print as empty and in the else block run the create table SQL script.
if adaptation.empty:
print('adaptation is empty')
else:
adaptation.to_sql(name='adaptation_staging', con=mysql_conn, if_exists='append', index=False)