I have a personal project to create a Telegram bot using python. What I want is to reply to any question with a dynamic answer generate from a database query. I don't want to create data query for every request from bot, so my idea is to generate a set of data (data frame) and then bot can take the answer from there. To generate the data frame, I want to schedule/reload the part of the querying script for every x minutes. My goal is to create Python script which can reload only on querying data without reloading the whole script. is there any ways to do this?
Sample code:
tt = datetime.now()
dsn_tns = cx_Oracle.makedsn(----)
conn = cx_Oracle.connect(user=----, password=----, dsn=dsn_tns)
cursor = conn.cursor()
sql = ("""select *
from TABLE
WHERE REPORTDATE > to_date(:tt,'DD-MM-YYYY HH24:MI:SS')""")
param = {"tt": tt}
data = psql.read_sql(sql,conn)#,params = param)
conn.close()
x = 2314 #value from question via bot
answer = data[(data['number'] == x))]
The part I want to reload regularly is from tt until conn.close().
I'm not sure why you don't want to rerun the query for each bot request, this would make more sense. It seems like you could also have missing data if you do not update your data for each bot request.
However, you can just wrap the code between tt and conn.close() in a function which you can set to run periodically.
def update_data()
global data
tt = datetime.now()
dsn_tns = cx_Oracle.makedsn(----)
conn = cx_Oracle.connect(user=----, password=----, dsn=dsn_tns)
cursor = conn.cursor()
sql = ("""select *
from TABLE
WHERE REPORTDATE > to_date(:tt,'DD-MM-YYYY HH24:MI:SS')""")
param = {"tt": tt}
data = psql.read_sql(sql,conn)#,params = param)
conn.close()
Related
I try to update my sqlite database using flask webhook.
It seems commands line work fine if I type manually in the python console but my flask webhook didn't update my SQLite database. It seems the apps fail at the "cursor.execute()" line.
here is my webhook code:
#app.route('/trendanalyser', methods=['POST'])
def trendanalyser():
data = json.loads(request.data)
if data['passphrase'] == config.WEBHOOK_PASSPHRASE:
#Init update variables
tastate = data['TrendAnalyser']
date_format = datetime.today()
date_update = date_format.strftime("%d/%m/%Y %H:%M:%S")
update_data = ((tastate), (date_update))
#Database connection
connection = sqlite3.connect('TAState15min.db')
cursor = connection.cursor()
#Database Update
update_query = """Update TrendAnalyser set state = ?, date = ? where id = 1"""
cursor.execute(update_query, update_data)
connection.commit()
return("Record Updated successfully")
cursor.close()
else:
return {"invalide passphrase"}
Can you please tell me what's wrong with my code ?
if it's can help, here is my database structure (my db creation):
#Database connection
conn = sqlite3.connect("TAState15min.db")
cursor = conn.cursor()
#Create table
sql_query = """ CREATE TABLE TrendAnalyser (
id integer PRIMARY KEY,
state text,
date text
)"""
cursor.execute(sql_query)
#Create empty row with ID at 1
insert_query = """INSERT INTO TrendAnalyser
(id, state, date)
VALUES (1, 'Null', 'Null');"""
cursor.execute(insert_query)
conn.commit()
#Close database connexion
cursor.close()
**I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue... **
I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue...
I am creating a twitter bot that searches twitter for ticker symbols. I am then sending them to a database. The problem that I have here is that the list that is getting sent is not changing.
Maybe I have to keep connecting to the database, but I can not figure out where my problem is. Can anyone figure out how to make make the list of tickers be different everytime?
def searchTwit():
tweets = api.search("#stocks",count=100)
return tweets
print("connecting to database")
#connecting to the database
conn = pyodbc.connect(
"Driver={SQL Server};"
"Server=..............;"
"Database=master;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
tickList=[]
def getTicker(tweets):
for tweet in tweets:
if "$" in tweet.text:
x = tweet.text.split()
for i in x:
if i.startswith("$") and i[1].isalpha():
i.strip(".")
i.upper()
tickList.append(i)
# print(var_string)
def retrieveTickers():
for i in tickList:
cursor.execute('INSERT INTO master.dbo.TickerTable (TickerName) VALUES (?);', (i))
conn.commit()
# thing to run
print("about to do while ")
while True:
sleep(60 - time() %60)
print("searchtwit")
searchTwit()
theTweets = searchTwit()
getTicker(theTweets)
print("getting Tickers")
retrieveTickers()
print("sending tickers")
print(tickList)
tickList=[]
print(tickList)
You can connect to a remote database or on your local machine. Define which database you want to use, so in your database server be 127.0.0.1:PORT (that means that the database is your machine) (THE PORT will change depending on which SGDB you want
I'm writing a simple script that checks if user account is about to expire. I'm having a problem with an UPDATE query - it doesn't update, basically. All examples I've found on the internet seem to use tuples to update rows, however my case requires parameters to be apart from each other.
I'm completely new to Python (I started literally yesterday). My database is MySQL (almost all examples on the web use SQLite) and I can't change that. I use Python 3 and the server is running on Ubuntu 18.04. I tried replacing %s with ? or :variable. I also tried insecure way of doing this (SQL Injection vulnerable) and it didn't work either.
This is my current code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import mysql.connector
from mysql.connector import Error
import datetime
try:
mydb = mysql.connector.connect(
host="localhost",
user="rootery-root",
passwd="example",
database="playground"
)
sqlCursor = mydb.cursor()
sqlCursor.execute("SELECT id, email, last_email_date FROM users WHERE soon_expires = 1")
sqlResult = sqlCursor.fetchall()
setLastMailCmd = """UPDATE users SET last_email_date =%s WHERE id =%s"""
today = datetime.date.today()
for i in range(0, len(sqlResult)):
id = sqlResult[i][0]
email = sqlResult[i][1]
lastemaildate = sqlResult[i][2]
daydiff = lastemaildate - today
setLastMail = sqlCursor.execute(setLastMailCmd, (id, today))
if daydiff.days >= 30:
print "Sending mail and updating \"last_email_date\"."
setLastMail
else:
print "%s already received an e-mail this month - ignored." % email
setLastMail # debugging purposes
except mysql.connector.Error as error:
print("SQL connection error.".format(error))
finally:
if (mydb.is_connected()):
sqlCursor.close()
mydb.close()
print("Disconnected from database.")
print(today)
I expected it to update my table with data provided by the for loop, however it does nothing at all.
Try using functions more. You put everything in one place and it's not easy to debug.
Your problem is the way you use setLastMail # debugging purposes. It does nothing.
What would be better:
mydb = mysql.connector.connect(
host="localhost",
user="rootery-root",
passwd="example",
database="playground"
)
sqlCursor = mydb.cursor()
def set_last_email(id):
stmt = """UPDATE users SET last_email_date =%s WHERE id =%s"""
today = datetime.date.today()
sqlCursor.execute(stmt, (id, today))
And then just execute your set_last_email(id).
Remember to make cursor global, otherwise it won't be available in your function. Or acquire it directly in your function from global connection.
That's of course a dummy example, but you need to start somewhere :)
I'm trying to connect to a sql server remotely that runs a store procedure and returns a huge file as an output.
When I run the file locally on the SQLbox its fine and returns ~800,000 rows as expected, but when I try to run it using the mySQLdb library from python, it receives a truncated output of only ~6000 rows.
It runs fine for smaller data, so I'm guessing there's some result limit that's coming into play.
I'm sure there's some property that needs to be changed somewhere but there doesn't seem to be any documentation on the pypi library regarding the same.
For explanatory purposes, I've included my code below:
import MySQLdb
import pandas as pd
connection = MySQLdb.connect(sql_server,sql_admin,sql_pw,sql_db)
sql_command = """call function(4)"""
return pd.read_sql(sql_command, connection)
I was able to solve this using cursors. The approach I took is shown below and hopefully should help anyone else.
connection = MySQLdb.connect (host = sql_server, user = sql_admin, passwd = sql_pw, db = sql_db)
cursor = connection.cursor ()
cursor.execute("""call function(4)""")
data = cursor.fetchall()
frame = []
for row in data:
frame.append(row)
return pd.DataFrame(frame)
cursor.close ()
# close the connection
connection.close ()
I am not too new to python but new to discord.py. I have tried to go through the discord.py manual but did not find where I can store some temporary variable on a discord bot.
discord.py manual: http://discordpy.readthedocs.io/en/latest/api.html
For example, in PHP SESSION, we can store information on the SESSION(). Did discord.py has the same kind of things?
For example, if we have user "A" and user "B". "A" will be stored as an object of "A_Object", such as messages etc. Similar for "B_Object" but will be different from "A_Object". In discord.py, is there a function like that?
Thank you so much for the help!
You can use sqlite database. Write inside your def:
# define database
import sqlite3
conn = sqlite3.connect("my_database.db")
cursor = conn.cursor()
# get stored object from database
sql = "SELECT * FROM my_table WHERE field_1=?"
cursor.execute(sql, [(value_1)])
data = cursor.fetchall()
# if object does not exist, create it
if len(data) == 0:
sql = "INSERT INTO my_table VALUES (?, ?)"
cursor.execute(sql, [(value_1), (value_2)])
# if stored object exist and we need update it
elif ...:
sql = "UPDATE my_table SET field_2 = ? WHERE field_1 = ?"
cursor.execute(sql, [(value_2), (value_1)])
else:
# get data from first object
value_of_field_1 = data[0][0]
# get data from third object
value_of_field_2 = data[2][1]
# close database connection
conn.commit()
conn.close()
my_database.db - is a sqlite db file and should be stored in sa same folder with bot's .py file.