【问题标题】:How to insert into sqlite when button is pressed?按下按钮时如何插入sqlite?
【发布时间】:2012-07-02 10:34:37
【问题描述】:

我正在尝试使用 Objective-C(最新的 xcode)将字符串插入到 sqlite 数据库中,但是由于某些奇怪的原因,我在 stackoverflow 或互联网上找到的每个示例似乎都不适用于我。

我没有使用任何 sdk,所以这很简单。

有没有人有一个实际工作的示例代码,可以将数据插入到数据库中,并且可以检索?

谢谢

【问题讨论】:

    标签: ios xcode sqlite insert


    【解决方案1】:
    sqlite3 *database;    
    
    if(sqlite3_open(___database file path__, &database) == SQLITE_OK)
    {
        NSString  * str = [[NSString alloc] initWithFormat:@"INSERT INTO %@ VALUES %@",___table name___,___value___];
        const char * sqlStatement = [str UTF8String];
    }
    else 
    {
        //ERROR
        return;
    }
    
    sqlite3_stmt *compiledStatement;
    
    
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {
            for (int i = 0;; i++)
            {
                char * ch;
                if ((ch = (char *)sqlite3_column_text(compiledStatement, i)))
                {
                    NSString * ret = [NSString stringWithCString:ch encoding:NSUTF8StringEncoding];
                }
                else 
                {
                    break;
                }
            }
        }
    }
    
    sqlite3_finalize(compiledStatement);
    sqlite3_close(database);
    

    这段代码像这样在表中插入值..

    【讨论】:

    • 非常干净易懂的代码。非常感谢您的努力。我会测试它并回来
    猜你喜欢
    • 2016-03-31
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    相关资源
    最近更新 更多