-2
NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

databasePath = [[NSBundle mainBundle] pathForResource:@"etkYeni2" ofType:@"sqlite"];    

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


if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){

    NSLog(@"icerdeyim");

    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO etk (etkTip) VALUES ('%@')",yeniEkleLabel.text];

    const char *query_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog(@"OK INSERTED");

    } else {
        NSLog(@"NOT INSERTED !!");

    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
}

Here is my part of the code which I getting a string from the textbox and inserted it to the sqlite database. I created the database from the firefox extention sqlite manager and added it to the app. It is running as I expected in simulator and ipod touch how I wanted (inserts elements and I can see them when I used SELECT query ), but it is not inserts an element on iphone . My ipod touch is on iOS 5 and iphone is on iOS 6. Is it because of the version of iOS or iphone and ipod touch is pretends differently to the sqlite or what ? I read most of the answers of this problem and still don't get the solution. Could someone can help me in this situation ? Thanks ..

efdalustaoglu
  • 168
  • 11
  • http://stackoverflow.com/questions/9261601/sqlite-insert-works-in-simulator-but-not-on-device and http://stackoverflow.com/questions/717108/where-would-you-place-your-sqlite-database-file-in-an-iphone-app – Buntylm May 08 '13 at 10:14
  • Application Bundle is read-only, you need to copy the database to document directory and use it. – Midhun MP May 08 '13 at 10:23
  • @efdalustaoglu have you implemented my answer. – Dharmbir Singh May 08 '13 at 10:32
  • Which one answer have you implemented ? – Dharmbir Singh May 08 '13 at 11:11
  • yes I implemented and thanks everybody so much .I did not know before I should not know I had to copy to database into documents directory.. Like yours , @MidhunMP and Jitendra's answers helped me a lot to solve it – efdalustaoglu May 08 '13 at 11:13
  • Jitendra's..Because I already tried yours but not reach the conclusion on iphone device(it inserts on simulator but not on the device).For it his answer helped me to understand the copying the database into documents folder. – efdalustaoglu May 08 '13 at 11:19
  • k my dear.......i thought you found your solution by my answer and you accept other one answer. By the way...its k and your welcome... – Dharmbir Singh May 08 '13 at 11:26
  • I look through all the people's answers , therefore your code help me too ,thank you for your attention to my question and help. – efdalustaoglu May 08 '13 at 11:34

2 Answers2

2

Your database not Copied at phone directory u need to add that database. using this code.

-(void)createdatabase
{

    NSString *databaseName = @"Card.sqlite";        // Database Name



    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = documentPaths[0];

    NSLog(@"Dir :  %@ ",documentsDir);

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];


    BOOL success;

        NSFileManager *fileManager = [NSFileManager defaultManager] ;

        success = [fileManager fileExistsAtPath:databasePath];


    if(success)
        return;


    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];


    NSLog(@"%@",databasePathFromApp);


    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

add your sqlite file into mainbundle. thsi will really helpful.

Jitendra
  • 5,055
  • 2
  • 22
  • 42
0

Please try to use this one. If you wanna insert data in sqlite in that case you must create your Database and insert query using this method.I hope it will be helpful..

- (void)executeQuery:(NSString *)insertQuery
{
    sqlite3_stmt *statement;
    if (sqlite3_open([[self databaseFilePath] UTF8String],&sqliteDB) == SQLITE_OK)
    {
        if(sqlite3_prepare_v2(sqliteDB,[insertQuery UTF8String], -1, &statement, NULL) == SQLITE_OK)
        {
            if(sqlite3_step(statement) != SQLITE_DONE)
            {
                sqlite3_finalize(statement);
            }
        }
        else
            NSLog(@"query Statement Not Compiled");

        sqlite3_finalize(statement);
        sqlite3_close(sqliteDB);
    }
    else
        NSLog(@"Database Not Opened");
}



- (NSString *)databaseFilePath
{
    databaseName=@"abc.sqlite";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [paths objectAtIndex:0];
    return [path stringByAppendingPathComponent:databaseName];
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66