python - Output SQL as string from pandas.DataFrame.to_sql -


is there way of making pandas (or sqlalchemy) output sql executed call to_sql() instead of executing it? handy in many cases need update multiple databases same data python , pandas exists in 1 of machines.

this more process question programming one. first, use of multiple databases. relational databases management systems (rdmbs) designed multiple-user systems many simultaneous users/apps/clients/machines. designed run 1 system, database serves central repository related applications. argue databases should agnostic apps , data-centric (postgre folks) , others believe databases should app-centric (mysql folks). overall, understand more involved flatfile spreadsheet or data frame.

usually, rdms's come in 2 structural types:

  1. file level systems sqlite , ms access (where databases reside in file saved cpu directory); these systems though still powerful , multi-user serve smaller business applications relatively handful of users or team sizes
  2. server-level systems sql server, mysql, postgresql, db2, oracle (where databases run on network without localized file); these systems serve enterprise level systems run full-scale business operations run on lan intranets or web networks.

meanwhile, pandas not database data analysis toolkit (much ms excel) though can import/export queried resultsets rdms's. therefore, maintains no native sql dialect ddl/dml procedures. moreover, pandas runs in memory on os calling python script , cannot shared other clients/machines. pandas not track changes intend in order know different states of data frame during runtime of script unless design way before , after , identify column/row changes.

with mouthful said, why not use 1 database , have python script serve of many clients connect database import/export data data frame. hence, after every data frame change run to_sql(). recall pandas' to_sql uses if_exists argument:

# drops table, recreates it, , updates df.to_sql(name='tablename', con=conn, if_exists='replace')  # appends df data existing table df.to_sql(name='tablename', con=conn, if_exists='append') 

in turn, every app/machine connects centralized database need refresh instance , current data available in real-time end use needs. though of course, table-locking states can issue in multi-user environments if user had table record in edit mode while script tried updating it. transactions here may help.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -