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);    

//给出在db中插入值的代码

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 回答