Oracle Python callproc() Script stops - python

I have a hughe problem.
I created a procedure and stored it in my OracleDB.
Now I want to execute the procedure, but its not working. There is no Error-Message Occuring, it just stops and dont continue after the first callproc call. Anybody any ideas?
!Autoincrement is enabled for the whole script!
import cx_Oracle dsn= cx_Oracle.makedsn("**********,1521,"orcl")
db= cx_Oracle.connect('******','*******',dsn)
db.autocommit = True cur = db.cursor()
cur.callproc("UPDATE_MISSING_VALUES", ['GENDER','***_PRE',1])
The Procedure is found and if I change the parameters, an SQL Error occurs. But if I let it like that, nothing happens.
If I run that query in SQL-Developer, it works just fine

It works now, I have actually now idea why, but the most important thing is that it works.
Is it possible, that the reason is that I made a
COMMIT;
Statement in my DB?? After that it worked suddenly.

Related

Can't get my python script to work/play audio when executed in crom

I'm having lots of trouble with a pythron script that I'm trying to run through crom.
The python script does a few things. It starts by importing modules for SQL, grabs information from an SQL database, turns the information into a text-to-speech string, and then reads it out loud. I currently have the server and the script on a raspberry pi, and when I run the script through the command prompt, it works fine.
However, I can't seem to get the script to run through cron. I've never used cron before, so I don't know much about how to debug it. Whenever I use grep CRON /var/log/syslog, it tells me that its executed when it's supposed to, so it's difficult to tell if its an error with the script running, or if the audio isn't playing. I'm happy to try out any potential solutions, and I'm a beginner to crom, so I wouldn't be surprised if it was something minor that I haven't come across yet. Any help is appreciated!
Edit 1: Here is a minimal reproducible example:
My python script effectively is:
# python sql commands
import pymysql
import os
from gtts import gTTS
#Please keep in mind that this information is just a placeholder, and not what is actually in the script - the script works as intended
db = pymysql.connect(host = "localhost",user = "root",password = "", database = "mydb")
cursor = db.cursor()
sql = "SELECT * FROM table"
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results
name = row[0]
atext = name
speech = gTTS(text = atext, lang = "en")
speech.save("speech.mp3")
os.system("omxplayer -o local speech.mp3")
db.close()
In the crontab, I have it set to run each minute:
* * * * * python3 /path/to/file/"Assignment Grabber.py"
The goal is for the python script to read the text out loud. It does this perfectly when I manually run the python script through the terminal. However, putting it into the crontab and waiting results with nothing.

I am new to python. I tried running a simple while loop but am receiving syntax error

I tried running the below code but VS Code is showing syntax error. I checked on internet and notes but found the loop is fine.
i = 1
while i <= 5:
print(i)
i = i + 1
While loop showing syntax error
I don't think there is anything wrong with your code but you should try to create a new folder preferably outside of Appdata. or One drive folder
There isn't anything wrong with your actual code. When I run it it executes as you would expect. I think the problem must be the way you are executing the program. I think you are attempting to run it in the python interpreter. Where it says "2:Python" you want it to be like "cmd" or "Code" or something and then you can just type in python loop.py maybe try clicking the plus next to it or select the first option in the dropdown.

How do you force Flask to query database on each GET request?

I have a very simple piece of code:
#app.route('/read')
def db_read():
dbcursor.execute('SELECT * FROM books;')
result = dbcursor.fetchall()
return render_template('read.html', result=result)
When I start the server, everything works fine, however, if I make the changes to the database while the app is running, the changes are not reflected on /read page, the app has to be restarted to reflect them.
I expect the code to query database each time I refresh the page and the current behavior seems very counter-intuitive to me. Can someone please help?
Have you tried setting TEMPLATES_AUTO_RELOAD to true in your config? It should toggle ON when you run in debug mode but it seems like template reloading has always been a little buggy.
You should also be using the flask run --debug command as well. app.run has issues that the run command copes with.
Your cursor is intialized withing the app instance hence queries executed are saved to the instance as read only, so changes reflected to the database are persistent within the instance.
There are several workarounds to overcome this isssue.
I would recommend you to create separate module for database instance and import where ever required.
database.py
mydb = mysql.connector.connect(
host=cfg['host'],
user=cfg['user'],
passwd=cfg['password'],
database=cfg['database']
)
db = mydb.cursor()
app.py
from database import db
#app.route('/read')
def db_read():
db.execute('SELECT * FROM books;')
result = db.fetchall()
return render_template('read.html', result=result)

Cursor.execute does not work in Python

I am using MySQLdb package in Python to update my database. I have a simple update command as follows :
update_query = "update user_details set `address`='%s' where `id`='%s'"
cursor.execute(update_query, (address, id1))
print(cursor._last_executed)
Here is the command executed :
update user_details set `address`='35, Chikmagalur' where `id`='242069'
The program runs fine without error. However, the database is not getting updated. The same command works when I run as an SQL query on PHPMyAdmin.
Any idea what could be the issue ?
this is a duplicate of ...
sql transactions needs to be committed, either explicitly or implicitly.
either issue a commit command explicitly
cursor._get_db().commit()
setting the connection to autocommit when opening the connection is also an option.

What is wrong with my string format? (Even WITH the trailing comma in the parameter)

First I tried:
sqlite3.cursor.execute("select * from mytable where mycol=?", (myval,))
when I execute it in the shell it works. In my class method it tells me:
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 12 supplied.
Then I tried:
sqlite3.cursor.execute("select * from mytable where mycol='%s'" % myval)
when I execute it in the shell it works. In my class method it tells me:
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 12 supplied.
So I decided to just format the string first and then pass that in. Like this:
sqlcmd = "select * from mytable where mycol='%s'" % myval
when I execute it in the shell it works. In my class method it tells me the same error:
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 12 supplied.
My method is defined like this:
def mymethod(self, myval):
cur = self.conn.cursor()
cur.execute( "..." ... )
....
What am I missing?
This was a "know your tools" problem. As I mentioned above in my comment, my issue was not understanding when/how pyCharm reloads modified code. I was testing my class in a python console in pyCharm. But after I changed my code it does not automatically reload the python code, but runs the cached code.
But the part that really threw me off was when it reports an error, it pulls the source code line from the current source code, not from the cached code. So my first time through I forgot the trailing command ...(myval,). When I got the error I realized my mistake and added it. I reran it and it displayed the same error, but pulled the source code in the error from my modified code. But this was not the cached code that was being executed.
I ran it on the command line and it worked (as expected). It was just not realizing that I needed to reload the python console when I made a code change.

Categories