I am playing with python for the first time on the raspberry pi.
I have a script that queries an SQL table and returns the value that is set.
What I can not get to work is setting the python variable from the results.
Here is part of the code I have
# execute SQL query using execute() method.
cursor.execute("select id from wallboard")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
# disconnect from server
db.close()
result = str("%s " % data)
print result
if result == 1:
The print displays the result okay but it is not going into the if statement.
I am very new to python so its possibly a simple fix but I'm stumped.
Thanks
Don't convert the result to string:
>>> "1" == 1
False
Also note that fetchone() would return you a single row of results which would be represented as a tuple - get the first item to get the actual id column value:
result = cursor.fetchone()[0]
Related
I'm new to Python and was working on a script which executes a postgres query and pulls its result, it's just a number:
con = psycopg2.connect("dbname=mydb user=postgres host=192.168.0.10")
cur = con.cursor()
myvar='TEST'
cur.execute("SELECT get_id('myvar')")
my_id = cur.fetchone()
print(my_id)
The results I get are like these depending on the myvar value:
(144,)
(140,)
(141,)
Sometimes when there's no value returned by the query, I get this:
(None,)
I was expecting something like "null" (similar to what I get when I run this on the DB) but that wasn't the case.
Question number one is: why do I get the values surrounded by a parenthesis and the comma at the end?
Question two is: How "if" may work when the value is 'None'?
I've tried this:
if my_id=='None':
if my_id=='(None,)':
but didn't work..
The result is a tuple, which is an immutable sequence
Check if the first item of the tuple is None:
if my_id[0] is None:
pass
Also, please be sure that you are using the proper string substitution with psycopg2. Your code should look something like this:
con = psycopg2.connect("dbname=mydb user=postgres host=192.168.0.10")
cur = con.cursor()
myvar='TEST'
cur.execute("SELECT get_id(%s)", [myvar])
my_id = cur.fetchone()[0]
if my_id is not None:
print(my_id)
def check_gen0_1(db_name):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
cursor.execute("select JuvPop from History where Generation=0")
global results1
results1 = cursor.fetchone()
def check_gen0_run(db_name):
if __name__ == "__main__":
check_gen0_1(db_name)
def menu2(dbless, db_name):
check_gen0_run(db_name)
print(results1)
This is my code. The result printed is
(0,)
How do I get rid of the brackets? How do I only get the 0 or whatever value is there.
.fetchone() returns a single row of results of the query which is represented as a tuple. If you need the first value from it, just get it by index:
print(results1[0])
Not sure if:
results1 = results1[1:-2]
print(results1)
or just:
print(results1[1:-2])
would get you what you wanted? Depends if the output you're getting is a string of course.
This splits the first char and last 2 characters from the results, which is fine if this is something that will happen for every value you're getting but will cause issues if these values are changeable.
I have troubles using a simple sql statement with the operator IN through pymssql.
Here is a sample :
import pymssql
conn = pymssql.connect(server='myserver', database='mydb')
cursor = conn.cursor()
req = "SELECT * FROM t1 where id in (%s)"
cursor.execute(req, tuple(range(1,10)))
res = cursor.fetchall()
Surprisingly only the first id is returned and I can't figure out why.
Does anyone encounter the same behavior ?
You're trying to pass nine ID values to the query and you only have one placeholder. You can get nine placeholders by doing this:
ids = range(1,10)
placeholders = ','.join('%s' for i in ids)
req = "SELECT * FROM t1 where id in ({})".format(placeholders)
cursor.execute(req, ids)
res = cursor.fetchall()
As an aside, you don't necessarily need a tuple here. A list will work fine.
It looks like you are only passing SELECT * FROM t1 where id in (1). You call execute with the tuple but the string only has one formatter. To pass all values, call execute like this:
cursor.execute(req, (tuple(range(1,10)),))
This will pass the tuple as first argument to the string to format.
EDIT: Regarding the executeone/many() thing, if you call executemany and it returns the last instead of the first id, it seems that execute will run the query 10 times as it can format the string with 10 values. The last run will then return the last id.
I'm looking for the way to be able to compare what is in a Python variable with something in a SQLite query.
For example:
num = input ("enter a number")
cur.execute("SELECT a.Name,a.Number FROM Ads a WHERE a.Number = (num)")
row = cur.fetchone()
while row:
print(row)
row = cur.fetchone()
The cur.execute line doesn't work. I don't know how to be able to compare content of a Python variable with data in a SQLite database via a SQlite query.
You put a '?' as a place holder for each Python variable, something like
num = 5
cur.execute("SELECT Name, Number FROM Ads WHERE Number = ?", (num,))
The execute() method expects a sequence as a second argument, since several '?' positional placeholders can be used in the SQL statement.
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