Get First Row Of SQLite Database Using Python [duplicate] - python

Is there an elegant way of getting a single result from an SQLite SELECT query when using Python?
for example:
conn = sqlite3.connect('db_path.db')
cursor=conn.cursor()
cursor.execute("SELECT MAX(value) FROM table")
for row in cursor:
for elem in row:
maxVal = elem
is there a way to avoid those nested fors and get the value directly? I've tried
maxVal = cursor[0][0]
without any success.

I think you're looking for Cursor.fetchone() :
cursor.fetchone()[0]

Or you could write a wrapper function that, given SQL, returns a scalar result:
def get_scalar_result(conn, sql):
cursor=conn.cursor()
cursor.execute(sql)
return cursor.fetchone()[0]
I apologize for the possibly less than syntactically correct Python above, but I hope you get the idea.

Be careful, accepted answer might cause TypeError!
Due to fetchone() documentation:
Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.
So with some SQL queries cursor.fetchone()[0] could turn into None[0] which leads to raising TypeError exception.
Better way to get first row or None is:
first_row = next(cursor, [None])[0]
If SQL query is empty, next will use default value [None] and get first element from that list without raising exceptions.

If you're not using pysqlite which has the built in cursor.fetchone
cursor.execute("select value from table order by value desc limit 1")

Sequence unpacking can be used to extract the scalar value from the result tuple.
By iterating over the cursor (or cursor.fetchall)if there are multiple rows:
for result, in cursor:
print(result)
Or using cursor.fetchone if there is a single row in the resultset:
result, = cur.fetchone()
print(result)
In both cases the trailing comma after result unpacks the element from the single-element tuple. This is the same as the more commonly seen
a, b = (1, 2)
except the tuples only have one element:
a, = (1,)

select count(*) from ... groupy by ... returns None instead of 0,
so fetchone()[0] would lead to an exception.
Therefore
def get_scalar_from_sql(sqlcur, sqlcmd):
# select count(*) from .... groupy by ... returns None instead of 0
sqlcur.execute(sqlcmd)
scalar = 0
tuple_or_None = sqlcur.fetchone()
if not tuple_or_None is None:
(scalar,) = tuple_or_None
return scalar

or you can try :
cursor.execute("SELECT * FROM table where name='martin'")

Related

Assigning variables to python from an sql database [duplicate]

Is there an elegant way of getting a single result from an SQLite SELECT query when using Python?
for example:
conn = sqlite3.connect('db_path.db')
cursor=conn.cursor()
cursor.execute("SELECT MAX(value) FROM table")
for row in cursor:
for elem in row:
maxVal = elem
is there a way to avoid those nested fors and get the value directly? I've tried
maxVal = cursor[0][0]
without any success.
I think you're looking for Cursor.fetchone() :
cursor.fetchone()[0]
Or you could write a wrapper function that, given SQL, returns a scalar result:
def get_scalar_result(conn, sql):
cursor=conn.cursor()
cursor.execute(sql)
return cursor.fetchone()[0]
I apologize for the possibly less than syntactically correct Python above, but I hope you get the idea.
Be careful, accepted answer might cause TypeError!
Due to fetchone() documentation:
Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.
So with some SQL queries cursor.fetchone()[0] could turn into None[0] which leads to raising TypeError exception.
Better way to get first row or None is:
first_row = next(cursor, [None])[0]
If SQL query is empty, next will use default value [None] and get first element from that list without raising exceptions.
If you're not using pysqlite which has the built in cursor.fetchone
cursor.execute("select value from table order by value desc limit 1")
Sequence unpacking can be used to extract the scalar value from the result tuple.
By iterating over the cursor (or cursor.fetchall)if there are multiple rows:
for result, in cursor:
print(result)
Or using cursor.fetchone if there is a single row in the resultset:
result, = cur.fetchone()
print(result)
In both cases the trailing comma after result unpacks the element from the single-element tuple. This is the same as the more commonly seen
a, b = (1, 2)
except the tuples only have one element:
a, = (1,)
select count(*) from ... groupy by ... returns None instead of 0,
so fetchone()[0] would lead to an exception.
Therefore
def get_scalar_from_sql(sqlcur, sqlcmd):
# select count(*) from .... groupy by ... returns None instead of 0
sqlcur.execute(sqlcmd)
scalar = 0
tuple_or_None = sqlcur.fetchone()
if not tuple_or_None is None:
(scalar,) = tuple_or_None
return scalar
or you can try :
cursor.execute("SELECT * FROM table where name='martin'")

