How do I use a Z Oracle Connection in a script?
Hello All, I am trying to save a long raw into a database. Z SQL Method pastes the parameters into the request, which is unsatisfactory because this parameter contains stray quotes and unprintable characters. I am trying to write a python script, given an oracle connection called Oracle_database_connection in the same folder. I currently have AttributeError: cursor on line 4 (really line 3) of: ======================== db=context.Oracle_database_connection; c=db.cursor() try: c.execute('update abs_acct_reference set profile=:p where id = :i', p=newProfile, i=acctId); c.commit() finally: c.close() =================== Help? J. Cone.
J. Cone writes:
Z SQL Method pastes the parameters into the request, which is unsatisfactory because this parameter contains stray quotes and unprintable characters. Unprintable characters should not pose problems. "dtml-sqlvar ... type=string" properly quotes embedded quotes. Thus, it should work!
I am trying to write a python script, given an oracle connection called Oracle_database_connection in the same folder.
I currently have AttributeError: cursor on line 4 (really line 3) of: Zope database connections are not themselves DB-API connections, but are wrappers around them.
You have in fact two nested wrappers: 1. the database adapter, derived from "Shared.DC.ZRDB.Connection". It is a persistent object stored in the ZODB. 2. the "db", a non-persistent wrapper around the DB-API connection Call call a database adapter to get the "db" wrapper. The "db", then, has a product specific way to get at the DB-API connection. Look at the code in "Products/ZOracleDA/db.py". Dieter
Hello Dieter, I think the problem is not quotes. I suspect it is caused by embedded NUL characters, because I am attempting to store binary data. Here are the last few lines from the trace file, and I will try your other information next. Thank you, J. Cone <assorted deallocation> * 995287742.487, 17, Cursor_getattr, 0x408a6de8, 0x405f051c ?= prepare, * * 995287742.488, 18, Cursor_getattr, 0x408a6de8, * * 995287742.488, 17, Cursor_prepare, 0x408a6de8, 0x408aa7f8, * * 995287742.488, 33, OCIStmtPrepare, 0x40894568, 0x408b3d9c ?= update abs_acct_ reference set profile = ', 41, * * 995287742.488, 34, OCIStmtPrepare, -1, * * 995287742.488, 36, RaiseOCIError, 1756, 0x407926a8 ?= ORA-01756: quoted strin g not properly terminated , * * 995287742.489, 17, ServerContext_getattr, 0x40883b50, 0x40476984 ?= rollback, * * 995287742.489, 18, ServerContext_getattr, 0x408a20b8, * * 995287742.489, 17, ServerContext_rollback, 0x40883b50, 0x4004caf0, * * 995287742.489, 33, OCITransRollback, 0, * * 995287742.490, 34, OCITransRollback, 0, * * 995287742.491, 17, ServerContext_rollback, * At 14:12 14/07/01 +0200, Dieter Maurer wrote: <snip>
Unprintable characters should not pose problems. "dtml-sqlvar ... type=string" properly quotes embedded quotes. Thus, it should work!
<other suggestion of traversing global variables snipped>
Hello Dieter, I am sorry to be so ignorant, but your second suggestion defeats me, so far. In my script, I have the lines: db0=Shared.DC.ZRDB.Connection c=db0.cursor which get me the errors: Error Type: NameError Error Value: Shared Please tell me which binding to look in for Shared. Thanks, J. Cone. At 14:12 14/07/01 +0200, Dieter Maurer wrote: <snip>
You have in fact two nested wrappers:
1. the database adapter, derived from "Shared.DC.ZRDB.Connection".
It is a persistent object stored in the ZODB.
2. the "db", a non-persistent wrapper around the DB-API connection
Call call a database adapter to get the "db" wrapper. The "db", then, has a product specific way to get at the DB-API connection. Look at the code in "Products/ZOracleDA/db.py".
Dieter
J. Cone writes:
I am sorry to be so ignorant, but your second suggestion defeats me, so far. Sorry to have caused confusion. Apparently, I do that sometimes...
db0=Shared.DC.ZRDB.Connection c=db0.cursor When I said, that a Zope Database Adapter instance is a "Shared.DC.ZRDB.Connection.Connection" instance, it did not mean, that you should access it this way.
I wanted to give you a hint, where to look for the sources. Looking at the sources (the available methods) gives you a feeling how to use it.
which get me the errors:
Error Type: NameError Error Value: Shared
"Shared" is a Python package. You must import it, before you can use it. As said above, you do not use it for your current problem, but if you need to, you would do something like: from Shared.DC.ZRDB.Connection import Connection I do not have the DCOracle sources here (at home), therefore the following is only right in principle. You will need to look at the sources to find out the details. Assume you are in an external method, and "da" already contains a Z OracleDA instance. You call it, to get the lower level wrapper: db= da() This "db" object has attributes that represent the low level DB-API connection and a cursor for it. Maybe the attributes are "cursor" (or "Cursor") and "connection" (or "Connection" or "Conn" or ...). Look at the sources in "Products/ZOracleDA/db.py"! Dieter
You are correct that the ZOracleDA object is a function object, although I cannot see from reading the source in either db.py or ZSQLMethods/SQL.py or Shared/DC/ZRDB/TM.py, where the object function is declared. (Oracle_database_connection is the name of something in the same folder, so I hope it is an instance of a class. It may come to show that I don't really understand OO) My attempt is now: db0=context.Oracle_database_connection db=db0() c=db.cursor and my error is now: - being prompted for a username and password - the administrator's account and password won't do - after three attempts or cancel, whichever happens first: Unauthorized ... File /usr/local/zope/Zope-2.3.3-src/lib/python/Products/PythonScripts/PythonScrip t.py, line 336, in _exec (Object: save_profile) (Info: ({'script': <PythonScript instance at 4086a3d0>, 'context': <r instance at 408b6ca8>, 'container': <Folder instance at 408684c0>, '_': <TemplateDict object at 408f7a58>, 'traverse_subpath': []}, (), {'newProfile': '\000\000\000\010\000\000<snip>\033', 'acctId': 56091}, None)) File Script (Python), line 5, in save_profile ... Unauthorized: cursor I have tried: - ticking every box in the Oracle connection - ticking every box in the Python Script - creating this __init__.py, which seems to be compiled: # Global module assertions for Python scripts from Products.PythonScripts.Utility import allow_module allow_module('ZOracleDA') allow_module('Shared.DC') allow_module('DCOracle2') - I have not done all of these simultaneously; only one-at-a-time Suggestions welcome :-) At 20:37 16/07/01 +0200, Dieter Maurer wrote:
J. Cone writes:
I am sorry to be so ignorant, but your second suggestion defeats me, so far. Sorry to have caused confusion. Apparently, I do that sometimes...
db0=Shared.DC.ZRDB.Connection c=db0.cursor When I said, that a Zope Database Adapter instance is a "Shared.DC.ZRDB.Connection.Connection" instance, it did not mean, that you should access it this way.
I wanted to give you a hint, where to look for the sources. Looking at the sources (the available methods) gives you a feeling how to use it.
which get me the errors:
Error Type: NameError Error Value: Shared
"Shared" is a Python package. You must import it, before you can use it. As said above, you do not use it for your current problem, but if you need to, you would do something like:
from Shared.DC.ZRDB.Connection import Connection
I do not have the DCOracle sources here (at home), therefore the following is only right in principle. You will need to look at the sources to find out the details.
Assume you are in an external method, and "da" already contains a Z OracleDA instance. You call it, to get the lower level wrapper:
db= da()
This "db" object has attributes that represent the low level DB-API connection and a cursor for it.
Maybe the attributes are "cursor" (or "Cursor") and "connection" (or "Connection" or "Conn" or ...).
Look at the sources in "Products/ZOracleDA/db.py"!
Dieter
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Hello Dieter, Please cancel the question below. I decided I was having the same problem as Jeff Hotz ([Zope] Python Script Permission Problem) and to adopt the same solution. I have extended my pluggable brain and made it go. The relevant code is: def saveProfile(self, dbase, profile): csr = dbase.cursor csr.execute("update fred set profile = :prof " "where id = :id", prof=Binary(profile),# Binary here prevents truncation id = self.id); dbase._finish() <dtml-call "saveProfile(Oracle_database_connection(), newProfile)"> Thanks for your help, J. Cone. At 20:30 16/07/01 +0100, J. Cone wrote:
You are correct that the ZOracleDA object is a function object, although I cannot see from reading the source in either db.py or ZSQLMethods/SQL.py or Shared/DC/ZRDB/TM.py, where the object function is declared.
<snip>
At 20:37 16/07/01 +0200, Dieter Maurer wrote: <snip>
I wanted to give you a hint, where to look for the sources. Looking at the sources (the available methods) gives you a feeling how to use it. <snip>
J. Cone writes:
... My attempt is now: db0=context.Oracle_database_connection db=db0()
c=db.cursor and my error is now: - being prompted for a username and password - the administrator's account and password won't do - after three attempts or cancel, whichever happens first: Unauthorized Low level DB-API wrappers are not designed to be used from any restricted environment (DTML, Python Script, PageTemplates, ...). They lack security declarations which makes all methods private. You can only use them from External Methods or Python Products.
Dieter
participants (2)
-
Dieter Maurer -
J. Cone