On Aug 26, 5:14 am, menuge <[EMAIL PROTECTED]> wrote:
> I have 2 tables:
> - info (id: integer, cfg: integer)
> - info_with_error (id:integer, cfg: varchar(32))
>
> I'd like to check the query before inserting into table. For instance:
>   INSERT INTO info VALUES("STRING")
> If my query has an error, i put the query in the table
> info_with_error...

Michael said it best, but my 2 bits:

info = Table('info', metadata,
    Column('id', Integer, primary_key=True),
    Column('cfg',Integer) )
info_with_error = Table('info_with_error', metadata,
    Column('id', Integer, primary_key=True),
    Column('cfg', String(200)))

Non-ORM:

stmt = info.insert()
try:
    stmt.execute(cfg="string")
except sa.exc.DataError, e:
    print 'error ',e.message
    info_with_error.insert().execute(cfg=str(stmt) + `e.params`)
    #writes:   "INSERT INTO info (id, cfg) VALUES (?, ?)['string']"


If you're using ORM to do inserts, you can also define a "property" in
the class which intercepts all puts on the value.
(not sure how you get the sql query text when using ORM inserts
however.)

class Info(object):
    def get_cfgint(self):
        return self.cfg
    def set_cfgint(self, val):
        self.cfg = int(val)
        #throws ValueError if bad, rather than have db error.
    cfgint = property(get_cfgint, set_cfgint)

and you can use .cfgint as if it were a column (but only on Info
instances, not on non-ORM statements).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to