Need help with python SQL

I’m trying to refresh the list widget after pressing the update button.
I’m making a function called refresh() but I think I need some help.
Thanks

My function:

def refresh(self):

    self.i_listwidget.clear()
    for record in records:
        self.i_listwidget.addItem(str(record[0]))

Post a complete working example so that we can help you.
That fragment has no SQL in it so i have no idea what to suggest.

I’m trying to refresh the list widget after pressing the update button.
I’m making a function called refresh() but I think I need some help.
Thanks

As Barry says, it would help to see your complete code. Ideally, as
small as possible but complete enough to run or at least see everything
which is going on.

My function:

Please paste code inside “code fences” (triple backticks). This
preserves indentation and other punctuation, which is import for code.
Eg:

 ```
 your code or programme output goes here
 ```

There’s a </> button on the message composition toolbar to do this for
you.

Your code:

 def refresh(self):
     self.i_listwidget.clear()
     for record in records:
         self.i_listwidget.addItem(str(record[0]))

This looks like an object method (it has a self parameter) rather than
a plain function. Is this from inside a class definition.

I’m going to assume that here records is a list of database query
result rows, which typically come back from an SQL query. If so, the
code looks ok and the most obvious problem is that records is not
defined.

Usually you’d just pass it as a parameter, eg:

 def refresh(self, records):
     self.i_listwidget.clear()
     for record in records:
         self.i_listwidget.addItem(str(record[0]))

If you’ve got a class inside with this referesh method, let’s call it
foo, then you’d use it like:

 ... code getting the db query and refreshing the widget ...
 query_records = ... db query call goes here ...
 foo.refresh(query_records)

Notice that the variable at the calling end can be whatever useful name
you like (query_records in the example above) and the refresh
function still calls it records internally because that is the name
of the function parameter.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks. The SQL is also to update the database with the updated information.
And i will also send my code.

I am trying to install a driver and connect to a database in VSC using the following code:

import mysql.connector
cnx = mysql.connector.connect(user=‘root’,
password=‘MyNewPass’,
host=‘127.0.0.1’,
database=‘education’,
auth_plugin=‘mysql_native_password’)
cursor = cnx.cursor()
query = (“SELECT * FROM colleges”)
cursor.execute(query)
for row in cursor.fetchall():
print(row)
cursor.close()
cnx.close()

When I try to connect (python3 read.py), in Terminal I get the following ERROR message (1.) When I test for python installation, it shows the version installed (2), hence ok.
(1.) PS C:\Users\maria\OneDrive\Desktop\Sample> python3 read.py
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
(2.)PS C:\Users\maria\OneDrive\Desktop\Sample> python --version
Python 3.11.0
Python with all scripts is installed, the Command Line shows the version and it is added to the path.

I have checked all possible reasons:

  1. In both System Variables and User Variables, Python is added to Path Variables. I reinstalled python to ensure “PATH” is ticked at the installation.
  2. I disable both Python and Python3 apps in “Settings > Manage App Execution Aliases”. In this case I get the following ERROR message
    "python3 : The term ‘python3’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1+ python3 read.py
  •   + CategoryInfo          : ObjectNotFound: (python3:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException"
    
    

I would really appreciate if somebody could help to resolve the issue.

Never mind, i actually found out what to do but thank you anyway.

For the benefit of everybody who is reading this topic, can you please
explain what was wrong and what you changed to fix it?

Thanks,
Cameron Simpson cs@cskk.id.au

After you close the database connection, you have to write in this code:

for record in records:
            self.i_listwidget.addItem(str(record[0]))