Home > Software design >  Retrieve data when clicking a button
Retrieve data when clicking a button

Time:08-09

Trying to receive input from WCT_Control into WCT_DataPull

Cant figure out how to get the data into WCT_DataPull to perform an action with it. I think I am going about this backwards, but I also think I have been staring at it too long.

Essentially, the user enters the information necessary into a GUI to connect to a specific SQL table (predetermined) and then saves the data in the table and outputs it as a csv file backup.

I want the user to click the submit button and that creates the backup. However, at this point when I click the button, it will store all the data in the variables (If I put a print statement in I see the correct values), but I cant seem to figure out how to get the variables to WCT_DataPull, where the backup creation action is performed.

WCT_Control

from PyQt5.QtWidgets import *
from WCT_View import Ui_MainWindow


class Controller(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.run.clicked.connect(lambda : self.submit())

    def submit(self):
        self.run.clicked.connect()
        server = self.server_entry.text()
        database = self.data_entry.text()
        station = self.station_entry.text()
        app = self.app_entry.text()
        backup_name = self.filename_entry.text()


        self.server_entry.setText('')
        self.data_entry.setText('')
        self.station_entry.setText('')
        self.app_entry.setText('')
        self.filename_entry.setText('')

        return server, database, station, app, backup_name

WCT_DataPull

from WCT_Control import *
import pyodbc
import csv

pull_data = Controller()

def write_bak():
    driver = 'ODBC Driver 17 for SQL Server'
    serv, data, stat, app, bak_name = pull_data.submit()

    conn = pyodbc.connect('DRIVER={0};SERVER={1};DATABASE={2};Trusted_Connection=yes'.format(driver, serv, data))
    cursor = conn.cursor()

    rows = cursor.execute("""
                    select DnsName, PackageName, Code, Value from WorkstationApplicationSettings
                    where DnsName=? and PackageName=?
                    """, stat, app).fetchall()
    for row in rows:
        print(row.PackageName,':', row.Code, ':', row.Value)

    with open(bak_name, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

CodePudding user response:

So you just have to do the things in opposite way, instead of using PYQT5 in WCT script, use the WCT function in PYQT5 script

WCT_Control

from PyQt5.QtWidgets import *
from WCT_DataPull import write_bak


class Controller(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.run.clicked.connect(lambda : self.submit())

    def submit(self):
        self.run.clicked.connect()
        server = self.server_entry.text()
        database = self.data_entry.text()
        station = self.station_entry.text()
        app = self.app_entry.text()
        backup_name = self.filename_entry.text()


        self.server_entry.setText('')
        self.data_entry.setText('')
        self.station_entry.setText('')
        self.app_entry.setText('')
        self.filename_entry.setText('')
        write_bak(serv, data, stat, app, bak_name)

WCT_DataPull

import pyodbc
import csv


def write_bak(serv, data, stat, app, bak_name):
    driver = 'ODBC Driver 17 for SQL Server'

    conn = pyodbc.connect('DRIVER={0};SERVER={1};DATABASE={2};Trusted_Connection=yes'.format(driver, serv, data))
    cursor = conn.cursor()

    rows = cursor.execute("""
                    select DnsName, PackageName, Code, Value from WorkstationApplicationSettings
                    where DnsName=? and PackageName=?
                    """, stat, app).fetchall()
    for row in rows:
        print(row.PackageName,':', row.Code, ':', row.Value)

    with open(bak_name, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

Also Make sure to write the code to run the WCT_control by using Qapplication

  • Related