def export_data(self):
        if (self.report_table.rowCount() == 0):
            gui_helper.prompt_error("Please enter a query")
            return

        file_path = gui_helper.prompt_file_save()

        if (not file_path):
            return

        with PdfPages(file_path) as pdf:
            conn = database.get_db_connection()
            for i in range(self.report_table.rowCount()):
                query_text = self.report_table.item(i, 1).text()
                graph_type = self.report_table.item(i, 0).text()

                dict_values = query.manual_sql_query(conn, query_text)
                try:
                    plt = self.export_graph_options[graph_type](graph_type,
                                                                dict_values)

                    fig = plt.gcf()
                    pdf.savefig()
                    plt.close(fig)
                except Exception as e:
                    gui_helper.prompt_error(
                        "Failed to export graph {}: ".format(i) + repr(e))
            conn.close()
            gui_helper.prompt_information(
                "Graphs has been successfully exported")
Exemple #2
0
    def export_data(self):
        query_text = self.query.toPlainText()
        if (len(query_text) == 0):
            prompt_error("Please enter a query")
            return

        try:
            conn = database.get_db_connection()
            dataframe = query.manual_sql_query(conn, query_text)
            dialog = QFileDialog()
            dialog.setFileMode(QFileDialog.AnyFile)

            if dialog.exec_():
                filepaths = dialog.selectedFiles()
                self.file_save(filepaths[0], dataframe)
        except Exception as e:
            gui_helper.prompt_error(str(e))
    def export(self, file_path, query_text):
        '''(ExportCSV, str, str) -> None
        Given a file path and sql query, export the data from that sql query
        into a csv format.

        Arguments:
            file_path {str}  -- CSV file name
            query_text {str} -- query string

        Returns:
            None
        '''
        conn = database.get_db_connection()
        # export query to csv file
        dataframe = query.manual_sql_query(conn, query_text)
        if (not file_path.endswith(".csv")):
            file_path += ".csv"
        dataframe.to_csv(file_path, index=False)
 def on_click(self):
     file_name = self.fileName_field.text()
     # getting respective query based on num in description
     key = (self.cb.currentText())[:1]
     conn = database.get_db_connection()
     quer = presetquery.get_preset(conn, key)
     query_result = query.manual_sql_query(conn, quer)
     conn.close()
     # if select CSV
     if self.b1.isChecked() == True:
         # export to CSV file
         print("save as csv ", exportFile.exportCSV(file_name,
                                                    query_result))
     # else if select PDF
     elif self.b2.isChecked() == True:
         # export to PDF file
         print("save as pdf ",
               exportPDF.exportToPDF(file_name + '.pdf', query_result))
import query
from os import path
import pandas as pd

class ExportCSV:
    def export(self, file_path, query_text):
        '''(ExportCSV, str, str) -> None
        Given a file path and sql query, export the data from that sql query
        into a csv format.

        Arguments:
            file_path {str}  -- CSV file name
            query_text {str} -- query string

        Returns:
            None
        '''
        conn = database.get_db_connection()
        # export query to csv file
        dataframe = query.manual_sql_query(conn, query_text)
        if (not file_path.endswith(".csv")):
            file_path += ".csv"
        dataframe.to_csv(file_path, index=False)

if __name__ == "__main__":
    connection = database.get_db_connection("root", "12345678", "world")
    # query.printDB(query.get_DBinfo())
    results = query.manual_sql_query(
        connection, "select Name, District from city")
    exportCSV("E:\project", "test.csv", results)