A
A
Arkady Baganin2019-11-02 21:22:35
Python
Arkady Baganin, 2019-11-02 21:22:35

How to solve "AttributeError: 'sqlite3.Connection' object has no attribute 'fetchone'" error?

Good day!
---
I'll be brief, I'm creating a report upload bot. During development, the bot began to throw an error: "AttributeError: 'sqlite3.Connection' object has no attribute 'fetchone'" .
---

The code

bot.py:
# -*- coding: utf-8 -*-
import sqlite3
import config
import telebot
import requests
import secrets
import json
import re
import time
from telebot import types
from datetime import datetime
import uuid

bot = telebot.TeleBot(config.token_bot)

@bot.message_handler(commands=['start'])
def get_document_query(message):
    # CREATE REQUES
    body = {"UserId": message.from_user.id, "UserName": message.from_user.username, "UserFirstName": message.from_user.first_name, "UserLastName": message.from_user.last_name, "ProjectKey": config.project_key} 
    us_r = requests.post('https://mindscan.ru/Outer/GetToken', data=body)
    text = 'Введите поисковый запрос'
    data = json.loads(us_r.text)
    token = data.get('Jwt')

    # SAVE TOKEN
    with sqlite3.connect(config.db_name) as conn:
        stmt = "INSERT INTO users (id, username , token) VALUES (Null, ?, ?)"
        args = (message.from_user.username, token)
        conn.execute(stmt, args)
        conn.commit()

    # SEND MESSAGE
    msg = bot.send_message(message.chat.id, text)

    # CREATE NEXT STEP
    bot.register_next_step_handler(msg, query_confirm)

@bot.message_handler(regexp='подтвердить отчёт')
def query_confirm(message):
    # CREATE TEXT
    text = 'Ваш поисковый запрос: *' + message.text + '*'
    keyboard = types.InlineKeyboardMarkup()
    callback_button = types.InlineKeyboardButton(text="Подтвердить", callback_data="confirm")
    keyboard.add(callback_button)

    # SEND MESSAGE
    bot.send_message(message.chat.id, text, parse_mode='Markdown', reply_markup=keyboard)

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        if call.data == "confirm":
            # HEADERS
            token = ''
            with sqlite3.connect(config.db_name) as conn:
                stmt = "SELECT token FROM users WHERE username=:login"
                args = {'login': call.message.from_user.username}
                conn.execute(stmt, args)
                row = conn.fetchone()
                token = row[0]['token']

            bot.send(call.message.chat.id, token)

while True:
    try:
        if __name__ == '__main__':
            print('Bot running..')
            bot.polling(none_stop=True)
    except Exception as ex:
        print('Error - {}'.format(str(ex)))
        print('Restarting..')
        bot.stop_polling()
        time.sleep(15)
        print('Running again!')
        bot.polling(none_stop=True)


---
How to solve this problem???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-11-02
@ark_yt

Read the sqlite3 module documentation. It should become clear that fetchoneis a cursor method, not a connection method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question