Hello i am developing a database dbmain with 2 tables my code
NSString *docsDir;
NSArray *dirPaths;
dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// docsDir=[dirPaths objectAtIndex:0];
docsDir = dirPaths[0];
databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"dbDare.db"]];
NSFileManager *filemgr=[NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath]==NO)
{
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS tblUser(ID INTEGER PRIMARY KEY AUTOINCREMENT, StrName TEXT,Strusername TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create table"
message:@"Failed to create table"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//status.text = @"Failed to create table";
}
char *errMsg1;
const char *sql_stmt1 =
"CREATE TABLE IF NOT EXISTS tblVanish (ID INTEGER PRIMARY KEY AUTOINCREMENT, strfilepath TEXT,datesaved TEXT)";
if (sqlite3_exec(contactDB, sql_stmt1, NULL, NULL, &errMsg1) != SQLITE_OK)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create table"
message:@"Failed to create table"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//status.text = @"Failed to create table";
}
sqlite3_close(contactDB);
} else {
// status.text = @"Failed to open/create database";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create open/create database"
message:@"Failed to create open/create database"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
after adding records to the table i can see the rows using lita software but when retriving the rows via this code
const char *dbpath=[databasePath UTF8String];
int iVal=0;
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = @"SELECT Strusername FROM tblUser";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
while (sqlite3_step(statement) == SQLITE_ROW) {
strname = [NSString stringWithFormat:@"Welcome %@" ,[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]];
}
} else {
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
i am not able to fetch any thing as there is no row displayed inside the table using ios but i can see the rows inside the table using lita please suggest where i am getting this wrong