Telegram. bot. SQlite3 how to display id?

You need that whenever the user clicks the command /start, id have been made in the table if it is not there. At this stage the table is written "built-in function id" and this is done every time(need only if ID not). What to add and what to remove? I would be grateful if the answer will be detailed, with working sqlite for the first time.

import telebot
import sqlite3
from telebot import types

bot = telebot.TeleBot('1084130875:AAGB5EfXIMo')
id = str(id)

@bot.message_handler(commands=['start'])
def start(message):
 markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
 btn1 = types.Keyboardbutton To('Search')
 btn2 = types.Keyboardbutton To('Connection')
 btn3 = types.Keyboardbutton To('Add')
 markup.add(btn1, btn2, btn3)

 send_message = f"<b>Hello {message.from_user.first_name}!</b>"
 bot.send_message(message.chat.id, send_message, parse_mode='html', reply_markup=markup)

 connection = sqlite3.connect("database.db", check_same_thread = True)
 cursor = connection.cursor()

 cursor.execute("CREATE TABLE IF NOT EXISTS users (id)")
 cursor.execute("INSERT OR IGNORE INTO users (id) VALUES (:id)", {"id": id})

connection.commit()
 connection.close()
April 19th 20 at 12:07
1 answer
April 19th 20 at 12:09
Solution
to begin to do imports:
import sqlite3
from sqlite3 import Error
from time import sleep, ctime


calling all SQL statements to do this via the:
def post_sql_query(sql_query):
 with sqlite3.connect('my.db') as connection:
 cursor = connection.cursor()
try:
cursor.execute(sql_query)
 except Error:
pass
 result = cursor.fetchall()
 return result


next you need to create a table in sqlite and as a primary key (that it is not the uniqueness defined) to make the id user:
def create_tables():
 users_query = "'CREATE TABLE IF NOT EXISTS USERS 
 (user_id INTEGER PRIMARY KEY NOT NULL,
 username TEXT
 first_name TEXT,
 last_name TEXT,
 reg_date TEXT);"'
 post_sql_query(users_query)


and the function of registering user:
def register_user(user, username, first_name, last_name):
 user_check_query = f SELECT * FROM USERS WHERE user_id = {user};'
 user_check_data = post_sql_query(user_check_query)
 if not user_check_data:
 insert_to_db_query = f INSERT INTO USERS (user_id, username, first_name, last_name, reg_date) VALUES ({user} "{username}", "{first_name}", "{last_name}", ctime());'
 post_sql_query(insert_to_db_query )

and then call it from the handler:
create_tables() # calling the function to create the users table


@bot.message_handler(commands=['start'])
def start(message):
 register_user(message.from_user.id, message.from_user.username
 message.from_user.first_name, message.from_user.last_name)
 bot.send_message(message.from_user.id Welcome f {message.from_user.first_name}' )


I think the principle is clear, you can continue with all your data to work and Yes the code myself but should work.
Enough detail?)
Yes, very detailed:) But not working. The table is created, but new users are not recorded( - Henry_Lemke commented on April 19th 20 at 12:12
@Henry_Lemke, test care is not passed.
insert_to_db_query = f INSERT INTO USERS (user_id, username, first_name, last_name, reg_date) VALUES ({user} "{username}", "{first_name}", "{last_name}", "{ctime()}");'

to properly wrap ctime - avery_Tremblay commented on April 19th 20 at 12:15
@avery_Tremblay, thank you very much, it works!)) - Henry_Lemke commented on April 19th 20 at 12:18

Find more questions by tags TelegramBotsSQLitePython