【问题标题】:Writing to SQLite Database objective C Iphone SDK写入 SQLite 数据库目标 C Iphone SDK
【发布时间】:2011-06-14 19:36:08
【问题描述】:

我需要使用 Objective C 和 iPhone SDK 将新条目写入 SQLite 数据库的帮助。

打开/关闭/读取数据库操作正常,但我不知道为什么这段代码不起作用。

-(void) testWriteToDatabase{

NSLog(@"instructions sent 3");
    sqlite3 *database;
    sqlite3_stmt *compiledStatement;
const char *insertSql="INSERT INTO data (name,pass,email) VALUES(?,?,?)";

NSString *userRegistered=userRegister.text;
NSLog(@"user %@",userRegistered);
NSString *passRegistered=passRegister.text;
NSLog(@"pass %@",passRegistered);
NSString *emailRegistered=emailRegister.text;
NSLog(@"email %@",emailRegistered);
sqlite3_stmt *insertStmt = nil;



if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK) {

    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) == SQLITE_OK){

NSLog(@"instructions sent 4");
sqlite3_bind_text(insertStmt, 1, [userRegistered UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [passRegistered UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 3, [emailRegistered UTF8String], -1, SQLITE_TRANSIENT);
            NSLog(@"instructions sent 5");
        while (sqlite3_step(compiledStatement) == SQLITE_ROW) {


        }

     }
sqlite3_reset(insertStmt);    
    }
sqlite3_close(database);    
}

老实说,我以前从未真正使用过 SQLite,但主要问题是它没有像我想要的那样向数据库“数据”添加条目。非常感谢您的帮助,干杯。

【问题讨论】:

  • 你有什么理由直接与 sqlite 对话而不用关心 Core Data 吗?
  • 我强烈推荐在 SQLite 周围使用 Gus Mueller 的 FMDatabase 包装类。 github.com/ccgus/fmdb
  • 我希望能够将数据库下载/上传到服务器,因此没有限制。老实说,我从来没有真正考虑过:/
  • 只有受虐狂直接在 Objective-C 中使用 SQLite C API。 Use FMDB(SQLite 包装器)或CoreData(对象图管理器)。
  • @Dave DeLong:真正的非受虐狂使用NSCodingNSKeyedArchiverNSKeyedUnarchiverNSArrayNSSet

标签: objective-c iphone-sdk-3.0 sqlite


【解决方案1】:

不应该

while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
}

while (sqlite3_step(insertStmt) == SQLITE_ROW) {
}

正如您在其他任何地方都使用过insertStmtcompiledStatement 未定义,因为我理解您的代码。

【讨论】:

    【解决方案2】:

    数据库是否有一个名为“数据”的表?琐碎的事情,但仍然。另外,请检查sqlite3_step 在每一步返回什么。

    【讨论】:

    • 是的,它确实有一个名为 data 的表,好的,我会检查一下。
    • 我正在努力寻找调用 Sqlite3_step 函数返回的代码... - 正如我所说,我是 SQLite 的完整新手:/
    • 我运行了这个命令:NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database)); 它返回的答案是 0。
    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多