【发布时间】:2018-06-18 17:26:46
【问题描述】:
我已经定义了一个数据库类如下:-
class Database(object):
""" Implements all interactions with the DB. """
def __init__(self, id_a, id_b, config):
self.config = config
self.id_a = id_a
self.id_b = id_b
self.db_connection = None
self.cursor = None
self.__init_db(config)
def __init_db(self, config):
"""
Initializes the MySQL Connector using the settings
specified in the system configuration.
"""
self.db_connection = mysql.connector.connect(user=config['database_user'],
password=config['database_password'],
host=config['database_host'],
database=config['database_name'])
self.cursor = self.db_connection.cursor(dictionary=True,buffered=True)
self.cursor.execute('SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;')
现在,当我定义以下函数以返回从 Mysql 数据库中获取的值时,我得到一个错误
def get_func(self):
sql = "SELECT c_id FROM table \
WHERE id_a = {} ".format(self.id_a)
self.cursor.execute(sql)
rows = self.cursor.fetchone()
if rows:
for row in rows:
ls1 = row['id_number']
return ls1
ls1 = row['id_number']
TypeError: string indices must be integers
【问题讨论】:
标签: python string python-2.7 byte mysql-python