RomanHouse
RomanHouse

Reputation: 2552

Writing to SQLite database. Something strange

I'm using SQLite database. The problem is that data isn't written to base. NSLog shows me "All ok" and insertSQL also is correct but still a record in the database does not happened. My code:

-(void)saveMessage:(id)sender 
{
    sqlite3 *database;

    databaseName = @"BlogDatabase.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

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

            NSString *insertSQL = [NSString stringWithFormat:@"insert into \"main\".\"MessagesData\" ( \"MessageText\", \"MessageDate\") values ( '%@', '%@');", _textField.text, [[NSDate alloc] init]];
            NSLog(@"%@", insertSQL);
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_stmt *compiledStatement;


            if(sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL) != SQLITE_OK)
                {
                    NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
                    }else{
                    if (sqlite3_step(compiledStatement) == SQLITE_DONE)
                        {
                            NSLog(@"All ok");
                            } else {
                            NSLog(@"FAIL");
                            }
                        }
            sqlite3_finalize(compiledStatement);
            }
    sqlite3_close(database);
    }

Upvotes: 2

Views: 163

Answers (2)

Praveen-K
Praveen-K

Reputation: 3401

In which database you are looking for? app db ?

Can you check out the number of record, just count the records.

SELECT count(*) FROM your_table_name

I doubt it whether you have any value in your _textField.text and date you are just initializing no setting it. and lets assume everything is fine, then

you should use BlogDatabase.MessagesData not main.MessagesData.

first try to run that insertQuery which you NSLog in your plain terminal. sqlite3 [app_db_path]/BlogDatabase.sqlite

Upvotes: 1

rakeshNS
rakeshNS

Reputation: 4257

You have to modify your code as below.

-(void)saveMessage:(id)sender 
{
sqlite3 *database;

databaseName = @"BlogDatabase.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];


  NSFileManager *fileManager = [NSFileManager defaultManager];

  BOOL success = [fileManager fileExistsAtPath:file];

  if(!success) {
      databasePath = [[NSBundle mainBundle] pathForResource:databaseName ofType:nil];
  }

Also put NSLog(@"Error. '%s'", sqlite3_errmsg(database)); after each database operation completed. (ie; after, sqlite3_step, sqlite3_prepare etc). This will print the exact error message while using database.

Upvotes: 1

Related Questions