iOS,SQL数据记录器

时间:2012-08-20 17:21:28

标签: sql ios

我一直在犯错误,遗憾的是我看不出我做错了什么。我想要一个带有插入tripnametimestamplatlongiaccuracyspeed的语句。我已经建立了数据库,但我无法编写成功的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);
    }
}

2 个答案:

答案 0 :(得分:0)

  1. 您也应该检查sqlite3_prepare_v2命令的结果。

  2. 如果它或sqlite3_step失败,您应该检查sqlite3_errmsg的结果,以便您知道失败的原因(在这种情况下,因为上面的代码没有明显的问题,我假设这是因为该表不存在。)

  3. 通常,如果您在尝试打开数据库时知道数据库应该存在,则应使用sqlite_open_v2(例如sqlite3_open(dbpath, &deathTrail, SQLITE_OPEN_READWRITE, NULL)),这样就不会为您创建空数据库。或者,如果您希望它创建数据库,那么请继续使用sqlite3_open,但如果该表尚不存在,请确保执行相应的SQL CREATE TABLE语句。

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

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

答案 1 :(得分:0)

你有没有解决这个问题?

如果没有,这是另一件要检查的事情。如果您之前在应用程序中执行了SELECT语句,则忘记包含一个     sqlite3_finalize(声明); 然后插入语句将无法正常工作。注销sql err msg将显示“数据已锁定”。您仍然可以执行更多SELECT语句,但只有一个缺少finalize语句,INSERTS将被破坏。