У меня есть функция, которая из БД берет определенную строку и возвращает id, но функция возвращает не id, как задумывалось, а None. В чем проблема?
Data Base
class DataBase:
def __init__(self, user, password, db):
self.user = user
self.password = password
self.db = db
try:
conn = self.get_conn()
# if conn.is_connected():
# print('Подключение прошло успешно.')
except Error as e:
print(e)
def get_conn(self):
conn = mysql.connector.connect(user=self.user,
password=self.password,
db=self.db)
return conn
def get_subcategory_id(self, title):
sql = "SELECT id FROM buy.subcategory WHERE title = '{0}';".format(title)
conn = self.get_conn()
c = conn.cursor()
c.execute(sql)
id = c.fetchall()
return (id)
Code python
class Tesco:
def write_database(self, data):
data_base = db.DataBase('root', '123456', 'buy')
data_base.get_subcategory_id(data['title'])
def get_products(self, html):
soup = BeautifulSoup(html, 'lxml')
name_sub_category = soup.find('span', class_='plp--breadcrumbs--last-crumb').text
data = {
'title': name_sub_category
}
print(self.write_database(data))
return отсутствует? Надо что-то вроде:
def write_database(self, data):
data_base = db.DataBase('root', '123456', 'buy')
return data_base.get_subcategory_id(data['title'])
Попробуй такую конструкцию
cursor.execute(sql, ())
for field in cursor:
print("found: ", fields)
Должно сработать, у меня сработало.
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости