0

我不断收到错误,遗憾的是我看不出我做错了什么。tripname我想要一个带有插入, timestamp, lat, longi, accuracy, and的语句speed。已经建好数据库但是不能写成功的SQL语句,请帮忙

这是我的代码:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    NSString *databaseDir;
    NSArray *dirPath;
    //get the documents path
    dirPath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    databaseDir =[dirPath objectAtIndex:0];
    databasePath=[[NSString alloc]initWithString: [databaseDir stringByAppendingPathComponent:
                                                   @"deathTrail.db" ]];
    sqlite3_stmt *statement;
    const char *dbpath=[databasePath UTF8String];
    if (sqlite3_open(dbpath, &deathTrail)==SQLITE_OK){

        NSString *sql_stmt1=[[NSString alloc] initWithFormat:@"INSERT VALUES INTO LOCATION_POINTS (LATITUDE, LONGITUDE) VALUES (\"%d\", \"%d\")",  newLocation.coordinate.latitude, newLocation.coordinate.longitude];

        const char *insertstmt=[sql_stmt1 UTF8String];
        sqlite3_prepare_v2(deathTrail, insertstmt, -1, &statement,NULL);
        if(sqlite3_step(statement)==SQLITE_DONE) {
            status.text=@"it worked";
        } else {
            status.text=@"it failed";
        }
        sqlite3_finalize(statement);
        sqlite3_close(deathTrail);
    }
}
4

2 回答 2

0

你解决了吗?

如果没有,这里还有一件事要检查。如果您在应用程序的早期执行 SELECT 语句并忘记包含 sqlite3_finalize(statement); 然后插入语句将不起作用。注销 sql err msg 将显示“数据已锁定”。您仍然可以执行更多的 SELECT 语句,但是在任何地方都缺少一个 finalize 语句,并且 INSERTS 将被破坏。

于 2012-08-21T14:20:03.857 回答
0
  1. 您也应该检查sqlite3_prepare_v2命令的结果。

  2. 如果要么sqlite3_step失败,你应该检查结果,sqlite3_errmsg这样你就知道失败了(在这种情况下,因为上面的代码没有明显的问题,我认为这是因为表不存在)。

  3. 一般来说,如果您在尝试打开它时知道该数据库应该存在,您应该使用sqlite_open_v2(eg sqlite3_open(dbpath, &deathTrail, SQLITE_OPEN_READWRITE, NULL)) 这样它就不会为您创建一个空白数据库。或者,如果您希望它创建数据库,则继续使用,但如果该表尚不存在,请sqlite3_open确保您正在执行适当的 SQL语句。CREATE TABLE

  4. 如果您认为它错误地为您创建了一个空白数据库,请不要忘记重置您的模拟器或删除并重新安装程序以确保将删除任何空白数据库。

  5. 如果您打算使用捆绑包中的数据库进行读写,请确保以编程方式将数据库从捆绑包复制到文档文件夹。

于 2012-08-20T17:47:43.797 回答