SQLite在目标c中插入数据

发布于 2024-12-11 16:24:12 字数 1536 浏览 0 评论 0原文

我想向 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];

I want to insert some data to mydatabase.sqlite but I have a problem. There is no error message when inserting data but then I open database and I cant see any data. There isnt any data.

this is addCoffee method in MyClass:

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

sending data:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-12-18 16:24:12
- (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);

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

}
冬天旳寂寞 2024-12-18 16:24:12

为发送到数据库的所有 sql 命令创建 NSLog,然后尝试从终​​端执行它们,您将看到所有错误并能够修复它们

Create NSLog for all sql commands that you send to database and then try to execute them from the terminal and you will see all errors and be able to fix them

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文