Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

SQLite autoincrement not working

edited September 2013 in Apps

I expect that inserting new sqlite record which has an id (integer primary key autoincrement) the new value is automatically inserted, but that doesn't happen. What can be wrong? If assign manually the next value the record is inserted as expected.

Comments

  • The field needs to be null when inserting a new record, so make sure to omit that field and it should be okay. Here's a quick example you can paste into the DB Manager:

    create table foo (id integer primary key, name char(32) not null);
    insert into foo values (null, "One");
    insert into foo values (null, "Two");
    insert into foo (name) values ("Three");
    select * from foo;
    drop table foo;
    

    What's the code you're using to insert new records?

  • edited September 2013

    I'm using that one created by CRUD generator.

        $article = new articles\Article (array (
            'id' => $_POST['id'], 
            'title' => $_POST['title'], 
            'description' => $_POST['description'], 
        ...
            'notes' => $_POST['notes']
        ));
        $article->put ();
    
    

    Thanks.

  • Ah yes, the default creates a text input field and insert line for each field in the crud-app command. If you remove the input from the view you should be able to remove the 'id' => $_POST['id'], line from handlers/add.php and it should work.

  • I plan on adding hints on field types to the crud-app command down the road, so it can generate smarter CRUD out of the box.

  • Solved, thank you. Of course, a smarter CRUD generator could fill in the gaps of a poor user like me :)

Sign In or Register to comment.