适用于 iOS 的 Sqlite 语句

发布于 2024-11-03 09:37:47 字数 893 浏览 0 评论 0原文

我正在尝试创建一个包含多个列的表。我正在尝试绑定值(这是一个变量)来创建表名。我不断收到错误:('创建更新语句时出错。'near "?": 语法错误'') 所以显然我在尝试绑定它时做错了。有人能为我解释一下吗?

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

}

I am trying to create a table with a number of columns. 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);

}

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

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

发布评论

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

评论(2

初懵 2024-11-10 09:37:47

您可以尝试更像这样的事情:

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 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));
溺渁∝ 2024-11-10 09:37:47

您不能将表名称绑定为参数。如果您打算这样做,那么您需要按照 Joe 的建议动态创建字符串。但要小心,因为这种方法可能会导致 SQL 注入攻击。例如,用户可以输入名称“;从 sqlite_master 中删除”。

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".

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