Using NSString/parameters into SQLite statement iOS

前端 未结 2 862
旧时难觅i
旧时难觅i 2021-01-28 10:50

I have my code below. My values _emailVaue _passwordValue and _nameValue are taken from UITextFields in my app. I was under t

2条回答
  •  爱一瞬间的悲伤
    2021-01-28 11:46

    I hope it's OK to answer my own question if it seems to work. Hopefully someone will find it useful. Would appreciate any feedback on where I might be going wrong.

    sqlite3_stmt    *statement;
    const char *dbpath = [_databasePath UTF8String];
    const char *insertSQL;
    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        insertSQL = "INSERT INTO CONTACTS (email, password, name) VALUES (?, ?, ?)";
        if(sqlite3_prepare_v2(_contactDB, insertSQL, -1, &statement, NULL) == SQLITE_OK)
        {
        sqlite3_bind_text(statement, 1, [_emailValue UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 2, [_passwordValue UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [_nameValue UTF8String], -1, SQLITE_TRANSIENT);
        }
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
    //worked
        } else {
    
    
    //didn't work
        }
        sqlite3_finalize(statement);
        sqlite3_close(_contactDB);
    }
    

提交回复
热议问题