Sqlite and auto increment

64 views
Skip to first unread message

Christophe Capezzone

unread,
Jan 22, 2014, 2:10:15 AM1/22/14
to f3-fra...@googlegroups.com
Hi,

Sorry if it's a simple question but I tried to get the answer with the documentation, try to read this group and search on google without any success....

I have an sqlite database with an auto incremented field :
CREATE TABLE documents (
docid INTEGER PRIMARY KEY autoincrement,
title TEXT,
path TEXT,
thumb TEXT );

I want to insert a record with title, path and thumb data, and retreive the docid once done.

$files=new DB\SQL\Mapper($f3->get('DB'),'documents');
$files->title=$file;
$files->path=$path;
$files->thumb=$thumb;
$files->insert();
$docid=$files->get('docid');
echo "docid = $docid</br>";
$files->reset();

But each time, I got a empty docid. I checked directly on database with a select and the autoincrement works well. So, I assume, it's just a problem in my code :)

I tried to update or save after the insert, tried to find with _id..... I'm running out of ideas now. How to "refresh" the content of the cursor to get the value of autoincremente field ?

Thanks for your help,
Christophe.

Sn0opy

unread,
Jan 22, 2014, 2:59:33 AM1/22/14
to f3-fra...@googlegroups.com
Without executing another load(), your "docid" won't be populated. Beside that, I'm not sure, whether insert() should be used. Instead, use save() which is the "correct" way and does the same (and a bit more).

If you use save(), the _id property should be populated in the same mapper object, so your code should look like this:

$files=new DB\SQL\Mapper($f3->get('DB'),'documents');
$files->title=$file;
$files->path=$path;
$files->thumb=$thumb;
$files->save();
$docid=$files->_id;
echo "docid = $docid</br>";

Christophe Capezzone

unread,
Jan 22, 2014, 3:14:52 AM1/22/14
to f3-fra...@googlegroups.com
Thanks for your answer. I changed save() to insert() to see if I got any change.

I don't want to to get _id  (I know how to and it already works), but I want to get the value of docid, which is an autoincrement field, with a different value of _id.

ikkez

unread,
Jan 22, 2014, 4:17:42 AM1/22/14
to f3-fra...@googlegroups.com
if "docid" is your primary key $files->_id returns that value.. you don't have another _id column in your table, do you?

Christophe Capezzone

unread,
Jan 22, 2014, 4:43:15 AM1/22/14
to f3-fra...@googlegroups.com
ok got it !

My primary key cotains same thing as rowid, so _id
I believed it was 2 differents chronos.

Thanks !
Reply all
Reply to author
Forward
0 new messages