Python, MySQL and cursors that fetch maps [duplicate] - python

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.

Related

Python SQLite equivalent to .tables (one-liner without Pandas)

When using the SQLite executable, I can print to console a list of all the tables in a database with .tables. Is there an equivalent one-liner way to do this in Python using the sqlite3 module without using Pandas?
Based on the answers at List of tables, db schema, dump etc using the Python sqlite3 API, it appears you have to create a cursor first, then a SQL query to return a list of the tables (which is actually a list of tuples as unicode characters that needs to be cleaned for a pretty print to console). That's five lines, versus SQLite's one line.
Or maybe a related question is: why do I need to open a cursor to get this information in Python sqlite3?
con = sqlite3.connect(db)
cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cursor.fetchall()
tables = map(str, [t[0] for t in tables])
print(tables)
It's not pretty, but since you're only using the variables once, you can simply chain all the calls.
print([str(t[0]) for t in sqlite3.connect(db).cursor().execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()])

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

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.

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]

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

Categories