I have a very simple function that exports sqlite to a panda dataframe to csv in my pyside2 project using latest version of pyside2 and python 3.7.
import pandas as pd
conn = sqlite3.connect(db_file, isolation_level=None,
detect_types=sqlite3.PARSE_COLNAMES)
db_df = pd.read_sql_query("SELECT * FROM error_log", conn)
db_df.to_csv('database.csv', index=False)
I had like to introduce the ability for a user to choose where to save the file. I can pull up a filechooser like
name = QFileDialog.getSaveFileName(self, "Save", os.getcwd(), "CSV Files (*.csv)")
My question is how do I connect the QfileDialog with the code that exports to csv.
I have tried the following without much success.
name = QFileDialog.getSaveFileName(self, "Save", os.getcwd(), "CSV Files (*.csv)")
if name[0] :
with open(name[0], "w") as file :
file.write(db_df)
ok, I figured this one out.
import pandas as pd
conn = sqlite3.connect("data.db", isolation_level=None,
detect_types=sqlite3.PARSE_COLNAMES)
name, _ = QFileDialog.getSaveFileName(self, "Save", os.getcwd()+os.sep+"all.csv", "CSV Files (*.csv)")
db_df = pd.read_sql_query("SELECT * FROM '{}'".format(userInput), conn)
if not name : return 0
db_df.to_csv(name, index=False)
does the trick ! Where userInput is the name of the table in the database data.db.