Sqlite3步== SQLITE_ROW不适合我

时间:2014-01-23 18:08:15

标签: objective-c sqlite

因为我是Xcode的新手,所以不知道解决这个问题。请帮我解决实际问题。

建立数据库连接,查询是执行,但我总是得到消息:执行查询后未找到结果。 :(

- (void)viewDidLoad
{
    NSString *MyDirectory;
    NSArray *DirectoryPaths;
    // Get the documents directory
    DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    MyDirectory = [DirectoryPaths objectAtIndex:0];
    // Build the path to the database file
    MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:@"mydb.db"]];
    //Status.text = MyDBPath;
    const char *Database = [MyDBPath UTF8String];
    if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
    {
        Status.text = @"Build Connection Successfully...!";
    }
    else
    {
        Status.text = @"Failed to open database...!";
    }
    [super viewDidLoad];
}


- (void) Find
{
    const char *Database = [MyDBPath UTF8String];
    sqlite3_stmt    *SQLStatement;
    NSString *Query;
    if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
    {
        Query = [NSString stringWithFormat:@"SELECT EmpName FROM EmpLogin WHERE          EmpID=\"%@\"",MyText.text];
        if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], 0, &SQLStatement, nil) == SQLITE_OK)
        {
            while (sqlite3_step(SQLStatement) == SQLITE_ROW)
            {
                NSString *Name = [[NSString alloc]
                                  initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
                TextResult.text = Name;
            }
            if (TextResult.text.length > 0)
            {
                Status.text = @"Result found";
            }
            else
            {
                NSLog(@"%s", sqlite3_errmsg(MyConnection));
                Status.text = @"Result not found";
                TextResult.text = @"";
            }
            sqlite3_finalize(SQLStatement);
        }
        else
        {
            //NSString *decode = [[NSString alloc]initWithCString:Query_Stmt encoding:NSUTF8StringEncoding];
            //Status.text = decode;
            Status.text = @"Query execution Failed...!";
        }
        sqlite3_close(MyConnection);
    }
}

Sqlite3_step(SQLStatement)== SQLITE_ROW __ 也无法正常工作

2 个答案:

答案 0 :(得分:2)

是的我解决了这个问题...... !!!! 实际上我只是将目录的路径更改为指向我的数据库路径。 这是我的最终和工作代码......无论如何,谢谢@HotLicks ,,, @CL。 :) :)

这就像魅力......:)


- (void) Find
{
MyDBPath = @"/Users/zaibi/Documents/IOSProjects/mydb.db";
MyDatabase = [MyDBPath UTF8String];
sqlite3_stmt    *SQLStatement;
NSString *Query;
//NSString *decode = [[NSString alloc]initWithCString:Database encoding:NSUTF8StringEncoding];
//NSLog(@"%@",decode);
if (sqlite3_open(MyDatabase, &MyConnection) == SQLITE_OK)
{
    Query = [NSString stringWithFormat:@"SELECT EmpName FROM EmpLogin WHERE EmpID=\"%@\"",MyText.text];
    if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], -1, &SQLStatement, nil) == SQLITE_OK)
    {
        if (sqlite3_step(SQLStatement) == SQLITE_ROW)
        {
            NSString *Name = [[NSString alloc]
                              initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
            TextResult.text = Name;
            Status.text = @"Result found";
        }
        else
        {
            NSLog(@"%s", sqlite3_errmsg(MyConnection));
            Status.text = @"Result not found";
            TextResult.text = @"";
        }
        sqlite3_finalize(SQLStatement);
    }
    else
    {
        NSLog(@"%s", sqlite3_errmsg(MyConnection));
        Status.text = @"Query execution Failed...!";
    }
    sqlite3_close(MyConnection);
}

}


虽然这种访问数据库的方式只是令人困惑......:/

NSString *MyDirectory;
NSArray *DirectoryPaths;
// Get the documents directory
DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
MyDirectory = [DirectoryPaths objectAtIndex:0];
// Build the path to the database file
MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:@"mydb.db"]];
//Status.text = MyDBPath;

答案 1 :(得分:1)

有时阅读the documentation会很有帮助:

  

如果nByte参数小于零,则读取zSql   第一个零终止符。如果nByte是非负的,那么它就是   从zSql读取的最大字节数。