Have pyodbc return a simple (scalar) value for a query that only returns one item

MathiasRa

I am having an issue when I select data from the SQL database through pyodbc where I end up with this type of result.

[(55.0, )]

I want the pure number (no "[" or "(" so I later can insert it into a different table after calculating new stuff with it. I am sure it's trivial but I just haven't been able to figure out how. Below is the code I am using:

rows = conn.execute("SELECT price from PG").fetchall()
print(rows[:1])
blue_note

You have [(55.0, )] because you have a list of rows (containing a single row in this example), and each row is a tuple (with a single element, since you just selected price). You can do

singlerow = rows[0]
price, = singlerow

However, fetching all rows to select just one seems weird, you should probably re-think your query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Best way to return 450 rows with query that currently only returns one

SQL query to only return the rows that have the highest value in a certain column

MySQL COUNT query returns only one value from a CASE expression

C# query inside a for loop returns only one value

Method returns one or more, should it return an Array when there is only one item?

Oracle sql create query to return value only for one 'employee'

How can I select from items from two tables with one item only have one value

Fetch returns only one item in collection

select returns only one item from XML

LINQ only returns one item in collection

Ansible returns only one item from a dictionary

SQL query many-to-many association table that have only one single associated item

JQuery return only one value

How to query two tables that have the same field, that returns id only if field value is the same

pyodbc rowcount only returns -1

Should a function have only one return statement?

Query the second highest value of a name, if there's only one value return null

Query only one item in a nested mongoose object

Mongo find query only returns one result

WP_Query returns only one post

Elasticsearch: How to only return results for text search query if the query contains all of the words in at least one item in the indexed array?

I need to return rows if the eeid and date only have one value from paycode id

Return rows if the eeid and date only have one value from paycode id

MySQL select MAX value from subquery only returns one results from outer query. Why?

Why does this query have only one result?

Dropdown only returns item text/value from first item

Python simple loop of averages only returns first item

Trying to have a function return a simple number, it returns something else

Running Mongoose Query Only If It Returns A Value

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive