python - 没有从 sql 查询 select python 中获得预期结果

标签 python mysql sql

我正在尝试从 SQL 中的特定行和列中进行选择。 我想找到特定的 user_name 行,然后从该行中选择 access_id 。

这是我的所有代码。

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

我想要获取交易 ID,但由于某种原因“结果”返回

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

结果也是类型“instancemethod”

最佳答案

尝试更改此行:

return self.cur.fetchall

return self.cur.fetchall()

如果方法名称后面没有括号,您将返回对该方法本身的引用,而不是运行该方法。

关于python - 没有从 sql 查询 select python 中获得预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39359548/

相关文章:

javascript - 从mysql下载数据到Android时出错

mysql - Rails 4 ActiveRecord - 如果查询为 ".find_by_sql"如何处理数据?

java - JOOQ用于SQL查询写入: Stack overflow exception in tests

mysql - 获取2天mysql之间的总工作时间

java - Hadoop - 保存日志数据和开发GUI

Python + 使函数更通用

python - 更改 Tkinter 文本小部件中的退格效果

python - 如何替换嵌套列表python 3中的元素?

mysql - 从两个表中有两个外键的多对多关系表中选择行

php - 从循环内的表中获取数组