SQLite sqlite3_prepare_v2没有错误,但也没有

问题描述 投票:0回答:4

嘿伙计们,我只是在玩SQLite,所以这对我来说很新。我有一个视图,可以保存和找到人员数据。 save-function和find-function工作得很好。现在,我有一个带有tableView的新视图。我想让联系人表中的所有人都填上清单。

到现在为止,我已经:

-(void)viewWillAppear:(BOOL)animated {
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"Opened DB");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT name FROM contacts"];

        const char *query_stmt = [querySQL UTF8String];
        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        //if (sqlite3_step(query_stmt) == SQLITE_DONE)
        {
            //NSLog(@"SQLite OK");
            NSLog(@"SQLite ok");
            //if (sqlite3_step(statement) == SQLITE_ROW)
            //{
                while(sqlite3_step(statement) == SQLITE_ROW) {
                    NSLog(@"SQLite ROW");
                    NSString *person = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                    [personsList addObject:person];
                }
            //} else {
            //    NSLog(@"Emtpy list");
            //}
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }
    NSLog(@"Calling reload");
    NSLog(@"%@", personsList);
    [tabelView reloadData];
}

这都在viewWillAppear中。加载并完成视图后,日志显示:2011-05-06 10:45:26.976 database[1412:207] Opened DB

2011-05-06 10:45:26.978 database[1412:207] could not prepare statement: not an error

2011-05-06 10:45:26.979 database[1412:207] Calling reload

2011-05-06 10:45:26.979 database[1412:207] (

)

所以看起来这个语句不是错误,但sqlite3_prepare_v2毕竟不是SQL_OK。有帮助吗?

编辑:在上一个视图中,创建人的声明是:@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", name.text, address.text, phone.text];

寻找某人的声明是:@"SELECT address, phone FROM contacts WHERE name=\"%@\"", name.text];

这两个语句起作用并显示数据。

iphone objective-c ios sqlite ios4
4个回答
3
投票

@joetjah我用你的代码来获取结果,它运行正常

2011-05-06 16:03:00.076 SQLiteDemo[6454:207] Opened DB
2011-05-06 16:03:09.550 SQLiteDemo[6454:207] could not prepare statement: not an error
2011-05-06 16:03:24.685 SQLiteDemo[6454:207] SQLite ok
2011-05-06 16:03:26.229 SQLiteDemo[6454:207] SQLite ROW
2011-05-06 16:03:28.254 SQLiteDemo[6454:207] SQLite ROW
2011-05-06 16:03:34.461 SQLiteDemo[6454:207] Calling reload
2011-05-06 16:03:35.009 SQLiteDemo[6454:207] (
    rahul,
    sqlite
)


-(void)testFunction{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;
    sqlite3 *contactDB;
    NSMutableArray *personsList = [[NSMutableArray alloc ] initWithCapacity:0];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"Opened DB");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT name FROM contacts"];

        const char *query_stmt = [querySQL UTF8String];
        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
            //if (sqlite3_step(query_stmt) == SQLITE_DONE)
        {
            //NSLog(@"SQLite OK");
            NSLog(@"SQLite ok");
            //if (sqlite3_step(statement) == SQLITE_ROW)
            //{
            while(sqlite3_step(statement) == SQLITE_ROW) {
                NSLog(@"SQLite ROW");
                NSString *person = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                [personsList addObject:person];
            }
            //} else {
            //    NSLog(@"Emtpy list");
            //}
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }
    NSLog(@"Calling reload");
    NSLog(@"%@", personsList);
    //[tabelView reloadData];
}

您可以比较代码,可能的失败点可能是1]数据未正确保存,因此无法获取任何内容。 2] databasePath被设置为空白(我怀疑你得到Open Db日志)

表联系人是使用“CREATE TABLE contacts(name text,age integer)”创建的,请尝试一次,让我知道它是否有效。


1
投票

试试这个,

-(void)viewWillAppear:(BOOL)animated {

    // Override point for customization after app launch.
    // Setup some globals
    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:@"contacts.db"];

    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"Opened DB");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT name FROM CONTACTS"];
        const char *query_stmt = [querySQL UTF8String];

        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            NSLog(@"SQLite ok");
                while(sqlite3_step(statement) == SQLITE_ROW) {
                    NSLog(@"SQLite ROW");
                    NSString *person = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                    [personsList addObject:person];
                }
            sqlite3_finalize(statement);
        }
        else
        {
            //NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(statement));
        }
        sqlite3_close(contactDB);
    }
    NSLog(@"Calling reload");
    NSLog(@"%@", personsList);
    [tabelView reloadData];
}

0
投票

我使用const char *dbpath = [databasePath UTF8String];,但我忘记在它上面添加以下代码:

// Get the documents directory
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
    const char *dbpath = [databasePath UTF8String];

0
投票

这对我有用:

在didload函数中创建表,然后将其添加(假设您已声明其他表视图函数):

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell        

    var selectStatement: OpaquePointer? = nil
    let selectSql = "select * from Inventory where ID = \((indexPath.row + 1))"
    if sqlite3_prepare_v2(Database.database.sqliteDB, selectSql, -1, &selectStatement, nil) == SQLITE_OK{
        if sqlite3_step(selectStatement) == SQLITE_ROW {

            let nameItem = sqlite3_column_text(selectStatement, 1)
            let price = sqlite3_column_double(selectStatement, 2)
            let quantity = sqlite3_column_int(selectStatement, 3)

            let nameString = String(cString: nameItem!)
            let priceString = String(format: "%.1f",price)
            let quantityString = String(quantity)

            cell.itemName.text = nameString
            cell.itemPrice.text = priceString
            cell.itemQuantity.text = quantityString

        }
    }

    return cell
}
© www.soinside.com 2019 - 2024. All rights reserved.