无法将数据插入到iphone上的sqlite数据库中

时间:2011-09-20 10:25:48

标签: iphone sqlite

我正在尝试创建(成功),插入(此处有问题)并选择(只是为了检查我是否正确)记录到数据库中。

我的整个代码如下:

-(NSInteger) InsertDataInDatabae {

    char *sqlStatement;
    sqlite3 *pDb;
    char *errorMsg;
    int returnCode;
    char *databaseName;
    databaseName="WaitListAll.db";
    returnCode = sqlite3_open(databaseName, &pDb);

    if(returnCode!=SQLITE_OK) {
        NSLog(@"Error in opening Database");
        return -1;
    }
    sqlStatement="drop table if exists waitlistallTable";
    returnCode = sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);

    if(returnCode!=SQLITE_OK) {
        NSLog(@"Table could not be droped");
        return -1;
    }

    sqlStatement="create table waitlistallTable(source varchar(10),waitlistid varchar(10),""restaurantid varchar(10),consumerid varchar(10), timeadded varchar(10))";

    returnCode=sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);
    if(returnCode!=SQLITE_OK) {
        NSLog(@"Table could not be Created Error:%@",errorMsg);//**getting error here as:unrecognized token ""**
        return -1;
    }

    NSInteger  i;
    for(i=0;i<[waitListAllRecordsArray count];i++) {
        NSDictionary *objDict=[waitListAllRecordsArray objectAtIndex:i];
        NSString *src,*wid,*rid,*cid,*tadded;
        src=[objDict objectForKey:@"SOURCE"];
        wid=[objDict objectForKey:@"WAITLISTID"];
        rid=[objDict objectForKey:@"RESTAURANTID"];
        cid=[objDict objectForKey:@"CONSUMERID"];
        tadded=[objDict objectForKey:@"TIMEADDED"];
        printf("\n %s %s %s  %s %s",src,wid,rid,cid);
        sqlStatement = sqlite3_mprintf("insert into waitlistallTable values(%s, %s, %s, %s, %s)",src,wid,rid,cid,tadded);

        returnCode=sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);

        if(returnCode!=SQLITE_OK) {
            printf("Error in inserting record :%s",errorMsg);
            return -1;
        }
    }

    sqlite3_stmt *statement;
    sqlStatement=sqlite3_mprintf("selet * from waitlistallTable");
    returnCode = sqlite3_prepare_v2(pDb, sqlStatement, strlen(sqlStatement), &statement,NULL);
    if(returnCode!=SQLITE_OK) {
        NSLog(@"Record could  not be Inserted  Error:%@",errorMsg);
        return -1;
    }

    returnCode=sqlite3_step(statement);
    while(returnCode==SQLITE_ROW) {
        char *src,*wid,*rid,*cid,*tadded;
        src=sqlite3_column_text(statement, 0);
        wid=sqlite3_column_text(statement, 1);
        rid=sqlite3_column_text(statement, 2);
        cid=sqlite3_column_text(statement, 3);
        tadded=sqlite3_column_text(statement, 4);
        NSLog(@"%@\t %@\t %@ \t%@ %@",src,wid,rid, cid,tadded);
    }

    sqlite3_finalize(statement);
    sqlite3_close(&pDb);

    //  sqlite3_free(sqlStatement);
    //NSLog(@"%@\t %@\t %@ \t%@ %@",src,wid,rid, cid,tadded);       //NSLog(@"*******object for dictionary is **************%@",[objDict objectForKey:@"SOURCE"]);
}

任何建议都会非常有帮助。 感谢

2 个答案:

答案 0 :(得分:0)

您正在尝试使用%s格式化程序插入NSString对象, 试试:

sqlStatement=sqlite3_mprintf("insert into waitlistallTable values(%s, %s, %s, %s, %s)",[src UTF8String],[wid UTF8String],[rid UTF8String],[cid UTF8String],[tadded UTF8String]);

希望它能帮助..!

答案 1 :(得分:0)

INSERT INTO历史记录(处方名,处方日,医生姓名,人名,个人资料)VALUES(?,?,?,?,?)

使用上述格式

插入值