Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output SQL as string from pandas.DataFrame.to_sql

Is there a way of making pandas (or sqlalchemy) output the SQL that would be executed by a call to to_sql() instead of actually executing it? This would be handy in many cases where I actually need to update multiple databases with the same data where python and pandas only exists in one of my machines.

like image 439
kliron Avatar asked Oct 03 '15 07:10

kliron


People also ask

What does DF to_sql return?

Returns None or int. Number of rows affected by to_sql. None is returned if the callable passed into method does not return an integer number of rows. The number of returned rows affected is the sum of the rowcount attribute of sqlite3.

What is DF to_sql?

DataFrame - to_sql() function. The to_sql() function is used to write records stored in a DataFrame to a SQL database. Syntax: DataFrame.to_sql(self, name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None) Parameters: Name.

How do you convert a DataFrame to a string?

If you want to change the data type for all columns in the DataFrame to the string type, you can use df. applymap(str) or df. astype(str) methods.


1 Answers

According to the doc, use the echo parameter as:

engine = create_engine("mysql://scott:tiger@hostname/dbname", **echo=True**)

like image 101
Jano Avatar answered Sep 28 '22 09:09

Jano