1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"register.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 
sqlite3_stmt *selectstmt;
if(!success)
        {
             NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"register.sqlite"];
            success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

            if (!success) 
                NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    sql = "select lastname,email,firstname from reg_FORM";

    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

         while(sqlite3_step(selectstmt) == SQLITE_ROW) {

             lastname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                    email=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                    firstname=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
                    NSLog(@"----%@",lastname);
                    NSLog(@"----%@",email);
                    NSLog(@"----%@",firstname);

                }
        sqlite3_finalize(selectstmt);
            }     
        sqlite3_close(database);
        }

このコードを使用してdbの値を取得していますが、dbにデータを挿入する方法を知っていました。私はこれを以下のコードで試していますが、機能しません

const char *sql = "insert into reg_FORM (firstname,lastname,email,company,phone) VALUES (aaa,aaa,aaa,aaaa,1223)";
            sqlite3_exec(database, [[NSString stringWithFormat:@"insert into reg_FORM (firstname,lastname,email,company,phone) VALUES (aaa,aaa,aaa,aaaa,1223)"] UTF8String], NULL, NULL, NULL);    

//データベースに値を挿入するためのコードを提供します

4

2 に答える 2

1
{
    BOOL returnValue = YES;
    sqlite3_stmt *insertStmt = nil;
    sqlite3 *UserDB ;   
    if (sqlite3_config(SQLITE_CONFIG_SERIALIZED) == SQLITE_OK)
    {
        NSLog(@"Can now use sqlite on multiple threads, using the same connection");
    }

    int ret = sqlite3_enable_shared_cache(1);
    if(ret != SQLITE_OK)
    {

    }
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([app.dataBasePath UTF8String], &UserDB) == SQLITE_OK)
    {
            if(insertStmt == nil) 
            {   
                NSString *strValue = [NSString stringWithFormat:@"insert into languagemaster Values(?,?)"];
                const char *sql = [strValue UTF8String];
                if(sqlite3_prepare_v2(UserDB, sql, -1, &insertStmt, NULL) != SQLITE_OK)
                {
                    NSLog(@"Error while creating insertStmt in tblUserAccount %@", [NSString stringWithUTF8String:(char *)sqlite3_errmsg(UserDB)]);
                    returnValue = NO;
                }
            }

            if(sqlite3_bind_int(insertStmt, 1, langid) ) // langid is int
            {
                return NO;
            }
            if(sqlite3_bind_text(insertStmt, 2, [strLanguageName  UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK)   // strLanguageName is string
            {
                return NO;
            }   

            if(SQLITE_DONE != sqlite3_step(insertStmt)) 
            {           
                NSLog(@"Error while Executing insertStmt in tblLocation %@", [NSString stringWithUTF8String:(char *)sqlite3_errmsg(UserDB)]);           
                returnValue = NO;
            }

            sqlite3_reset(insertStmt);

            if (insertStmt) 
            {
                sqlite3_finalize(insertStmt);
                insertStmt = nil;
            }
    }
    sqlite3_close(UserDB);  

    return returnValue;                           
}
于 2012-09-05T11:26:48.020 に答える
0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"register.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 
sqlite3_stmt *selectstmt;
if(!success)
{
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"register.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success) 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    const char *sql = "insert into reg_FORM (firstname,lastname,email,company,phone) VALUES ('sdcbb','bbbb','bbbb','bbbb',1111122)";
    sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL); 
    if(sqlite3_step(selectstmt)==SQLITE_DONE)
    {
        NSLog(@"insert successfully");
    }
    else
    {
        NSLog(@"insert not successfully");

    }


    sqlite3_finalize(selectstmt);
    sqlite3_close(database);
}
于 2012-10-08T10:46:24.853 に答える