iOS 8 - sqlite,未知错误。

时间:2015-04-24 08:59:46

标签: ios database sqlite

这是我第一次在我的应用中使用Sqlite。所以我按照this tutorial。我编辑了一些数据库以符合我的目的:

CREATE TABLE appointmentInfo(clientInfoID integer primary key, customername text, personname text, position text,appointmenttime text)

然后我用这段代码查询:

NSString *query = [NSString stringWithFormat:@"insert into appointmentInfo values(null, '%@', '%@', '%@', '%@')", self.customernameTF.text, self.personnameTF.text, self.positionTF.text,self.meetingtimeTF.text];
[self.dbManager executeQuery:query];

我一直收到错误:

DB Error: unknown error

但是当我检查sql文件时,会捕获记录。

什么似乎是问题?

3 个答案:

答案 0 :(得分:5)

我在教程下面的讨论中找到了解决方案:

  

我更改了变量" executeQueryResults"到一个int而不是一个BOOL。这似乎解决了这个问题。

我这样做,我收到了成功的消息。

答案 1 :(得分:0)

这是因为记录插入成功所以,DB显示未知错误。 我也遇到了同样的问题,但每次都有数据库更新成功。

-(BOOL)createDB{
    NSString *docsDir;
    NSArray *dirPaths;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];
    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString:
                    [docsDir stringByAppendingPathComponent: @"vcndatabase.db"]];
    BOOL isSuccess = YES;
    NSFileManager *filemgr = [NSFileManager defaultManager];
    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt ="CREATE TABLE IF NOT EXISTS ANSWERLIST (ID INTEGER PRIMARY KEY AUTOINCREMENT,ANSWER TEXT, INDEXPATH TEXT)";
            if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!= SQLITE_OK)
            {
                isSuccess = NO;
                NSLog(@"Failed to create table");
            }
            sqlite3_close(database);
            return  isSuccess;
        }
        else {
            isSuccess = NO;
            NSLog(@"Failed to open/create database");
        }
        sqlite3_reset(statement);
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }

    return isSuccess;
}

- (BOOL) saveData:(NSString*)answer index:(NSString*)index
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

//        NSString *insert_stmtstr = [NSString stringWithFormat:@"INSERT OR REPLACE INTO ANSWERLIST (ANSWER, INDEXPATH) VALUES ( \"%@\",(SELECT ANSWER FROM ANSWERLIST WHERE INDEXPATH = \"%@\");",answer, index];

        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO ANSWERLIST (ANSWER, INDEXPATH) VALUES (\"%@\",\"%@\")",answer,index ];
                                const char *insert_stmt = [insertSQL UTF8String];
                                sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
                                if (sqlite3_step(statement) == SQLITE_DONE)
                                {
                                    NSLog(@"%s", sqlite3_errmsg(database));
                                   // return YES;
                                }
                                else {
                                    NSLog(@"%s", sqlite3_errmsg(database));
                                  //  return NO;

                                }


    sqlite3_finalize(statement);
    sqlite3_close(database);
    }
    return NO;
}

- (NSString*) findByRegisterNumber:(NSString*)index
{
            const char *dbpath = [databasePath UTF8String];
            if (sqlite3_open(dbpath, &database) == SQLITE_OK)
            {
//                NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM ANSWERLIST "];
                NSString *querySQL = [NSString stringWithFormat:@"SELECT ANSWER FROM ANSWERLIST WHERE INDEXPATH=\"%@\"",index ];
                const char *query_stmt = [querySQL UTF8String];

                if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)
                {
                    if (sqlite3_step(statement) == SQLITE_ROW)
                    {
                        NSString *name = [[NSString alloc] initWithUTF8String:
                                          (const char *) sqlite3_column_text(statement, 0)];

//                         NSLog(@"%s", sqlite3_errmsg(database));
//                        NSString *department = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                      //  [resultArray addObject:department];
//                        NSString *year = [[NSString alloc]initWithUTF8String:
//                                          (const char *) sqlite3_column_text(statement, 2)];
                        sqlite3_reset(statement);
                        sqlite3_finalize(statement);
                        sqlite3_close(database);
                        return name;
                    }
                    else{
                        NSLog(@"%s", sqlite3_errmsg(database));
                        sqlite3_reset(statement);
                        sqlite3_finalize(statement);
                        sqlite3_close(database);
                        NSLog(@"Not found");

                    }
                }

                }

            return nil;
}
-(void)update:(NSString*)answer index:(NSString*)index{


    const char *dbpath = [databasePath UTF8String];
    if(sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
     //   NSString *insertSQL = [NSString stringWithFormat:@"UPDATE ANSWERLIST SET ANSWER = '%@' WHERE INDEXPATH ='%@'",answer,index];

        const char *sql = "update ANSWERLIST Set ANSWER = ? Where INDEXPATH=?";
        if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK){
            sqlite3_bind_text(statement, 2, [index UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(statement, 1, [answer UTF8String], -1, SQLITE_TRANSIENT);
        }
    }
    char* errmsg;
    sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

    if(SQLITE_DONE != sqlite3_step(statement)){
        NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
    }
    else{

    }
    sqlite3_finalize(statement);
    sqlite3_close(database);

}

答案 2 :(得分:0)

在我的情况下,当我插入记录时,查询未执行并且出现DB未知错误。 手机内存出现问题。我重新启动然后检查了内存。甚至我的应用程序都无法安装。

出于同样的原因也得到“安装失败的无效参数”,这也解决了这个问题:Installation Failed "Invalid argument" when trying to run Today application extension

重启手机或查看内存。

为我工作......;)