Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

SQlite Inserting values

obaidjawadobaidjawad Posts: 133Registered Users
I have the application that needs the data to be inserted in the SQLite Database file, but when I'm trying to insert the data, it is failing the condition and not inserting the values, Can anyone help me out by pointing me where I'm going wrong.


-(id)init {
self=[super init];
//Database Name
DBName=@\"chathistory.sqlite\";

//Getting Database path
NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDir=[documentsPaths objectAtIndex:0];
DBPath =[documentsDir stringByAppendingPathComponent:DBName];
NSLog(@\"%@\",DBPath);

return self;
}

-(void)checkAndCreateDatabase{

BOOL Success;

//NSFileManager maintains File

NSFileManager *FileManager=[NSFileManager defaultManager];

//Checks Database Path

Success=[FileManager fileExistsAtPath:DBPath];

//if file exists it returns true

if (Success) {
return;
NSLog(@\"database failed\");
}

NSString *databasePathFromApp=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
NSLog(@\"database created\");
[FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];




}



-(void)insertDataUsername:(NSString *)user conv:(NSString *)sconv {

[self checkAndCreateDatabase];

//To setup Database object
sqlite3 *database;

[COLOR=\"Red\"] if (sqlite3_open([DBPath UTF8String], &database)== SQLITE_OK) {[/COLOR]
NSString *statement;
sqlite3_stmt *compliedstatement;
statement=[[NSString alloc]initWithFormat:@\"INSERT INTO usertable VALUES('%@', '%@')\",user,sconv];
NSLog(@\"%@,, %@\",user,sconv);
const char *sqlstatement =[statement UTF8String];
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {

//sqlite3_step(compiledstatement) executes the statement...

if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
NSAssert1(0, @\"Error by inserting. '%s'\", sqlite3_errmsg(database));
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@\"Error\" message:@\"Error by inserting\" delegate:self cancelButtonTitle:@\"Ok\" otherButtonTitles:@\"Done\", nil];
[alert show];
}
else
{
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@\"Success!\" message:@\"Data Successfully Entered\" delegate:self cancelButtonTitle:@\"Ok\" otherButtonTitles:@\"Done\", nil];
[alert1 show];
}
}
sqlite3_finalize(compliedstatement);
}
//Closing Database

sqlite3_close(database);

}




It is failing this condition in the code

if (sqlite3_open([DBPath UTF8String], &database)== SQLITE_OK) {
Post edited by obaidjawad on

Replies

  • obaidjawadobaidjawad Posts: 133Registered Users
    obaidjawad;432117 said:
    I have the application that needs the data to be inserted in the SQLite Database file, but when I'm trying to insert the data, it is failing the condition and not inserting the values, Can anyone help me out by pointing me where I'm going wrong.


    -(id)init {
    self=[super init];
    //Database Name
    DBName=@\"chathistory.sqlite\";

    //Getting Database path
    NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDir=[documentsPaths objectAtIndex:0];
    DBPath =[documentsDir stringByAppendingPathComponent:DBName];
    NSLog(@\"%@\",DBPath);

    return self;
    }

    -(void)checkAndCreateDatabase{

    BOOL Success;

    //NSFileManager maintains File

    NSFileManager *FileManager=[NSFileManager defaultManager];

    //Checks Database Path

    Success=[FileManager fileExistsAtPath:DBPath];

    //if file exists it returns true

    if (Success) {
    return;
    NSLog(@\"database failed\");
    }

    NSString *databasePathFromApp=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
    NSLog(@\"database created\");
    [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];




    }



    -(void)insertDataUsername:(NSString *)user conv:(NSString *)sconv {

    [self checkAndCreateDatabase];

    //To setup Database object
    sqlite3 *database;

    [COLOR=\"Red\"] if (sqlite3_open([DBPath UTF8String], &database)== SQLITE_OK) {[/COLOR]
    NSString *statement;
    sqlite3_stmt *compliedstatement;
    statement=[[NSString alloc]initWithFormat:@\"INSERT INTO usertable VALUES('%@', '%@')\",user,sconv];
    NSLog(@\"%@,, %@\",user,sconv);
    const char *sqlstatement =[statement UTF8String];
    if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {

    //sqlite3_step(compiledstatement) executes the statement...

    if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
    NSAssert1(0, @\"Error by inserting. '%s'\", sqlite3_errmsg(database));
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@\"Error\" message:@\"Error by inserting\" delegate:self cancelButtonTitle:@\"Ok\" otherButtonTitles:@\"Done\", nil];
    [alert show];
    }
    else
    {
    UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@\"Success!\" message:@\"Data Successfully Entered\" delegate:self cancelButtonTitle:@\"Ok\" otherButtonTitles:@\"Done\", nil];
    [alert1 show];
    }
    }
    sqlite3_finalize(compliedstatement);
    }
    //Closing Database

    sqlite3_close(database);

    }




    It is failing this condition in the code

    if (sqlite3_open([DBPath UTF8String], &database)== SQLITE_OK) {

    Is there anyone to help me out!
Sign In or Register to comment.