Returning a SQL table query with pymssql

Hi,

I am wondering if it’s possible to use an uplink using pymssql to return a table to use in a repeating panel or data grid components.

Above was one of my attempts to do this through an uplink. I’m sure it’s incorrect as I’m new to python and Anvil so any help would be greatly appreciated.

I then have self.repeating_panel_1.items = anvil.server.call(‘get_sql_data’) in my form code.

The idea is simple; I’d like to edit the data I get from sql on an anvil form.

I’ve tried to find documentation on the matter but most of it seems related to MySQL or does not use pymssql.

Thanks for any advice

Welcome to the Anvil community.

I use PyMySQL with great success so I’m sure that library works just as well.

Please can you tell us what is going wrong?

As a starting point, I would make sure your uplink function is actually producing a result (use print statements to display the returned data).

Next, I would make sure that data is being returned in a format required by the repeating panel as described here :

Regarding asking questions, the forum gives you a template of what information to provide when you create a topic. Filling that in as completely as you can will greatly increase the chances of someone helping you effectively.

Hi David, thanks for the tips and warm welcome.

What is going wrong is I’m trying to return a sql table to be viewed in a data grid, once I do that I want to try make that data editable.

The result is no error when I try to run the app but the data grid simply opens empty.

I’ve been able to insert into my sql table with a submit form, now I want to take the next step and access the data and possibly edit it.

During trying to insert using the uplink Anvil was great at telling me what the problem was and where, so I could format where required to get it to work. The fact this is returning no error is leaving me lost.

OK, so you need to follow my suggestion and make sure your query is returning something.

Instead of returning the result immediately, assign it to a variable and print it, eg -

result = cursor.fetchall()
print("My result is - ", result)

If the result is not in the format the repeating panel requires, then it needs to be cajoled until it is.

If the result is in the correct format, then you need to do the same on the form (ie don’t assign the result directly to the repeating panel, store it and print it so you see the result in the debug window).

Those steps will probably lead you to the issue.

3 Likes

Thanks David, I’ll give it a try :smile:

Okay, I managed to print the results for my server uplink using a variable.

varGet = anvil.server.call(‘get_sql_data’)
print(varGet)

Here’s what it’s returning with no errors:

It’s a simple table I made with ID (int), User_Name (nvarchar(50)), Date_Of_Birth (datetime), and Score (int) columns.

I also read in the documentation you linked that a repeating panels item property needs to be set to an iterable. However, when I select the component I see an item property but it says Set at runtime and cannot be interacted with.

You probably will want to pass the component.items property a list of dictionaries that comes from your MsSQL table.
The last time I used pymssql this was done at the cursor level using a parameter passed to the cursor object at creation.

so if conn was your connection object:
cursor = conn.cursor(as_dict=True)

I googled this for you: pymssql examples — pymssql 2.1.2 documentation

Then in your repeating panels items properties, you will be able to access the data using the column names for each row / part of the panel.

Here is a clone link to an example I created before, it shows with a hand-written test object what the object looks like if done correctly:
https://anvil.works/build#clone:MCAHTDHMX5XEZMOP=LQN7F6V2FEUMVPS5WOKJKMEI

2 Likes

Oh, also I would try messing around with DataGrids first, Anvil Docs | Data Grids

If you can get those to work with some hand written example data, you will already understand how repeating panels work without having to mess with creating objects attached to data and formatting before you get to the ‘putting data on a screen’ part.

3 Likes