Running Python script through Crontab, but SQL database not updating - python

I have a Python script to update a SQL database. When I run the script on Terminal manually, everything works fine. But when I run this on a crontab, the database is not getting updated.

Related

Execute mysql commands in linux terminal using Python

I would like to automate the setup of a database and I would like to do it by using a python script to execute some commands in a linux terminal.
But I cannot see any way of executing commands in the terminal after connection to mysql database.
Below you can see a part of the script:
from time import sleep
from os import system
print("Setting up the database...\n")
system("sudo mysql -u root")
sleep(2)
This starts mysql, and after this none of the commands I try to execute from python get executed.
For example, I would like to run commands like these:
system("CREATE DATABASE mydatabase;")
system("CREATE USER 'user'#'localhost' IDENTIFIED BY '[PASSWORD]';")
system("GRANT ALL PRIVILEGES ON mydatabase.* TO 'user'#'localhost';")
Is this possible?
Edit 1: Potential solution as suggested by #AndHeFallen:
In python I create an sql file named db.sql, then run this in the terminal:
with open("db.sql","w") as db:
db.write("CREATE DATABASE mydatabase;\n")
db.write("CREATE USER 'user'#'localhost' IDENTIFIED BY '[PASSWORD]';\n")
db.write("GRANT ALL PRIVILEGES ON mydatabase.* TO 'user'#'localhost';\n")
system("sudo mysql -u root < db.sql")
I don't think it's possible to do that. If you want to interact with your MySQL database with a Python script, you'll need to connect directly to your db with an api like MySQLdb. (I may be wrong but the way you want to do may cause security issues)

CommandError: You appear not to have the 'psql' program installed or on your path. MacOS Catalina

I am working on a django web app and trying to connect and verify connection to a PostgreSQL database I had created to store webscraped data I'd like the webapp to be able to query. I have correctly set up the database in Settings.py
When I enter python manage.py dbshell my Atom terminal returns
CommandError: You appear not to have the 'psql' program installed or on your path.
As per instructions elsewhere, I tried running this is iTerm2
export PATH="/Applications/Postgres.app/Contents/Versions/12.3/bin:$PATH" but nothing seemed to change and I am still receiving the error.
I have PSQL 12.3 installed on my computer through postgress.app and it is functioning normally.

Python Script gets ORA-03150 but same procedure runs fine from sqldeveloper

My Python Script fails while running a stored procedure that accesses a view that uses a DB link to access a remote table.
That stored procedure works fine when run from Oracle SQL Developer but not when run from the python script.
When run from SQL Developer the stored procedure takes about a minute to run but fails in the python script after about 16 minutes.
It throws this error:
ORA-03150: end-of-file on communication channel for database link
Why would it fail from python and not sql developer? Why is it slower in python? It is logging in with the same userid in both.

Python Won't Run Even Through It Is In the Path

I have a SQL Server Agent job that executes some python scripts using CmdExec. Everything is set up with a proxy account as expected.
When I run the job I get:
Message
Executed as user: domain\proxyaccount. 'python' is not recognized as an internal or external command, operable program or batch file. Process Exit Code 1. The step failed.
I'm using Anaconda and Python is in the system PATH variable. When I run python from command line, it works. When I run python cutting and pasting the specific command from the job, it works. When I use runas to mimic the proxy account it works. The only place Python doesn't run is form inside the job.
What else do I need to look at to trouble shoot this issue?
You should restart SQL Server Agent after you installed Python on the server.
It is necessary for SQL Server Agent to load new environment variables, including the updated PATH with Python in it.
There are also suggestions to restart SQL Server too, but I believe restarting SQL Server Agent will be enough.

Insert into DB using remote server

I am using a search API to fetch some results. These results are inserted into the database. The only issue is it takes too much time. What I want to do is get my script running, even when I close my laptop it should be running and inserting records into the remote database. Does running the script on the server like this helps:
python myscript.py &

Categories