views:

95

answers:

1

A very simple example I am trying to create as I grasp iPhone App programming.

First TableView shows the makes, selecting one takes user to different view(Can I use the same view to show models) to show models. If the models TableView, that displays the car models, allows user to add a new record, how and where do I capture the primary key(pk) of the parent key(make)?

Which method would I capture the primary key and how?

Sqlite database:

CREATE TABLE make(pk INTEGER PRIMARY KEY, parentkey INTEGER DEFAULT 0, title TEXT);

INSERT INTO make(title) VALUES('Honda');
INSERT INTO make(title) VALUES('Toyota');
INSERT INTO make(title) VALUES('Mazda');
INSERT INTO make(title) VALUES('Nissan');

INSERT INTO make(title, parentkey) VALUES('Civic', 1);
INSERT INTO make(title, parentkey) VALUES('Accord', 1);
INSERT INTO make(title, parentkey) VALUES('Corolla', 2);
INSERT INTO make(title, parentkey) VALUES('Corona', 2);
A: 

Via SQL, you can query the SQLITE_SEQUENCE table after every insert:

SELECT seq FROM SQLITE_SEQUENCE WHERE name = 'make'

This will only work reliably if you do your inserts and this select in the same transaction, locking the table so no one else can do an insert before you get the value from the sequence table.


Using the API (and not SQL):

sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);

(from http://www.sqlite.org/c3ref/last_insert_rowid.html)

Zach Wily