MySQL Procedure with not OUTPUT, get all the result from python [duplicate] - python

This question already has answers here:
Cannot return results from stored procedure using Python cursor
(3 answers)
Closed 2 years ago.
I have a Procedure that is not having OUT or IN, I don't have the permission to change the Procedure.
cursor = connection.cursor()
cursor.callproc('store_procedure',())
res = cursor.fetchall()
I tried everything but I can't get the results from MySQL Procedure.
The procedure without Python is running and return the results, it takes about 20sec but is return something.
The Procedure returns multiple rows.
Can I get the results without change the Procedure?
Use MySQLdb package for python.

Procedures are a bit more complicated, because they can return multiple resultsets
cursor.callproc('store_procedure')
for result in cursor.stored_results():
records = result.fetchall()
print(records)
Even when you retrieve only one table.

Related

How to access the results of queries? [duplicate]

This question already has answers here:
sqlalchemy print results instead of objects
(2 answers)
Closed 5 years ago.
I am working on writing a simple test to validate that the number of distinct values in an "id" column matches the number of rows for each table. I am expecting to be able to access particular values of an object, but when I run code and try to print the value of my variable, I can see that my object is a sqlalchemy.engine.result.ResultProxy object at..., as opposed to being something human readable. I have been on the SQLAlchemy website for over an hour, and have googled several permutations of my question, but have not found what I am looking for.
My code, with terminal output, is below:
from sqlalchemy import create_engine
engine = create_engine('postgresql://kyle.pekosh#localhost:5432/testload')
connection = engine.connect()
id_count = connection.execute('SELECT COUNT(DISTINCT(id)) FROM csv.agencies')
id_count
<sqlalchemy.engine.result.ResultProxy object at 0x10357d290>
This is SQLAlchemy's expected behavior. You need to interact with the ResultProxy. As per SQLAlchemy's documentation:
The returned result is an instance of ResultProxy, which references a DBAPI cursor and provides a largely compatible interface with that of the DBAPI cursor. The DBAPI cursor will be closed by the ResultProxy when all of its result rows (if any) are exhausted. A ResultProxy that returns no rows, such as that of an UPDATE statement (without any returned rows), releases cursor resources immediately upon construction.
The ResultProxy API allows you to fetch the data:
results = connection.execute('SELECT COUNT(DISTINCT(id)) FROM csv.agencies')
id_count = results.first()[0]

How to format selecting data using SQLite3 [duplicate]

This question already has answers here:
How to print a list of tuples with no brackets in Python
(5 answers)
Closed 5 years ago.
Currently doing some selecting exercises using SQLite 3 on Python 3. This all works fine, however I want to format the output onto the shell. At the moment, when I run this program:
def select_all_products2(tech):
with sqlite3.connect('movie.db') as db:
cursor = db.cursor()
cursor.execute("select * from User where userOccupation=?",(tech,))
products2 = cursor.fetchall()
return products2
products2 = select_all_products2("technician")
print(products2)
It just prints all the matching fields out in a long, ugly list. Is there a way I can format the output onto the shell, say, with a \n after each field so it's much more easier to read?
Well yes, an SQL result set is an iterable so you can start with
for product in select_all_products2("technician"):
print (product)
Then you realize that a row is also an iterable which makes it quite possible to format the output into nice looking columns.

Selecting field by the column name [duplicate]

This question already has answers here:
Python: use mysqldb to import a MySQL table as a dictionary?
(5 answers)
Closed 9 years ago.
After executing a query statement on a MySQL database connection, I perform:
rows = cursor.fetchall()
This gives an array of arrays. I'd like to have an array of dictionaries, where each dictionary takes its keys from the requested column names of my table and associates the values from the table.
How do I do this?
Well, you forgot to mention which mysql library you're using.
If using oursql (which I recommend, it is certainly the best one), use oursql's DictCursor. Example:
conn = oursql.connect(...)
curs = conn.cursor(oursql.DictCursor)
If using MySQLdb (why?) Use MySQLdb's DictCursor. Example:
conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
curs = conn.cursor()
Doing that will give you a cursor that returns dicts for each row. Remember to not have duplicate rownames in your query.

Python Script to Delete Data from SQLite DB [duplicate]

This question already has answers here:
Sqlite insert query not working with python?
(2 answers)
Closed 6 years ago.
I'm new to python and SQLite, so I apologize if this is a dumb question. I've written the code below to open up a database and delete the data in the STAGING_LIDs table. The script runs, but when I check the DB, the data is still there. Am I doing something wrong?
import sqlite3
import csv
conn = sqlite3.connect('C:\\SQLite\\Budget_Dev.db')
cur = conn.cursor()
#delete all table data
cur.execute("DELETE FROM STAGING_LIDs;")
I'm using bernie's answer in this question (the accepted answer) as a template.
I figured it out. I needed to add a line:
conn.commit()

Python, MySQL and cursors that fetch maps [duplicate]

This question already has answers here:
Python: use mysqldb to import a MySQL table as a dictionary?
(5 answers)
Closed 9 years ago.
After executing a query statement on a MySQL database connection, I perform:
rows = cursor.fetchall()
This gives an array of arrays. I'd like to have an array of dictionaries, where each dictionary takes its keys from the requested column names of my table and associates the values from the table.
How do I do this?
Well, you forgot to mention which mysql library you're using.
If using oursql (which I recommend, it is certainly the best one), use oursql's DictCursor. Example:
conn = oursql.connect(...)
curs = conn.cursor(oursql.DictCursor)
If using MySQLdb (why?) Use MySQLdb's DictCursor. Example:
conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
curs = conn.cursor()
Doing that will give you a cursor that returns dicts for each row. Remember to not have duplicate rownames in your query.

Categories