开发者

How do I insert some data into an Sqlite 3 database

开发者 https://www.devze.com 2023-03-14 22:04 出处:网络
Through the following code I am retrieving data from a Db, however I need to insert some data into a Sqlite3 database with this code before select statement. Can any one tell me where I need to put th

Through the following code I am retrieving data from a Db, however I need to insert some data into a Sqlite3 database with this code before select statement. Can any one tell me where I need to put the insert query and how can I execute it.

I am selecting the datas through the following code:

sqlite3* database;

- (void)initializeDatabase 
{   
    list=[[NSMutableArray alloc] init];
    BOOL success;
    NSFileManager *filemanager=[NSFileManager defaultManager];
    NSError *error;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory= [paths object开发者_开发知识库AtIndex:0];
    NSString *writablePath=[documentsDirectory stringByAppendingPathComponent:@"SymbolTalk.sqlite"];
    success=[filemanager fileExistsAtPath:writablePath];
    if(!success)
    {
        NSString *defaultDBPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SymbolTalk.sqlite"];
        success=[filemanager copyItemAtPath:defaultDBPath toPath:writablePath error:&error];
        if(!success)
        {
            NSAssert1(0,@"Failed to create writable databasefile withw message %@.",[error localizedDescription]);
        }
    }

    //Specify where to get the database from
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory= [paths objectAtIndex:0];
    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"SymbolTalk.sqlite"];

    //Open the database
    //might have to make database as property
    if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK)
    {
        const char *sql="select filename from scenes";
        sqlite3_stmt *statement;

        if(sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW) 
            {
                //NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]);
                [list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
            }
        }
    }
}


sqlite3_stmt *insert_statement = nil;

const char *sql = "INSERT INTO scenes (filename) VALUES(?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
    NSAssert1(NO, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}

sqlite3_bind_text(insert_statement, 1, [newFilename UTF8String], -1, SQLITE_TRANSIENT);

if (sqlite3_step(insert_statement) == SQLITE_DONE) {
    // code if all ok
} else {
    NSAssert1(NO, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}

sqlite3_reset(insert_statement);
sqlite3_finalize(insert_statement);
0

精彩评论

暂无评论...
验证码 换一张
取 消