0

I am trying to select from a specific row and then column in SQL. I want to find a specific user_name row and then select the access_id from the row.

Here is all of my code.

import sys, ConfigParser, numpy
import MySQLdb as mdb
from plaid.utils import json

class SQLConnection:
    """Used to connect to a SQL database and send queries to it"""
    config_file = 'db.cfg'
    section_name = 'Database Details'

_db_name = ''
_hostname = ''
_ip_address = ''
_username = ''
_password = ''

def __init__(self):
    config = ConfigParser.RawConfigParser()
    config.read(self.config_file)
    print "making"

    try:
        _db_name = config.get(self.section_name, 'db_name')
        _hostname = config.get(self.section_name, 'hostname')
        _ip_address = config.get(self.section_name, 'ip_address')
        _user = config.get(self.section_name, 'user')
        _password = config.get(self.section_name, 'password')
    except ConfigParser.NoOptionError as e:
        print ('one of the options in the config file has no value\n{0}: ' +
            '{1}').format(e.errno, e.strerror)
        sys.exit()


    self.con = mdb.connect(_hostname, _user, _password, _db_name)
    self.con.autocommit(False)
    self.con.ping(True)

    self.cur = self.con.cursor(mdb.cursors.DictCursor)


def query(self, sql_query, values=None):
        """
        take in 1 or more query strings and perform a transaction
        @param sql_query: either a single string or an array of strings
            representing individual queries
        @param values: either a single json object or an array of json objects
            representing quoted values to insert into the relative query
            (values and sql_query indexes must line up)
        """
        #  TODO check sql_query and values to see if they are lists
        #  if sql_query is a string
        if isinstance(sql_query, basestring):
            self.cur.execute(sql_query, values)
            self.con.commit()
        #  otherwise sql_query should be a list of strings
        else:
            #  execute each query with relative values
            for query, sub_values in zip(sql_query, values):
                self.cur.execute(query, sub_values)
            #  commit all these queries
            self.con.commit
        return self.cur.fetchall


def get_plaid_token(self,username):
        result= self.query("SELECT access_id FROM `users` WHERE `user_name` LIKE %s",[username])
        print type (result)
        return result


print SQLConnection().get_plaid_token("test")

I would like the get the transaction ID but for some reason "result" returns

> <bound method DictCursor.fetchall of <MySQLdb.cursors.DictCursor
> object at 0x000000000396F278>>

result is also of type "instancemethod"

1
  • What does autocommit and ping do? Commented Sep 7, 2016 at 1:10

1 Answer 1

2

try changing this line:

return self.cur.fetchall

to

return self.cur.fetchall()

Without the parentheses after the method name, you are returning a reference to that method itself, not running the method.

Sign up to request clarification or add additional context in comments.

1 Comment

@gordonLinoff I will accept the answer. I have to wait 10 minutes before I can however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.