Python: display first element of tuple in cursor - python

I have the following code:
conn = mysql.connector.connect(database='test', user='me', password='pwd')
cursor = conn.cursor()
query = ( "select id from T where project = 10" )
cursor.execute(query)
result = cursor.fetchall()
result is showing as:
[(Decimal('476749'),), (Decimal('478045'),), (Decimal('479713'),)]
is it possible to show it as : [476749, 478045, 479713]

Why use zip/map when python is so much more elegant?
[int(i[0]) for i in cursor.fetchall()]

You can use zip function to get the first items and map to convert the decimals to integer:
>>> import decimal
>>> map(int,zip(*[(decimal.Decimal('476749'),), (decimal.Decimal('478045'),), (decimal.Decimal('479713'),)])[0])
[476749, 478045, 479713]
And in your code :
result = map(int,zip(*cursor.fetchall()))

Related

im facing type errorr object of type cx_oracle has no len()

[![enter image description here][1]][1]hi im facing type error cx+_orcle has no len() can you please help
query1="SELECT B.RMT_SITE_NM, A.CO_APPL_PRFL_ID, A.PRFL_ID FROM MIGRATION_TRACKING A, T_SFT_INIT_PRTCL B WHERE A.PRFL_ID=B.INIT_PRTCL_ID AND A.STATUS='Scheduled' AND A.PHASE='Phase 1' AND A.WAVE='Wave 1'"
cursor = connection()
ans = cursor.execute(query1)
if ans:
for rows in range(len(ans)):
name = str(ans[rows][0])
co_id_table = cursor.execute(query2,(name))
if co_id_table:
co_id = co_id_table[0][17]
data = cursor.execute(query3,(co_id))
data = data[0]
rndm_id = generate_id() ```
[1]: https://i.stack.imgur.com/YsnMs.jpg
[2]: https://i.stack.imgur.com/bttB1.jpg
This is the incorrect way of iterating over rows. You should instead do this:
for row in cursor.execute(query1):
name = str(row[0])
...
If you prefer to get all of the rows up front (since you are going to use the same cursor to execute other queries), then you can do this:
cursor.execute(query1)
rows = cursor.fetchall()
The value returned from cursor.execute() when the statement executed is a query is simply the cursor itself. Since the cursor implements the iteration protocol, you can also do this:
cursor.execute(query1)
rows = list(cursor)

Prestodb + Python: Using a List as Query Argument

I'm trying to use prestodb in Python and pass a list of numbers as an argument in a query and it's giving this error:
PrestoUserError: PrestoUserError(type=USER_ERROR, name=TYPE_MISMATCH, message="line 208:33: IN value and list items must be the same type: bigint", query_id=20211122_175131_24052_rruhu)
The code is similar to this:
import prestodb
from prestodb import dbapi
import os
conn=prestodb.dbapi.connect(
host=os.environ['aa'],
port=os.environ['bb'],
user=os.environ['cc'],
password=os.environ['dd'],
catalog='hive'
)
date_start = '2021-10-10'
date_end = '2021-10-15'
list_id = (1,2,3,4)
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in ({list_id})
'''.format(date_start=date_start,date_end=date_end,list_id=list_id)
cur = conn.cursor()
cur.execute(sql)
query_result = cur.fetchall()
format will not join the list_id correctly. Try combining ids into comma separated strings with ','.join(map(str, list_id)):
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in ({list_id})
'''.format(date_start=date_start,date_end=date_end,list_id=','.join(map(str, list_id)))
UPD
Or, as suggested by #Tomerikoo - just str(list_id) and remove extra parenthesis from the format:
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in {list_id}
'''.format(date_start=date_start,date_end=date_end,list_id=str(list_id))

Mysql SUM query returning "1" instead of correct much higher amount in pyqt5

Table example:
"id","xxx', "xxxx" "xxxx", "dec"
where x = string, id = id, dec = decimal.
It just returns a 1 instead of the correct 274. When I run the query in MySql, the result is correct.
def add_amounts(self):
myTotal = 0
mydb = MySQLdb.connect("localhost", "TomM", "67706621TM", "expenses")
cursor = mydb.cursor()
myTotal = cursor.execute("SELECT SUM(amount) as total FROM all_transactions;")
print(myTotal)
As you didn't tell us the exact table structure, i don't know which criteria you use to summarize the amount.
But you have to use a query like this.
SELECT ColumnName, SUM(amount) as total FROM all_transactions
GROUP BY ColumName;
So you get the sum of the amount for every what ever you have in ColumName .
You are printing the return value of cursor.execute, but the value you want - the result of the SUM - is in the first column of the first row of the resultset.
>>> import MySQLdb
>>> conn = MySQLdb.connect('localhost', 'root', 'secret', 'test')
>>> cursor = conn.cursor()
>>> cursor.execute("""SELECT SUM(assessment) FROM my_table;""")
1
>>> row = cursor.fetchone() # Get the row
>>> result = row[0] # Our result is in the first column of the row.
>>> print(result)
1552.50

convert python sql list into dictionary

How to convert
cursor.execute("SELECT strftime('%m.%d.%Y %H:%M:%S', timestamp, 'localtime'), temp FROM data WHERE timestamp>datetime('now','-1 hours')")
# fetch all or one we'll go for all.
results = cursor.fetchall()
for row in results[:-1]:
row=results[-1]
rowstr="['{0}',{1}]\n".format(str(row[0]),str(row[1]))
temp_chart_table+=rowstr
result
['01.15.2015 21:38:52',21.812]
into dictionary output in form of:
[{timestamp:'01.15.2015 21:38:52',temp:21.812}]
Edit
This is fetchone sample I currenyly use and it works fine:
def get_avg():
conn=sqlite3.connect(dbname)
curs=conn.cursor()
curs.execute("SELECT ROUND(avg(temp), 2.2) FROM data WHERE timestamp>datetime('now','-1 hour') AND timestamp<=datetime('now')")
rowavg=curs.fetchone()
#print rowavg
#rowstrmin=format(str(rowavg[0]))
#return rowstrmin
**d = [{"avg":rowavg[0]}]**
return d
conn.close()
#print get_avg()
schema = {"avg": ("number", "avg")}
data = get_avg()
# Loading it into gviz_api.DataTable
data_table = gviz_api.DataTable(schema)
data_table.LoadData(data)
json = data_table.ToJSon()
#print results
#print "Content-type: application/json\n\n"
print "Content-type: application/json"
print
print json
Then I make jQuery call and pass it into javascript and found help for that in here
ajax json query directly to python generated html gets undefined
As I can see you are using format to write in the form of a string.
Note from the docs
it is not possible to use { and } as fill char while using the str.format() method
To make it look like a dictionary you can do
"[{timestamp:'%s',temp:%s}]\n"%(str(row[0]),str(row[1]))
But if you want to make it a dictionary then you will have to do
row_dic = [{'timestamp':row[0],'temp':row[1]}]
Try this instead:
cursor.execute("SELECT strftime('%m.%d.%Y %H:%M:%S', timestamp, 'localtime'), temp FROM data WHERE timestamp>datetime('now','-1 hours')")
# fetch all or one we'll go for all.
results = cursor.fetchall()
temp_chart_table = []
for row in results:
temp_chart_table.append({'timestamp': row[0], 'temp': row[1]})
In most of the python database adapters you can use a DictCursor to retrieve records using an interface similar to the Python dictionaries instead of the tuples.
Using psycopg2:
>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
... (100, "abc'def"))
>>> dict_cur.execute("SELECT * FROM test")
>>> rec = dict_cur.fetchone()
>>> rec['id']
1
>>> rec['num']
100
>>> rec['data']
"abc'def"
Using MySQLdb:
>>> import MySQLdb
>>> import MySQLdb.cursors
>>> myDb = MySQLdb.connect(user='andy47', passwd='password', db='db_name', cursorclass=MySQLdb.cursors.DictCursor)
>>> myCurs = myDb.cursor()
>>> myCurs.execute("SELECT columna, columnb FROM tablea")
>>> firstRow = myCurs.fetchone()
{'columna':'first value', 'columnb':'second value'}
def stuffToDict(stuff):
return {"timestamp":stuff[0],"temp":stuff[1]}
That would be a dictionary. The sample output you showed is a list of dictionaries, which can be achieved by putting square brackets around the dictionary. I don't know why you'd want that, though. Also, because of the missing quotes, it wasn't legal python syntax.
Use MySQLdb's cursor library.
import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.connect(host=db_host, user=db_user, passwd=db_passwd, db=db_schema, port=db_port, cursorclass=MySQLdb.cursors.DictCursor)
cursor = conn.cursor()
cursor.execute("SELECT timestamp, localtime, temp FROM data WHERE timestamp>datetime('now','-1 hours')")
# fetch all or one we'll go for all.
results = cursor.fetchall()
Then you have access to the results as a dictionary:
>>> results['timestamp']
14146587
>>> results['localtime']
20:08:07
>>> results['temp']
temp_variable_whatever

Python MySQLDB: Get the result of fetchall in a list

I would like to get the result of the fetchall operation in a list instead of tuple of tuple or tuple of dictionaries.
For example,
cursor = connection.cursor() #Cursor could be a normal cursor or dict cursor
query = "Select id from bs"
cursor.execute(query)
row = cursor.fetchall()
Now, the problem is the resultant row is either ((123,),(234,)) or ({'id':123}, {'id':234})
What I am looking for is (123,234) or [123,234]. Be best if I can save on parsing the resulset.
And what about list comprehensions? If result is ((123,), (234,), (345,)):
>>> row = [item[0] for item in cursor.fetchall()]
>>> row
[123, 234, 345]
If result is ({'id': 123}, {'id': 234}, {'id': 345}):
>>> row = [item['id'] for item in cursor.fetchall()]
>>> row
[123, 234, 345]
I'm sure that after all this time, you've solved this problem, however, for some people who may not know how to get the values of a cursor as a dictionary using MySQLdb, you can use this method found here:
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'test623', 'testdb')
with con:
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM Writers LIMIT 4")
rows = cur.fetchall()
for row in rows:
print row["Id"], row["Name"]
This old Q comes up on Google while searching for flattening db queries, so here are more suggestions...
Consider a fast list-flattening iterator.
Others answers use fetchall() which first loads all rows in memory, then iterates over that to make a new list. Could be inefficient. Could combine with MySQL so-called server side cursor:
# assume mysql on localhost with db test and table bs
import itertools
import MySQLdb
import MySQLdb.cursors
conn = MySQLdb.connect(host='localhost',db='test',
cursorclass=MySQLdb.cursors.SSCursor )
cursor = conn.cursor()
# insert a bunch of rows
cursor.executemany('INSERT INTO bs (id) VALUES (%s)',zip(range(1,10000)) )
conn.commit()
# retrieve and listify
cursor.execute("select id from bs")
list_of_ids = list(itertools.chain.from_iterable(cursor))
len(list_of_ids)
#9999
conn.close()
But the question is also tagged Django, which has a nice single field query flattener
class Bs(models.Model):
id_field = models.IntegerField()
list_of_ids = Bs.objects.values_list('id_field', flat=True)
Make your cursor object in this manner:
db = MySQLdb.connect("IP", "user", "password", "dbname")
cursor = db.cursor(MySQLdb.cursors.DictCursor)
Then when you perform cursor.fetchall() on a query, a tuple of dictionaries will be obtained, which you can later convert to a list.
data = cursor.fetchall()
data = list(data)
list= [list[0] for list in cursor.fetchall()]
this will render results in one list like - list = [122,45,55,44...]
If there is only one field, i can use this to make a list from database:
def getFieldAsList():
kursor.execute("Select id from bs")
id_data = kursor.fetchall()
id_list = []
for index in range(len(id_data)):
id_list.append(id_data[index][0])
return id_list
cursor.execute("""Select * From bs WHERE (id = %s)""",(id))
cursor.fetchall()

Categories