How to get value from Cursor execution result Python - python

I have a sqllite database in which the field dad_mmsi is created without a type:
c.execute('''CREATE TABLE IF NOT EXISTS AIS_anoniem
(dad_mmsi, dad_navstatus......)'''')
when i fetch the top result;
c.execute('SELECT DISTINCT dad_mmsi FROM AIS')
print c.fetchall()[0]
it prints:
(u'456000001',)
which is not the same as what i put in, because it is converted to a tuple.
EDIT: since it is a tuple i need to access the index of the value i want:
print c.fetchall()[0][0] == '456000001'
gives me:
'true'

fetchall returns a list of rows, and each row is a list (tuple) of columns.
So this is correct.
Your query with DISTINCT does not return the top result, it returns all unique values.
To return only the first result, use
SELECT dad_mmsi FROM AIS LIMIT 1
(but you should add an ORDER BY clause to control which record you get).
To return only one row, use not fetchall but fetchone.

Related

How to execute a query with SQL Alchemy and store the result into a variable type int [duplicate]

I have:
res = db.engine.execute('select count(id) from sometable')
The returned object is sqlalchemy.engine.result.ResultProxy.
How do I get count value from res?
Res is not accessed by index but I have figured this out as:
count=None
for i in res:
count = res[0]
break
There must be an easier way right? What is it? I didn't discover it yet.
Note: The db is a postgres db.
While the other answers work, SQLAlchemy provides a shortcut for scalar queries as ResultProxy.scalar():
count = db.engine.execute('select count(id) from sometable').scalar()
scalar() fetches the first column of the first row and closes the result set, or returns None if no row is present. There's also Query.scalar(), if using the Query API.
what you are asking for called unpacking, ResultProxy is an iterable, so we can do
# there will be single record
record, = db.engine.execute('select count(id) from sometable')
# this record consist of single value
count, = record
The ResultProxy in SQLAlchemy (as documented here http://docs.sqlalchemy.org/en/latest/core/connections.html?highlight=execute#sqlalchemy.engine.ResultProxy) is an iterable of the columns returned from the database. For a count() query, simply access the first element to get the column, and then another index to get the first element (and only) element of that column.
result = db.engine.execute('select count(id) from sometable')
count = result[0][0]
If you happened to be using the ORM of SQLAlchemy, I would suggest using the Query.count() method on the appropriate model as shown here: http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=count#sqlalchemy.orm.query.Query.count

Wrong value of row obtained

I am using the python.mysql connector to access a MySQL table.
The MySQL database has a table policy_data which has a few columns, one being application_no.
The application_no column has values in the form of t000... totaling 8 digits including t.
Ideally, the first value of application_no column is t0000001.
So I pass a command (from Python):
cursor.execute(select* application_no from policy_data where...(some condition)
data = cursor.fetchall()
appl = data[0][0] # this should give me 't0000001'
Here's the problem: I tried the above command as it is on MySQL, and it gives me t0000001. But from Python (the above code), the value (appl=data[0][0]) is coming as t.
I even tried putting the received value inside str(), but it still doesn't work.
data=cursor.fetchall() returns a list of tuples (one tuple for each row of your table)
appl=data[0][0] returns the first element of the first tuple namely the value of first column of first row in your query result.
Given this, if column 'application_no' is second in your query result (and it is as you use * in your query) you will get the values of this column with data[i][1]
So if you check for aapl=data[0][1] it sould return your desired output 't0000001'
If I understand it correctly your SQL query returns a list of strings. By doing
aapl=data[0][0]
you grab the first string, and then its first character 't'
maybe give
aapl=data[0]
a try

How to solve Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied on Delete and Excutemany? [duplicate]

Say I have a list of following values:
listA = [1,2,3,4,5,6,7,8,9,10]
I want to put each value of this list in a column named formatteddate in my SQLite database using executemany command rather than loop through the entire list and inserting each value separately.
I know how to do it if I had multiple columns of data to insert. For instance, if I had to insert listA,listB,listC then I could create a tuple like (listA[i],listB[i],listC[i]). Is it possible to insert one list of values without a loop. Also assume the insert values are integers.
UPDATE:
Based on the answer provided I tried the following code:
def excutemanySQLCodewithTask(sqlcommand,task,databasefilename):
# create a database connection
conn = create_connection(databasefilename)
with conn:
cur = conn.cursor()
cur.executemany(sqlcommand,[(i,) for i in task])
return cur.lastrowid
tempStorage = [19750328, 19750330, 19750401, 19750402, 19750404, 19750406, 19751024, 19751025, 19751028, 19751030]
excutemanySQLCodewithTask("""UPDATE myTable SET formatteddate = (?) ;""",tempStorage,databasefilename)
It still takes too long (roughly 10 hours). I have 150,000 items in tempStorage. I tried INSERT INTO and that was slow as well. It seems like it isn't possible to make a list of tuple of integers.
As you say, you need a list of tuples. So you can do:
cursor.executemany("INSERT INTO my_table VALUES (?)", [(a,) for a in listA])

SQL insert query through python loop

I'm trying to insert multiple rows into a table using a for-loop in python using the following code:
ID = 0
values = ['a', 'b', 'c']
for x in values:
database.execute("INSERT INTO table (ID, value) VALUES (:ID, :value)",
ID = ID, value = x)
ID += 1
What I'd expected to happen was that this piece of code would insert three rows into my table. The only problem is that it only executes the query once. So I'd only get the row " 0, 'a' ".
There aren't any error messages popping up, it just doesn't update the table with the other two values. Weirdly enough however, I can circumvent this problem by using multiple queries, like so:
ID = 0
values = ['a', 'b', 'c']
for x in values:
database.execute("INSERT INTO table (ID) VALUES (:ID)", ID = ID)
database.execute("INSERT INTO table (value) VALUES (:value)", value = x)
ID += 1
While this updates my code, this method becomes more tedious as I add columns to my table further down the line. Does anyone know why the first snippet of code doesn't work and the second one does?
The execute method takes an array as the second parameter.
execute(sql[, parameters])
Executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders instead of SQL literals). The sqlite3 module
supports two kinds of placeholders: question marks (qmark style) and
named placeholders (named style).
This should work:
database.execute("INSERT INTO table (ID, value) VALUES (:ID, :value)", [ID , x])
You might want to investigte executemany while you're in the doc.
From the same doc:
commit()
This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not
visible from other database connections. If you wonder why you don’t
see the data you’ve written to the database, please check you didn’t
forget to call this method.
You might want to investigte executemany while you're in the doc.

sqlite3 remove brackets from printed data

I have created a script that finds the last value in the first row of my database
import sqlite3
global SerialNum
conn = sqlite3.connect("MyFirstDB.db")
conn.text_factory = str
c = conn.cursor()
SerialNum = c.execute('select Serial from BI4000 where Serial in (Select max(Serial) from BI4000)')
print SerialNum
conn.commtt()
conn.close()
the program prints the result
[('00003',)]
which is the last result in the current database, all the data that will be entered into the final database will be serial numbers and so it will be in order.
My question is can I remove all the quotations/brackets/comma as I wish to asign this value to a variable.
The program that I wish to make is a testing system that adds new entries to the database, I wish to check what the last entry is in the database so the system can continue the entries from that point.
The result of the query you execute is being represented as a Python list of Python tuples.
The tuples contained in the list represent the rows returned by your query.
Each value contained in a tuple represents the corresponding field, of that specific row, in the order you selected it (in your case you selected just one field, so each tuple has only one value).
Long story short: your_variable = SerialNum[0][0]
If you want to retrieve just one column from one row, use:
c.execute('select Serial from BI4000 where Serial in (Select max(Serial) from BI4000)')
result = c.fetchone()
if result: # first row returned?
print result[0] # first column
Your query could be simplified to:
c.execute('Select max(Serial) from BI4000')

Categories