Wrong value of row obtained - python

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

Related

Query external tuples list embedded within a SQL query

I have to run a sql query that grabs the values only if two conditions are true. So for example, I need to grab all the values where asset=x and id_name=12345. There are about 10k combinations between asset and id_name that I need to be able to query for using sql. Usually I would just do the following:
select * from database where id_name IN (12345)
But how do I make this query when two conditions have to be true. id_name has to equal 12345 AND asset has to equal x.
I tried turning the list i need into tuples like this:
new_list = list(scams[['asset', 'id_name']].itertuples(index=False, name=None))
which gives me a list like this:
new_list = (12345, x), (32342, z)...etc.
Any suggestions would be great. Thanks!
Based on my understanding you need to query or fetch records based on a combination of two filters. Also you have around 10K combinations. Here is a simple SQL based solution.
Create a new column in the same table or build a temp table/view with a new column say "column_new". Populate the concatenated value of id_name and asset in the new column. You can use a concatenation function based on the database. Example in SQL server use CONCAT(column1,column2).
Now you can write your SQL as select * from database where colum_new IN ("12345x","32342z");.
Note : You can also use a "-" or "|" between column 1 and column 2 while doing a concatenation.

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])

Problems with SQL and Python to extract values

I have a problem when i extract values from a DB with sqlite3. Everytime that i extract values ,for example, one number have this format:
[(6,)]
And is like tuple.
I want only the 6 value WITHOUT , and ( and [
Thanks for your help in advanced!
This is common in all SQL adapters. They always return a list of rows (tuples) even if you only fetchone, it comes back with a tuple, because it doesn't know if this is one value, or one row of values, so to stay constant... tuple.
x[0] is your first row
x[0][0] is your first item in that row.

How do I express this query in SQL Alchemy

I am trying to query a table in an existing sqlite database. The data must first be subsetted as such, from a user input:
query(Data.num == input)
Then I want to find the max and min of another field: date in this subset.
I have tried using func.min/max, as well as union, but received an error saying the columns do not match. One of the issues here is that func.min/max need to be used as query arguments, not filter.
ids = session.query(Data).filter(Data.num == input)
q = session.query(func.max(Data.date),
func.min(Data.date))
ids.union(q).all()
ArgumentError: All selectables passed to CompoundSelect must have identical numbers of columns; select #1 has 12 columns, select #2 has 2
Similarly, if I use func.max and min separately, the error says #2 has 1 column.
I think seeing this query in SQL might help as well.
Thanks
The following solution works. You first need to set up the query, then filter the data down afterwards.
query = session.query(Data.num, func.min(Data.date),
func.max(Data.date), Data.date)
query = query.filter(Data.num == input)
query = query.all()

How to get value from Cursor execution result 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.

Categories