|
From: J. M. C. <jm...@do...> - 2005-04-18 23:12:01
|
If I attempt to:
cursor.execute('create database blah...')
I get the following error:
libpq OperationalError: ERROR: CREATE DATABASE cannot run inside a
transaction block
How can I accomplish my objective?
|
|
From: J. M. C. <jm...@do...> - 2005-04-18 23:35:18
|
If I attempt to:
cursor.execute('create database blah...')
I get the following error:
libpq OperationalError: ERROR: CREATE DATABASE cannot run inside a
transaction block
How can I accomplish my objective?
|
|
From: J. M. C. <jm...@do...> - 2005-04-18 23:43:26
|
Here's one way to do it, though kludgey:
cursor.execute('commit') # note that this closes our transaction, so
that the following works:
cursor.execute('create database blah')
J. Michael Caine wrote:
> If I attempt to:
> cursor.execute('create database blah...')
> I get the following error:
> libpq OperationalError: ERROR: CREATE DATABASE cannot run inside a
> transaction block
>
> How can I accomplish my objective?
>
|
|
From: Andrew M. <an...@ob...> - 2005-04-20 03:51:20
|
>> If I attempt to:
>> cursor.execute('create database blah...')
>> I get the following error:
>> libpq OperationalError: ERROR: CREATE DATABASE cannot run inside a
>> transaction block
>>
>> How can I accomplish my objective?
>
>Here's one way to do it, though kludgey:
> cursor.execute('commit') # note that this closes our transaction, so
>that the following works:
> cursor.execute('create database blah')
The method I use is:
db = PgSQL.connect(...)
db.autocommit = True
curs = db.cursor()
curs.execute('....')
db.close()
--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
|
|
From: Milton i. <min...@gm...> - 2005-05-12 15:33:29
|
El 12/05/05, Milton inostroza<min...@gm...> escribi=F3:
> db =3D PgSQL.connect(...)
> db.autocommit =3D True
Debes tener cuidado con esta linea db.autocommit=3DTrue, ya que si
quieres insertar por ejemplo en 3 tablas y est=E1s est=E1n relacionadas te
producir=E1n un gran dolor de cabeza...observa:
=20
table direccion, table trabajador, el trabajador tiene una direccion y
direccion puede tener a uno o muchos trabajadores (esta es la
relacion), entonces en codigo quedar=EDa de la siguiente manera
=20
try:
insertar en direccion
insertar en trabajador (utilizando la pk de direccion como fk
en trabajador)
except:
sys.exc_info()[1] (imprime el tipo de exception)
=20
ahora, pregunta que pasa al utilizar db.autocommit=3DTrue....imaginate
que en direccion se inserte todo bien y en trabajador exista un
problema (imaginate que existe un campo int y el usuario lo ingresa
como cadena) entonces trabajador no se inserta y te manda la exception
(ahi todo bien)....PERO esto es erroneo ya que ingresaste una
direccion pero no a un trabajador ahora con autocommit el rollback no
funciona, entonces te quedaste con una direccion que no es de nadie y
eso es un ERROR GRAVE en un sistema de verdad.
=20
En mi sistema elimine el autocommit=3DTrue y trabajo de esta forma, me
da menos dolores de cabeza y la curva de verificaciones de datos
decrece un monton (cantidad de if para verificar que los campos sean
correctos).
=20
bueno ahi va el codigo
=20
db =3D PgSQL.connect(...)
cursor=3Ddb.cursor()
=20
try:
sql =3D"""
INSERT INTO direccion
(nombre_comuna,nombre_calle_direccion,
numero_direccion,block_direccion,
departamento_direccion
)
VALUES ('%s','%s','%s','%s','%s')
"""%(
self.comboboxentryComuna.child.get_text().upper(),
self.entryCalle.get_text().upper(),
self.entryNumero.get_text(),
self.entryBlock.get_text().upper(),
self.entryDepartamento.get_text()
)
cursor.execute(sql)
=20
sql =3D"""
INSERT INTO trabajador
VALUES("""+ ",".join(["'%s'" %(n1) for n1 in trabajador])=
+")"
self.cursor.execute(sql)
=20
db.commit()
except:
print sys.exc_info()[1]
db.rollback()
=20
db.close()
=20
si te das cuenta realizo un commit() cuando todas las inserciones se
realizan de forma satisfactoria, en el except pongo imprimir el error
y un rollback para eliminar cualquier transaccion que se haya
realizado y que no me sirva, ejemplo: que se haya insertado bien
direccion y mal trabajador, ESO no me sirve entonces aplico un
rollback().
=20
my language is Spanish-Chile, I did not write in ingles because it was
occupied making a task. I hope that my aid serves to you.
=20
--
Milton Inostroza Aguilera
Secretario Academico Centro de Alumnos
Encargado de Auspicios y Patrocinios - 6to. Encuentro Nacional de Linux
Desarrollador de RemuneX (sistema de remuneraciones amparado bajo GPL)
|
|
From: Karsten H. <Kar...@gm...> - 2005-05-12 16:00:19
|
> sql ="""
> INSERT INTO trabajador
> VALUES("""+ ",".join(["'%s'" %(n1) for n1 in trabajador])+")"
> self.cursor.execute(sql)
Don't do that.
Do
sql = "... where ... = %s and ... = %s and ..."
self.cursor.execute(sql, trabadajor)
Otherwise people can run SQL injection attacks unless you are
*really* careful.
Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346
|