How would I get just the integer when fetching an integer from mysql [duplicate]

Is there an elegant way of getting a single result from an SQLite SELECT query when using Python?
for example:
conn = sqlite3.connect('db_path.db')
cursor=conn.cursor()
cursor.execute("SELECT MAX(value) FROM table")
for row in cursor:
for elem in row:
maxVal = elem
is there a way to avoid those nested fors and get the value directly? I've tried
maxVal = cursor[0][0]
without any success.
I think you're looking for Cursor.fetchone() :
cursor.fetchone()[0]
Or you could write a wrapper function that, given SQL, returns a scalar result:
def get_scalar_result(conn, sql):
cursor=conn.cursor()
cursor.execute(sql)
return cursor.fetchone()[0]
I apologize for the possibly less than syntactically correct Python above, but I hope you get the idea.
Be careful, accepted answer might cause TypeError!
Due to fetchone() documentation:
Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.
So with some SQL queries cursor.fetchone()[0] could turn into None[0] which leads to raising TypeError exception.
Better way to get first row or None is:
first_row = next(cursor, [None])[0]
If SQL query is empty, next will use default value [None] and get first element from that list without raising exceptions.
If you're not using pysqlite which has the built in cursor.fetchone
cursor.execute("select value from table order by value desc limit 1")
Sequence unpacking can be used to extract the scalar value from the result tuple.
By iterating over the cursor (or cursor.fetchall)if there are multiple rows:
for result, in cursor:
print(result)
Or using cursor.fetchone if there is a single row in the resultset:
result, = cur.fetchone()
print(result)
In both cases the trailing comma after result unpacks the element from the single-element tuple. This is the same as the more commonly seen
a, b = (1, 2)
except the tuples only have one element:
a, = (1,)
select count(*) from ... groupy by ... returns None instead of 0,
so fetchone()[0] would lead to an exception.
Therefore
def get_scalar_from_sql(sqlcur, sqlcmd):
# select count(*) from .... groupy by ... returns None instead of 0
sqlcur.execute(sqlcmd)
scalar = 0
tuple_or_None = sqlcur.fetchone()
if not tuple_or_None is None:
(scalar,) = tuple_or_None
return scalar
or you can try :
cursor.execute("SELECT * FROM table where name='martin'")

PyGreSQL is returning unexpected result for a one column select statement

