iphone - 如何在iPhone应用程序中使用sqlite prepare语句在Sqlite数据库中插入数据

标签 iphone sqlite

-(void)insertDataImage_ID:(NSInteger)ID ImageName:(NSString*)Image Bookmark:(NSString*)Title
{
    [self checkAndCreateDB];

    sqlite3 *database;

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

        NSString *statement;
        sqlite3_stmt *compliedstatement;
        statement =[[NSString alloc]  initWithFormat:@"insert into tblBookMark values(%d, '%@' , '%@')", ID,Image, Title ];

        statement = [[NSString alloc] initWithFormat :@"select * from tblBookMark"];
        NSLog(@"T-1");

        const char *sqlstatement = [statement UTF8String];

        if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL) == SQLITE_OK) {

            NSLog(@"T-2");
            if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {

                NSLog(@"T-3");

                NSAssert1 (0,@"Error by inserting '%s'",sqlite3_errmsg(database));

                UIAlertView *AlertOK=[[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error by inserting" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
                [AlertOK show];
                [AlertOK release];
            }
            NSLog(@"T-4");
            sqlite3_finalize(compliedstatement);
        }
        NSLog(@"T-5");
    }
    sqlite3_close(database);
    NSLog(@"T-6");

}


这段代码有什么问题。它没有显示任何错误,但它

如果(sqlite3_prepare_v2(数据库,sqlstatement,-1,&compliedstatement,NULL)== SQLITE_OK)

代码流没有进入这一行,任何人都可以帮助我确定错误或建议解决方案。

最佳答案



static sqlite3_stmt *insertStmt = nil;

if(insertStmt == nil) 
{
    insertSql = "INSERT INTO Loginchk (uname,password) VALUES(?,?)";
    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(insertStmt, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    NSLog("Inserted");
//Reset the add statement.
sqlite3_reset(insertStmt);
insertStmt = nil; 

关于iphone - 如何在iPhone应用程序中使用sqlite prepare语句在Sqlite数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912040/

相关文章:

c++ - 从 SQLite 中的准备语句获取原始 SQL 查询

iphone - XCode静态分析器: Analyzer skipped this file due to parse errors

ios - 我如何在使用我的应用程序时关闭其他应用程序的推送通知

iphone - UISearchBar 的颜色像联系人

perl DBIx sqlite {sqlite_unicode=>1}?

python - 为什么使用 sqlite 插入 SQLAlchemy 比直接使用 sqlite3 慢 25 倍?

ios - 存储图像文件的最佳 Swift 兼容数据库

iphone - 如何在 PDFPage 的屏幕上突出显示、添加注释和捕获/共享文本

iphone - CoreData prepareForDeletion 被调用无限次

iphone - XCode4 - 在哪里寻找由核心数据创建的sqlite文件