I am working on a Python script that makes use of SqlAlchemy library to execute Stored Procedure on SQL Server. I am passing XML contents to the stored procedure.
xml_text = "<root><element>some xml contents</element></root>"
query = PROCS['myStoreProc']
session = get_session()
result = session.execute(text(query, bindparams=[bindparam('xmlContent', value=xml_text)])).fetchall()
session.commit()
return result
I am able to get the query and session fine. But for some reason the command session.execute hangs. It neither returns any result nor any exception.
Related
I'm using Sqlite3 database in my Python application and query it using parameters substitution.
For example:
cursor.execute('SELECT * FROM table WHERE id > ?', (10,))
Some queries do not return results properly and I would like to log them and try to query sqlite manually.
How can I log these queries with parameters instead of question marks?
Python 3.3 has sqlite3.Connection.set_trace_callback:
import sqlite3
connection = sqlite3.connect(':memory:')
connection.set_trace_callback(print)
The function you provide as argument gets called for every SQL statement that is executed through that particular Connection object. Instead of print, you may want to use a function from the logging module.
Assuming that you have a log function, you could call it first :
query, param = 'SELECT * FROM table WHERE id > ?', (10,)
log(query.replace('?', '%s') % param)
cursor.execute(query, param)
So you don't modify your query at all.
Moreover, this is not Sqlite specific.
Have you seen this pandas sqlite error? ArgumentError("Could not parse rfc1738 URL from string '/')? It only happens in a NOSE test.
From the other questions on SO, the err seems to be related to sqlite. But I can't tell how since there are no others using a simple sqlite3 db like I am.
In my unit test, I call a class method that reads from an sqlite3 DB via pandas read_sql. It works perfectly in any python session as well as Jupyter notebook. But for some reason when I run the code through a nosetest, nose tells me there's an Argument Error. And I cannot reproduce it.
I've confirmed the DB is working properly. As I mentioned, the pd.read_sql works perfectly in any other setting.
In the method definition, I am doing the following read,
# get data
div_query = 'SELECT * FROM Dividends WHERE Symbol == "{stock}"'.format(stock = symbol)
div_history = pd.read_sql(div_query, con=dbcnx)
And in the NOSE test,
def test_shiftBeforeDividend():
# here is where the err occurs
result = filters.shiftBeforeDividend('DUK')
# result now equals ArgumentError("Could not parse rfc1738 URL from string '/'")
def setup():
try:
db = 'mydb.db'
test_class.connectToDB(db)
return test_class
except Exception as e:
return e
def teardown():
try:
filters.closeDBConnection(self.dbcnx[0])
except Exception as e:
return e
# startup
filters = setup()
Any ideas on how to eliminate the issue?
Turns out after some hair pulling that the error was a simple misplaced '/' in the db file path. Hence, sqlite could not connect to a DB and all calls to the DB resulted in an error. In all the help topics I read on similar subjects, it seems that this error always results from an improper DB reference (i.e., file path). So if you run into the issue, make sure your file path exists and is correct before trying operations on your DB.
I have NGINX UWSGI and WEB2PY installed on the server. Web2py application performing only one function by accessing the database and printing rows in the table.
def fetch():
import psycopg2
conn = psycopg2.connect(database="postgres",
user="postgres",
password="qwerty",
host="127.0.0.1")
cur = conn.cursor()
cur.execute("SELECT id, name from TEST")
rows = cur.fetchall()
conn.close()
return rows
When the function is called locally the table contents is returned.
But when I'm trying to call the function from remote machine I get an internal error 500.
One more interesting thing, is when function looks like this:
def hello():
return 'hello'
String 'hello' is returned. Starting adding it an import directive immediately causes error page to be generated.
Can any one please suggest the proper application syntax/logic?
My guess is that your MySQL service doesn't allow remote access. Could you check your MySQL configuration?
vim /etc/mysql/my.cnf
Comment out the following lines.
#bind-address = 127.0.0.1
#skip-networking
If there is no skip-networking line in your configuration file, just add it and comment out it.
And then restart the mysql service.
service mysql restart
Forgive the stupid question but have you checked if the module is available on your server?
When you say that the error appears in your hello function as soon as you try to import, it's the same directive import psycopg2?
Try this:
Assuming that fetch() it's defined on controllers/default.py
open folder views/default and create a new file called fetch.html
paste this inside
{{extend 'layout.html'}}
{{=rows}}
fetch.html is a view or a template if you prefer
Modify fetch() to return a dictionary with rows for the view to print
return dict(rows=rows)
this is very basic tough, you can find more information about basic steps in the book -> http://www.web2py.com/books/default/chapter/29/03/overview#Postbacks
I'm trying to use an SQLite insert operation in a python script, it works when I execute it manually on the command line but when I try to access it on the web it won't insert it in the database. Here is my function:
def insertdb(unique_id,number_of_days):
conn = sqlite3.connect('database.db')
print "Opened database successfully";
conn.execute("INSERT INTO IDENT (ID_NUM,DAYS_LEFT) VALUES (?,?)",(unique_id,number_of_days));
conn.commit()
print "Records created successfully";
conn.close()
When it is executed on the web, it only shows the output "Opened database successfully" but does not seem to insert the value into the database. What am I missing? Is this a server configuration issue? I have checked the database permissions on writing and they are correctly set.
The problem is almost certainly that you're trying to create or open a database named database.db in whatever happens to be the current working directory, and one of the following is true:
The database exists and you don't have permission to write to it. So, everything works until you try to do something that requires write access (like commiting an INSERT).
The database exists, and you have permission to write to it, but you don't have permission to create new files in the directory. So, everything works until sqlite needs to create a temporary file (which it almost always will for execute-ing an INSERT).
Meanwhile, you don't mention what web server/container/etc. you're using, but apparently you have it configured to just swallow all errors silently, which is a really, really bad idea for any debugging. Configure it to report the errors in some way. Otherwise, you will never figure out what's going on with anything that goes wrong.
If you don't have control over the server configuration, you can at least wrap all your code in a try/except and manually log exceptions to some file you have write access to (ideally via the logging module, or just open and write if worst comes to worst).
Or, you can just do that with dumb print statements, as you're already doing:
def insertdb(unique_id,number_of_days):
conn = sqlite3.connect('database.db')
print "Opened database successfully";
try:
conn.execute("INSERT INTO IDENT (ID_NUM,DAYS_LEFT) VALUES (?,?)",(unique_id,number_of_days));
conn.commit()
print "Records created successfully";
except Exception as e:
print e # or, better, traceback.print_exc()
conn.close()
I had a procedure that was not working.
If I tried to run: "BEGIN proc_name; END;" in SQL Developer or via script I had the same error.
I've fixed the procedure and now when I run that same command in SQL Developer, it's fine, but the script returns an error.
When I try:
...
sql = """EXEC proc_name"""
con = connection.cursor()
con.execute( sql )
...
I get DatabaseError: ORA-00900: invalid SQL statement, but probably is because of that: Problem with execute procedure in PL/SQL Developer and I'm not really worried about it.
What is really making me curious is when I try:
...
sql = """BEGIN proc_name;END;"""
con = connection.cursor()
con.execute( sql )
...
I get the same error that I had before fix the procedure.
Do you have any idea what is going on?
PS: This is a python script using cx_Oracle and I'm using Oracle 10g.
Try using the callproc() or callfunc() method on the cursor, instead of execute(). They are not exactly Py DB API compatible, but should do the job for cx_Oracle...