objective-c - Objective C 中的 Sqlite 数据库插入语句

标签 objective-c ios xcode sqlite insert

我尝试只向我的 sqlite 数据库中插入两个整型变量。我创建了一个名为 ups.sqlite 的数据库,它有一个表 (upssTable) 并且该表有两列。但是当我打开/Users/ds/Library/Application Support/iPhone Simulator/5.0/Applications/FCB4B455-4B7F-4C47-81B6-AC4121874596/SqliteDeneme.app/ups.sqlite 时,ups.sqlite 中没有数据。我的代码在这里:

- (IBAction)buttonClick:(id)sender {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ups.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

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

NSLog(@"database path %@",dbPath);


if(!(sqlite3_open([dbPath UTF8String], &cruddb) == SQLITE_OK))
{
    NSLog(@"An error has occured.");
}

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

    NSString *  str1 =@"1";
    NSString * str2 =@"1";


    const char *sql = "INSERT INTO upssTable (column1, column2) VALUES (?,?)"; 

    NSInteger result = sqlite3_prepare_v2(cruddb,sql, -1, &stmt, NULL);
    NSLog(@"upss %s\n", sqlite3_errmsg(cruddb));

    if(result == SQLITE_OK)        
    {
        sqlite3_bind_int(stmt, 1, [str1 integerValue]);
        sqlite3_bind_int(stmt, 2, [str2 integerValue]);

    }
    else
    {
        NSAssert1(0, @"Error . '%s'", sqlite3_errmsg(cruddb));                        
    }
    sqlite3_reset(stmt);
    sqlite3_finalize(stmt);
    sqlite3_close(cruddb);
}
}

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"ups.sqlite"];
}

我该如何解决这个问题?请帮我。感谢您的回复。

最佳答案

您根本没有使用过 sqlite3_step()。试试这个方法...

sqlite3 *database; 
 dbPath=[self.databasePath UTF8String];
    if(sqlite3_open(dbPath,&database)==SQLITE_OK)
    {
        const char *sqlstatement = "INSERT INTO upssTable (column1, column2) VALUES (?,?)"; 
        sqlite3_stmt *compiledstatement;

        if(sqlite3_prepare_v2(database,sqlstatement , -1, &compiledstatement, NULL)==SQLITE_OK)
        {
             NSString *  str1 =@"1";
    NSString * str2 =@"1";
      sqlite3_bind_int(compiledstatement, 1, [str1 integerValue]);
        sqlite3_bind_int(compiledstatement, 2, [str2 integerValue]);
         if(sqlite3_step(compiledstatement)==SQLITE_DONE)
            {
                NSLog(@"done");
            }
            else NSLog(@"ERROR");
            sqlite3_reset(compiledstatement);
    }
    else
    {
        NSAssert1(0, @"Error . '%s'", sqlite3_errmsg(cruddb));                        
    }
    sqlite3_close(database);
}

关于objective-c - Objective C 中的 Sqlite 数据库插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972091/

相关文章:

objective-c - 多个 Xcode 安装

iphone - appDelegate 和 ViewControllers

ios - Objective-C 错误导致数组无法桥接

iOS 如何从 tableview 单元格中获取按钮文本

objective-c - 如何在 iOS 5 的 Info.plist 中创建 UIBackgroundModes 键

objective-c - cocoa 以编程方式向窗口添加文本

ios - Swift:将照片添加到自定义相册

ios - 为什么我在 swift 中看不到响应值?

Swift 无法使用类型访问类中的静态变量(: Instance)

ios - 如何自动实现 Properties 和 ivars 的 NSCoding