Ad
How To Save Telegram Python Variable Value To Local Database?
Sorry, i am newbie in python
i have problem to save variable in python bot telegram into localhost. How to save the value of python bot telegram variable into the local database? Like the following variables
user.sex,user.age, user.q27
I've made a connection like the this
con = pymysql.connect(db="python", user="root", passwd="",host="localhost",port=3306,autocommit=True)
cur = con.cursor()
And try to execute the query in process_data
chat_id = message.chat.id
q27 = message.text
user = user_dict[chat_id]
user.q27 = q27
cur.execute("INSERT INTO diagnosa (sex, age, jawaban) VALUES (%s, %s, %s)" (user.sex,user.age, user.q27))
msg = bot.send_message(chat_id, 'Nice to meet you ' + user.name + '\n Age:' + str(user.age) + '\n Sex:' + user.sex + '\n jawaban:' + user.q27 )
bot.register_next_step_handler(msg, send_end)
Complete code
import pymysql
import time
import telebot
from telebot import types
API_TOKEN = ''
bot = telebot.TeleBot(API_TOKEN)
user_dict = {}
con = pymysql.connect(db="python", user="root", passwd="",host="localhost",port=3306,autocommit=True)
cur = con.cursor()
class User:
def __init__(self, name):
self.name = name
self.age = None
self.sex = None
self.q27 = None
@bot.message_handler(commands=['help', 'start'])
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
msg = bot.reply_to(message, """\
Siapa Nama Anda?
""")
bot.register_next_step_handler(msg, process_name_step)
def process_name_step(message):
try:
chat_id = message.chat.id
name = message.text
user = User(name)
user_dict[chat_id] = user
msg = bot.reply_to(message, 'Berapakah umur anda?')
bot.register_next_step_handler(msg, process_age_step)
except Exception as e:
bot.reply_to(message, 'Kesalahan name step')
def process_age_step(message):
try:
chat_id = message.chat.id
age = message.text
if not age.isdigit():
msg = bot.reply_to(message, 'Umur haruslah sebuah angka. Berapakah usia anda?')
bot.register_next_step_handler(msg, process_age_step)
return
user = user_dict[chat_id]
user.age = age
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Pria', 'Wanita')
msg = bot.reply_to(message, 'Apakah jenis kelamin anda?', reply_markup=markup)
bot.register_next_step_handler(msg, process_q27_step)
except Exception as e:
bot.reply_to(message, 'Kesalahan age step')
def process_data(message):
try:
chat_id = message.chat.id
q27 = message.text
user = user_dict[chat_id]
user.q27 = q27
cur.execute("INSERT INTO diagnosa (sex, age) VALUES (%s, %s, %s)" (user.sex,user.age, user.q27))
msg = bot.send_message(chat_id, 'Nice to meet you ' + user.name + '\n Age:' + str(user.age) + '\n Sex:' + user.sex + '\n jawaban:' + user.q27 )
bot.register_next_step_handler(msg, send_end)
except Exception as e:
bot.reply_to(message, 'oooops')
def send_end(message):
msg = bot.reply_to(message,"""Byeee""")
bot.polling()
Ad
Answer
finally I get the solution of the problem. I do not need cur = con.cursor ()
.
Before writing the query, we need to write
with con.cursor () as cursor:
sql = "query" cursor.execute (sql)
con.commit ()
con.close ()
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad