M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

Sqlite3 step == SQLITE_ROW not working for me

As i am new to Xcode so no idea to fix this issue. Please help me out to figure the actual issue.

Database connection is build up, the query is execute, but I always get the message as : Result not found, after execution the query. :(

- (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 __ nor working

Upvotes: 1

Views: 370

Answers (2)

M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

Yes i resolved the issue…!!!! actually i just change the path of the directory to direct to my DB path. here is my final and working code… anyway thanks @HotLicks,,, @CL. :) :)

This works like a charm … :)


- (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);
}

}


while this way of accessing the DB is just confusing… :/

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;

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47729

Sometimes it helps to read the documentation:

If the nByte argument is less than zero, then zSql is read up to the first zero terminator. If nByte is non-negative, then it is the maximum number of bytes read from zSql.

Upvotes: 1

Related Questions