[Tutor] insert queries into related tables referencing foreign keys using python
Joel Goldstick
joel.goldstick at gmail.com
Sat Dec 24 17:34:32 CET 2011
On Sat, Dec 24, 2011 at 9:57 AM, Monte Milanuk <memilanuk at gmail.com> wrote:
> Lie Ryan <lie.1296 <at> gmail.com> writes:
>
>> In python-sqlite, the rowid of the
>> last insert operation can be queried using cursor.lastrowid. Therefore,
>> you can query the lastrowid, right after the insert, to find the primary
>> key of the value you had just inserted. So, in code:
>>
>> ...
>> cur = conn.execute('INSERT ... ')
>> pk = cur.lastrowid
>> ...
>>
>> or even:
>>
>> ...
>> pk = conn.execute('INSERT ... ').lastrowid
>> ...
>>
>> Be careful that in multithreaded program, each thread should have their
>> own cursors, or otherwise another thread could possibly do another
>> insert before you can query the lastrowid.
>>
>
> okay, this touches on something that had been worrying me a bit... whether
> another insert could hit before I queried for the lastrowid, regardless of how I
> did it. So you're saying that as long as I have the one cursor open, the
> lastrowid it gets will be the lastrowid from its operations, regardless of other
> commits or transactions that may have happened in the meantime?
>
>>
>> In general, despite the superficial similarities, most database engine
>> wrappers have their own ways of doing stuffs. Generally, you need a
>> full-blown ORM to smooth out the differences.
>>
>
> So... what would be considered a 'full-blown' ORM? SQLobject or SQLalchemy...
> or something else?
>
> Thanks,
>
> Monte
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
Django has an ORM and it works with sqlite3, mysql and I think postgress
Although Django is a full framework for writing web apps the various
modules can be used together or separately.
http://www.djangobook.com/en/2.0/chapter05/ talks about models and the
ORM
--
Joel Goldstick
More information about the Tutor
mailing list