I have the following PostgreSQL database table:
TABLE session_monitor
(
id int,
customer_name varchar(150)
)
When I am running the following code:
seq = pg_cur.execute("SELECT id ,customer_name from session_monitor")
for id, customer_name in seq:
print(id)
print(customer_name)
it is working fine.
But when I try
pg_cur.execute("SELECT max(id) as max_id from session_monitor")
for max_id in seq:
print(max_id)
I am not getting a number, but rather: Row(max_id= 5)
I tried a workaround that sort of solves the problem:
seq = pg_cur.execute("SELECT max(id) as max_id,1 as one from session_monitor")
for max_id, one in seq:
print(max_id)
but it seems kind of lame to use it like this.
What am I doing wrong here - is there a different way to iterate the query result in the second case?
You get this result due to the way how DB API 2 works (it returns tuples for rows from the database), combined with the Python quirk that you need to add a comma when you want to specify a tuple with one element. It has little to do with PyGreSQL.
So, when you first do this, you get a sequence of (named) tuples in seq:
seq = cur.execute("SELECT id, customer_name FROM session_monitor")
When you then do the following, you iterate over that sequence while unpacking the row tuple you get in each iteration to id and customer_name:
for id, customer_name in seq:
print(id)
print(customer_name)
When you only select one column, like this, you still get a sequence of (named) tuples, with only one element per tuple:
seq = cur.execute("SELECT max(id) AS max_id FROM session_monitor")
Now you tried to iterate over the sequence like this:
for max_id in seq:
print(max_id)
The difference to the loop above is that this one does no tuple unpacking. So max_id will be the complete row, which is printed.
To make use of tuple unpacking like above, you need to add a comma after the max_id:
for max_id, in seq:
print(max_id)
This will print what you expected. Btw, you can also add parens around the tuple unpacking expression. However, you still need a comma if a tuple only has one element.
Of course, if you only get one row with one column, you could just do:
seq = cur.execute("SELECT max(id) AS max_id FROM session_monitor")
max_id = list(seq)[0][0]
print(max_id)
Or, alternatively, using the fetchone() method:
cur.execute("SELECT max(id) as max_id FROM session_monitor")
max_id = cur.fetchone()[0]
print(max_id)
And of course, you can also make use of the fact that you get named tuples:
cur.execute("SELECT max(id) as max_id from session_monitor")
print(cur.fetchone().max_id)
As a side note, using the "classic" PyGreSQL interface instead of DB API 2, you would do this:
q = db.query('SELECT max(id) FROM session_monitor')
print(q.singlescalar())
The single() method gets a single row like fetchone(), and singlescalar() gets the first column of that row.

Syntax Error in SQL Query( select ) when an empty list is passed

