Show SQL Server fields in Telegram

0

I have the following code in python with connection to SQLServer database made in ubuntu (it is a bot for telegram):

import pymssql
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

logger = logging.getLogger(__name__)

server = '10.248.205.209'
user = 'esolis'
password = 'secret'
database = 'pythonstorage'

conecction = pymssql.connetion(server, user, password, database)
 cur = connection.cursor()
 sql = ' SELECT id,servicio FROM logdaga'
  cur.execute(sql)

def start(bot, update):
    update.message.reply_text('Bienvenido a BotSQL, ¿En que te puedo ayudar?, Para ver mis comandos usa: /help')

def tables(bot, update):
    for row in cur.fetchall():
        resultado = row[0], row[1]
    update.message.reply_text(resultado)

This table logdaga has 10 records but ......

As a result of executing the /tables command in the telegram bot, I get the following result:

As you can see in the image, only one record appears instead of the 10 records it contains, I would like to know what I am doing wrong: (.

Beforehand I thank you for your help.

    
asked by Santiago Huh 26.07.2017 в 02:14
source

1 answer

1

As I see it, in the last line update.message.reply_text(resultado) is out of the cycle FOR , so it only prints a record, the last one obtained in the cycle.

<!-- language: lang-py -->
def tables(bot, update):
    for row in cur.fetchall():
        resultado = row[0], row[1]
        update.message.reply_text(resultado)

You can save everything in a list if you like it too.

<!-- language: lang-py -->
def tables(bot, update):
    lista = []
    for row in cur.fetchall():
        resultado = row[0], row[1]
        lista.append(resultado)
    update.message.reply_text(lista)
    
answered by 04.04.2018 / 08:34
source