Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Sqlite管理iPhone上对象中的数据库关系_Iphone_Sqlite_Uitableview - Fatal编程技术网

如何使用Sqlite管理iPhone上对象中的数据库关系

如何使用Sqlite管理iPhone上对象中的数据库关系,iphone,sqlite,uitableview,Iphone,Sqlite,Uitableview,例如,如果我能够显示包含所有电影和流派的UITableView。单击电影上的“编辑”应加载一个新视图(加载编辑视图是否正确?),其中包含一个文本框和某种选择器或另一个视图来选择类型。选择类型后,如何捕获要保存在电影表中的GenreId? CREATE TABLE movie(pk INTEGER PRIMARY KEY, title TEXT, genreId INTEGER); INSERT INTO make(title) VALUES('GoldFinger', 1); INSERT I

例如,如果我能够显示包含所有电影和流派的UITableView。单击电影上的“编辑”应加载一个新视图(加载编辑视图是否正确?),其中包含一个文本框和某种选择器或另一个视图来选择类型。选择类型后,如何捕获要保存在电影表中的GenreId?

CREATE TABLE movie(pk INTEGER PRIMARY KEY, title TEXT, genreId INTEGER);

INSERT INTO make(title) VALUES('GoldFinger', 1);
INSERT INTO make(title) VALUES('Octopussy', 1);
INSERT INTO make(title) VALUES('Love Story', 3);
INSERT INTO make(title) VALUES('Where Eagles Dare', 1);
INSERT INTO make(title) VALUES('Zombies', 2);

CREATE TABLE genre(pk INTEGER PRIMARY KEY, title TEXT);

INSERT INTO genre(title) VALUES('Thriller');
INSERT INTO genre(title) VALUES('Horror');
INSERT INTO genre(title) VALUES('Romance');
我使用Sqlitebooks示例作为指导

// Movie.h
@interface Movie : NSObject {
// Opaque reference to the underlying database.
sqlite3 *database;
// Primary key in the database.
NSInteger primaryKey;
// Attributes.
NSString *title;
NSInteger *genreKey;
// Internal state variables. Hydrated tracks whether attribute data is in the object or the database.
BOOL hydrated;
// Dirty tracks whether there are in-memory changes to data which have no been written to the database.
BOOL dirty;
NSData *data;
}
// Property exposure for primary key and other attributes. The primary key is 'assign'     because it is not an object,
// nonatomic because there is no need for concurrent access, and readonly because it     cannot be changed without
// corrupting the database.
@property (assign, nonatomic, readonly) NSInteger primaryKey;
// The remaining attributes are copied rather than retained because they are value     objects.
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSInteger *genreKey;
// Finalize (delete) all of the SQLite compiled queries.
+ (void)finalizeStatements;
// Creates the object with primary key and title is brought into memory.
- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db;
// Inserts the book into the database and stores its primary key.
- (void)insertIntoDatabase:(sqlite3 *)database;
// Brings the rest of the object data into memory. If already in memory, no action is     taken (harmless no-op).
- (void)hydrate;
// Flushes all but the primary key and title out to the database.
- (void)dehydrate;
// Remove the book complete from the database. In memory deletion to follow...
- (void)deleteFromDatabase;

// Movie.m
// Flushes all but the primary key and title out to the database.
- (void)dehydrate {
    if (dirty) {
    // Write any changes to the database.
    // First, if needed, compile the dehydrate query.
    if (dehydrate_statement == nil) {
        const char *sql = "UPDATE movie SET title=?, genreid=? WHERE pk=?";
        if (sqlite3_prepare_v2(database, sql, -1, &dehydrate_statement, NULL) != SQLITE_OK) {
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }
    }
    // Bind the query variables.
    sqlite3_bind_text(dehydrate_statement, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(dehydrate_statement, 2, [genreId UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_int(dehydrate_statement, 3, primaryKey);
    // Execute the query.
    int success = sqlite3_step(dehydrate_statement);
    // Reset the query for the next use.
    sqlite3_reset(dehydrate_statement);
    // Handle errors.
    if (success != SQLITE_DONE) {
        NSAssert1(0, @"Error: failed to dehydrate with message '%s'.", sqlite3_errmsg(database));
    }
    // Update the object state with respect to unwritten changes.
    dirty = NO;
}
// Release member variables to reclaim memory. Set to nil to avoid over-releasing them 
// if dehydrate is called multiple times.
[movie release];
movie = nil;
[data release];
data = nil;
// Update the object state with respect to hydration.
hydrated = NO;
}
@end

这并不能直接回答您的问题,但是,我建议您研究3.0 SDK中添加的新CoreData API。它们将允许您对关系数据进行建模,并抽象出任何手动SQL工作,从而减少代码,如果操作正确,则性能会更好。

您是否在询问如何从用户选择的UI元素中获取流派id,或者使用什么SQL语句用流派id更新数据库?为了简单起见,假设有两个屏幕。屏幕1有显示标题和类型的电影列表。点击(触摸)任何标题的流派,屏幕2显示流派列表。选择流派,您将返回到屏幕1,新的流派现在被选中。现在我想把这个标题和新的GenreId一起保存回数据库。希望这能让我的问题更清楚一点。