I'm running SQL Query using python-Django RAW Query..!!
I'm using IN() function to pass my tuple in the query.
My code looks like this...
Here I am getting the list of dnc_domains and dnc_company from a json/function
dnc_domain_names = list(map(lambda x: get_domain_name(x), dnc_websites))
dnc_company_names = list(map(lambda l: l.lower(), list(filter(None, list(map(lambda x: x['company_name'], dnc_info))))))
QUERY:
select_query = """
select c.id
from (
select id, lower(company_name) as c_name,substring(website from '(?:.*://)?(?:www\.)?([^/]*)') as website_domain,
from contacts
where campaign_id = %s
) c
where
c.c_name IN %s
OR c.website_domain IN %s
"""
Executing Query:
with connection.cursor() as cursor:
cursor.execute(select_query, (campaign.id,tuple(dnc_company_names),tuple(dnc_domain_names))
matching_contact_ids = cursor.fetchall()
But, there is a case when any dnc_company_names or dnc_domain_name is empty [] then my Query throws an Error otherwise if there at least 1 element in any of them then it works fine.
SQL Error:
syntax error at or near ")"
LINE 5: WHERE id IN ()
^
So, help me to tackle this error. SQL should handle both empty or non empty tuples.
This is solved by providing NULL values in list
if not dnc_company_name:
dnc_company_name = [None]
finally, in my question, I am converting dnc_company_name into tuple.
and it solved my problem.
The right part of the IN must be a subquery returning exactly one column:
https://www.postgresql.org/docs/current/static/functions-subquery.html#FUNCTIONS-SUBQUERY-IN
I tried
SELECT *
FROM table_with_data
WHERE toto IN (SELECT * FROM empty);
which throws this error:
ERROR: subquery has too few columns
I guess you should provide something as IN (NULL) in the case your columns are strictly NOT NULL.

How to get the numbers of data rows from sqlite table in python

I am trying to get the numbers of rows returned from an sqlite3 database in python but it seems the feature isn't available:
Think of php mysqli_num_rows() in mysql
Although I devised a means but it is a awkward: assuming a class execute sql and give me the results:
# Query Execution returning a result
data = sql.sqlExec("select * from user")
# run another query for number of row checking, not very good workaround
dataCopy = sql.sqlExec("select * from user")
# Try to cast dataCopy to list and get the length, I did this because i notice as soon
# as I perform any action of the data, data becomes null
# This is not too good as someone else can perform another transaction on the database
# In the nick of time
if len(list(dataCopy)) :
for m in data :
print("Name = {}, Password = {}".format(m["username"], m["password"]));
else :
print("Query return nothing")
Is there a function or property that can do this without stress.
Normally, cursor.rowcount would give you the number of results of a query.
However, for SQLite, that property is often set to -1 due to the nature of how SQLite produces results. Short of a COUNT() query first you often won't know the number of results returned.
This is because SQLite produces rows as it finds them in the database, and won't itself know how many rows are produced until the end of the database is reached.
From the documentation of cursor.rowcount:
Although the Cursor class of the sqlite3 module implements this attribute, the database engine’s own support for the determination of “rows affected”/”rows selected” is quirky.
For executemany() statements, the number of modifications are summed up into rowcount.
As required by the Python DB API Spec, the rowcount attribute “is -1 in case no executeXX() has been performed on the cursor or the rowcount of the last operation is not determinable by the interface”. This includes SELECT statements because we cannot determine the number of rows a query produced until all rows were fetched.
Emphasis mine.
For your specific query, you can add a sub-select to add a column:
data = sql.sqlExec("select (select count() from user) as count, * from user")
This is not all that efficient for large tables, however.
If all you need is one row, use cursor.fetchone() instead:
cursor.execute('SELECT * FROM user WHERE userid=?', (userid,))
row = cursor.fetchone()
if row is None:
raise ValueError('No such user found')
result = "Name = {}, Password = {}".format(row["username"], row["password"])
import sqlite3
conn = sqlite3.connect(path/to/db)
cursor = conn.cursor()
cursor.execute("select * from user")
results = cursor.fetchall()
print len(results)
len(results) is just what you want
Use following:
dataCopy = sql.sqlExec("select count(*) from user")
values = dataCopy.fetchone()
print values[0]
When you just want an estimate beforehand, then simple use COUNT():
n_estimate = cursor.execute("SELECT COUNT() FROM user").fetchone()[0]
To get the exact number before fetching, use a locked "Read transaction", during which the table won't be changed from outside, like this:
cursor.execute("BEGIN") # start transaction
n = cursor.execute("SELECT COUNT() FROM user").fetchone()[0]
# if n > big: be_prepared()
allrows=cursor.execute("SELECT * FROM user").fetchall()
cursor.connection.commit() # end transaction
assert n == len(allrows)
Note: A normal SELECT also locks - but just until it itself is completely fetched or the cursor closes or commit() / END or other actions implicitely end the transaction ...
I've found the select statement with count() to be slow on a very large DB. Moreover, using fetch all() can be very memory-intensive.
Unless you explicitly design your database so that it does not have a rowid, you can always try a quick solution
cur.execute("SELECT max(rowid) from Table")
n = cur.fetchone()[0]
This will tell you how many rows your database has.
I did it like
cursor.execute("select count(*) from my_table")
results = cursor.fetchone()
print(results[0])
this code worked for me:
import sqlite3
con = sqlite3.connect(your_db_file)
cursor = con.cursor()
result = cursor.execute("select count(*) from your_table").fetchall() #returns array of tupples
num_of_rows = result[0][0]
A simple alternative approach here is to use fetchall to pull a column into a python list, then count the length of the list. I don't know if this is pythonic or especially efficient but it seems to work:
rowlist = []
c.execute("SELECT {rowid} from {whichTable}".\
format (rowid = "rowid", whichTable = whichTable))
rowlist = c.fetchall ()
rowlistcount = len(rowlist)
print (rowlistcount)
The following script works:
def say():
global s #make s global decleration
vt = sqlite3.connect('kur_kel.db') #connecting db.file
bilgi = vt.cursor()
bilgi.execute(' select count (*) from kuke ') #execute sql command
say_01=bilgi.fetchone() #catch one query from executed sql
print (say_01[0]) #catch a tuple first item
s=say_01[0] # assign variable to sql query result
bilgi.close() #close query
vt.close() #close db file

Categories

Resources