objective-c - SQLite在 objective-c 中插入数据

标签 objective-c ios xcode sqlite

我想向 mydatabase.sqlite 中插入一些数据,但我遇到了问题。插入数据时没有错误消息,但随后我打开数据库,但看不到任何数据。没有任何数据。

这是 MyClass 中的 addCoffee 方法:

if(addStmt == nil) {
    const char *sql = "insert into records(Date, Latitude, Longitude) 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, [Date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(addStmt, 2, [Latitude doubleValue] );
sqlite3_bind_double(addStmt, 3, [Longitude doubleValue] );

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
    recordID = sqlite3_last_insert_rowid(database);

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

发送数据:

iDailyAppDelegate *appDelegate = (iDailyAppDelegate *)[[UIApplication sharedApplication] delegate];

//Create a MyClass Object.
MyClass *coffeeObj = [[MyClass alloc] initWithPrimaryKey:0];
coffeeObj.Date = dateInString;
NSDecimalNumber *temp = [[NSDecimalNumber alloc] initWithString:first];
coffeeObj.Latitude = temp;
NSDecimalNumber *temp2 = [[NSDecimalNumber alloc] initWithString:second];
coffeeObj.Longitude = temp2;
[temp release];
coffeeObj.isDirty = NO;
coffeeObj.isDetailViewHydrated = YES;

//Add the object
[appDelegate addCoffee:coffeeObj];

最佳答案

- (void)inserting{

    database=nil;
    if (sqlite3_open([[DBAction getSqlitePath] UTF8String], &database) == SQLITE_OK){
        const char *sql = [[NSString stringWithFormat:@"insert  into SiteTable set Site_Address = '%@'",someString] UTF8String];
        sqlite3_stmt *updateStmt = nil;
        if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
        {
            //NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
        }
        if (SQLITE_DONE != sqlite3_step(updateStmt)){
            //NSAssert1(0, @"Error while creating database. '%s'", sqlite3_errmsg(database));
        }
        sqlite3_reset(updateStmt);
        sqlite3_finalize(updateStmt);
    }
    else{
        //NSAssert1(0, @"Error while opening database '%s'", sqlite3_errmsg(database));
    }
    sqlite3_close(database);

}

关于objective-c - SQLite在 objective-c 中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7871816/

相关文章:

iphone - For循环和数组问题

ios - “名称”不可用 : not available on iOS - XCode11

ios - didRegisterForRemoteNotificationsWithDeviceToken 在 ios 9 中没有调用

iphone - 比较/设置字符串的最快方法

ios - 从代码和 Storyboard 中实例化自定义按钮 - 如何制作 init 方法

ios - Xcode 6.4 iOS 图表 API 错误

ios - 没有这样的模块 Firebase

ios - 带有 webview 的 SearchBar 不工作

ios - 聚合 CMPedometerData(iPhone + Watch 总计数)

swift - 如何解决 Swift 5 中的 DispatchQueue 问题