Very basic table, basic query.
My table has these 5 columns:
project_id, name, job, status, state
I wish to query all names related to a specific project_id.
I am receiving a tuple as the result set, with only the field values and not the field names.
RESULT: ('5', 'bob', 'worker', 'union', '123-456-7890', 'CA')
How would I amend this query to receive the field names (i.e. column titles) in the output?
DESIRED: {project_id: '5', name:'bob', job:'worker', status:'union', state:'CA'}
Code:
from sqlalchemy import create_engine
def my_engine(db):
USERNAME = 'admin'
PASSWORD = 'password'
HOST = 'localhost'
DB_PORT = 1234
DATABASE = str(db)
return create_engine('mysql+pymysql://{0}:{1}@{2}:{3}/{4}'
.format(USERNAME, PASSWORD, HOST, DB_PORT, DATABASE), pool_pre_ping=True)
def query_mysql(self, table_name, col_title, srx_val):
myconn = my_engine('dbname')
return_list = myconn.execute("SELECT * FROM `{0}` WHERE `{1}` = '{2}'"
.format(table_name, col_title, srx_val)).fetchall()
return return_list
outdata = self.query_mysql('users', 'project_id', self.PROJ_NUM)
#returns: ('5', 'bob', 'worker', 'union', '123-456-7890', 'CA')