lantianyiming 2013-07-11 06:03 采纳率: 0%
浏览 1151

SQLite添加项目到数据库

动态添加项目到SQlite数据库失败:

-(void) addPatientToDatabase:(Patient *)newPatient {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];    
    sqlite3 *database;
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "insert into patients (firstName, surname, dob, homeNumber, mobileNumber, email, address, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            sqlite3_bind_text(compiledStatement, 1, [newPatient.patientName UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 2, [newPatient.patientSurname UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 3, [newPatient.patientDoB UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 4, [newPatient.patientHomeNumber UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 5, [newPatient.patientMobileNumber UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 6, [newPatient.patientEmail UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 7, [newPatient.patientAddress UTF8String], -1, SQLITE_TRANSIENT);
            NSData *dataForPicture = UIImagePNGRepresentation(newPatient.patientPicture);
            sqlite3_bind_blob(compiledStatement, 8, [dataForPicture bytes], [dataForPicture length], SQLITE_TRANSIENT);
        }
        if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
            sqlite3_finalize(compiledStatement);
        }
    }
    sqlite3_close(database);
}

如果在if语句中有断点就添加一些断点和记号:

if(sqlite3_step(compiledStatement) == SQLITE_DONE) {

但是没有挑选,谢谢。

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-05 14:19
    关注

    看起来你的代码是在尝试向一个 SQLite 数据库中的 "patients" 表插入一条新记录。


    在这个方法中,你首先打开了一个数据库连接,然后准备了一条 SQL INSERT 语句,并使用 sqlite3_bind_* 函数绑定了该语句的参数。


    最后,你使用了 sqlite3_step 函数来执行这条语句。如果该函数返回 SQLITE_DONE,则意味着语句成功执行。


    如果你的插入操作失败,那么可能是因为以下原因之一:

    • 数据库连接打开失败

    • SQL 语句准备失败

    • 参数绑定失败

    • 执行语句失败

    要确定哪个原因导致了失败,你可以检查相应的函数调用的返回值,并根据需要调试代码。


    此外,如果你希望在插入操作失败时获得更多信息,你还可以使用 sqlite3_errmsg 函数来获取错误消息。

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题