开发者

Sqlite statements for iOS

I am trying to create a table with a number of co开发者_StackOverflow社区lumns. I am trying to bind the value (which is a variable) to create the table name. I keep getting an error: ('Error while creating update statement. 'near "?": syntax error'') So obviously I am doing something wrong with trying to bind it. Can anyone shed some light on this for me?

- (void)addTable{

     NSString *cat = sourceName;

     if(addtablestmt == nil) {

    const char *sqlStr = "CREATE Table ? ('itemID' 'integer','itemName' 'char(50)','itemCategory' 'char(50)','itemCount' 'integer','itemDone' 'char(50)','itemNote' 'char(50)','itemOrder' 'char(50)',PRIMARY KEY (itemID))";


    if(sqlite3_prepare_v2(database, sqlStr, -1, &addtablestmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));

    sqlite3_bind_text(addtablestmt, 1, [cat UTF8String], -1, SQLITE_TRANSIENT);

    }

    if(SQLITE_DONE != sqlite3_step(addtablestmt)){
        NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));

    }

    sqlite3_reset(addtablestmt);

}


You might try something more like:

NSString *sqlStr = [NSString stringWithFormat:@"CREATE Table %@ ('itemID' 'integer','itemName' 'char(50)','itemCategory' 'char(50)','itemCount' 'integer','itemDone' 'char(50)','itemNote' 'char(50)','itemOrder' 'char(50)',PRIMARY KEY (itemID))", sourceName];

if(sqlite3_prepare_v2(database, [sqlStr UTF8String], -1, &addtablestmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));


You cannot bind table name as a parameter. If you are planning on doing that, then you need to dynamically create the string as suggested by Joe. However be careful as that approach could lead to sql injection attacks. For ex a user could enter a name "; Delete from sqlite_master".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