如何通过iPhone项目中的Obj-C方法修改SQLite数据库中的表格

3

大家好

我需要在现有的表中添加一列,那么如何通过obj c更改sqlite数据库中的表结构呢?我正在使用以下代码向表中插入数据,同样地,我该如何编写更新表的方法?

- (void) InsertRecord {


if(addStmt == nil) {
    const char *sql = "insert into tbl_Users(FirstName,MiddleName) Values(?,?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [strFirstName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [strMiddleName UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(addStmt, 3, [strLogin UTF8String], -3, SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    //productID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);

能有人帮助我吗?提前谢谢。


嗨,你试过使用“sqlitepersistentobjects”吗?https://github.com/everzet/sqlitepersistentobjects,我认为它对于操作SQLite非常有用。 - Michael Bai
这就是我使用CoreData的好处,它会根据用户模式版本自动添加列...由于这个原因,我从来没有写过sqlite更新表... - Vincent Guerci
1
只有受虐狂才会在Objective-C中直接使用SQLite C API。请改用FMDB(一个SQLite封装库)或CoreData(一个对象图管理器)。 - Dave DeLong
有没有人能给我一个简单的可执行测试项目?我尝试了那个链接,但那些代码会崩溃。提前谢谢。 - Kiran
1个回答

18

这可能有些用处。 头文件

NSString *databasePath;

.m文件 //调用这些方法

    [self checkAndCreateDB ];
    [self alterDB];

-(void)checkAndCreateDB {
    NSString* databaseName = @"MasterDB.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success1;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success1 = [fileManager fileExistsAtPath:databasePath];
    if(success1) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

-(void) alterDB{
    sqlite3 *database;
    sqlite3_stmt *statement; 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {

        NSString *updateSQL = [NSString stringWithFormat: @"ALTER TABLE User ADD COLUMN testColumn TEXT"];
        const char *update_stmt = [updateSQL UTF8String];
        sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);

        if(sqlite3_step(statement)==SQLITE_DONE) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB altered" message:@"Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];
            [alert release];
            alert=nil;

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Updation" message:@"DB not Altered" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            [alert release];
            alert=nil;
        }   
        // Release the compiled statement from memory
        sqlite3_finalize(statement);    
        sqlite3_close(database);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接