0

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)
1
  • @eyllansec, the difference here being the user can choose the filename with the QFileDialog box and the filename with which to save. The previous questions answered how to export the dataframe but not how to allow the users to choose where to save and with which filename. Commented May 19, 2020 at 0:51

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.