added support for sqlite database

This commit is contained in:
Gourav Kumar 2022-01-10 07:44:40 +05:30
parent 5260e4fbf5
commit 8cf97d2475
3 changed files with 22 additions and 12 deletions

View File

@ -1,9 +1,11 @@
# Database
DATABASE=sqlite
DB_NAME=abcd
DB_USER=abcd
DB_PWD="abcd!#$"
DB_HOST=localhost
DB_PORT=5432
DB_FILE="sample.sqlite"
# Keys
WORDBOT_TOKEN=0000000000:abcdefghij_kl_mn-op_qrst

View File

@ -9,6 +9,7 @@ import logging
import os
import psycopg2
import requests
import sqlite3
import telegram
import telegram.ext
import time
@ -21,21 +22,28 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, Callb
load_dotenv()
logging.basicConfig(filename='logs/word_bot.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
log_file_name = f"logs/wordbot_log_{datetime.date.today().strftime('%Y_%m_%d')}.log"
logging.basicConfig(filename=log_file_name, filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
logger = logging.getLogger()
def connect_db():
pgcon = psycopg2.connect(
database=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PWD"),
host=os.getenv("DB_HOST"),
port=os.getenv("DB_PORT")
)
database = os.getenv('DATABASE')
if database == 'postgres':
con = psycopg2.connect(
database=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PWD"),
host=os.getenv("DB_HOST"),
port=os.getenv("DB_PORT")
)
print("connected to postgres")
elif database == 'sqlite':
con = sqlite3.connect(os.getenv('DB_FILE'))
print("connected to sqlite")
return pgcon
return con
def start(
@ -94,14 +102,14 @@ def word_def(
Sends the message
"""
pgcon = connect_db()
if pgcon is None:
con = connect_db()
if con is None:
print("Db connection failed")
word_query = 'select * from gre_words order by random() limit 1'
while True:
cur = pgcon.cursor()
cur = con.cursor()
cur.execute(word_query)
row = cur.fetchall()
selected_word = row[0][1].lower()

BIN
wordbot.sqlite Normal file

Binary file not shown.