views:

75

answers:

1

I am creating an exercise app that will record the weight used and the number of "reps" the user did in 4 "Sets" per day over a period of 7 days so the user may view their progress.

I have built the database table named FIELDS with 2 columns ROW and FIELD_DATA and I can use the code below to load the data into the db. But the code has a sql statement that says,

INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%@'); 

When I change the statment to:

INSERT INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%@'); 

Nothing happens. That is no data is recorded in the db.

Below is the code:

#define kFilname @"StData.sqlite3"

- (NSString *)dataFilePath
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  return [documentsDirectory stringByAppendingPathComponent:kFilname];
}

-(IBAction)saveData:(id)sender;
{

  for (int i = 1; i <= 8; i++)
  {
    NSString *fieldName = [[NSString alloc]initWithFormat:@"field%d", i];
    UITextField *field = [self valueForKey:fieldName];
    [fieldName release];

    NSString *insert = [[NSString alloc] initWithFormat:
        @"INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)
          VALUES (%d, '%@');",i, field.text];
    // sqlite3_stmt *stmt;

    char *errorMsg;

    if (sqlite3_exec (database, [insert UTF8String],
        NULL, NULL, &errorMsg) != SQLITE_OK)
    {
      // NSAssert1(0, @"Error updating table: %s", errorMsg);
      sqlite3_free(errorMsg);
    }  
  }
  sqlite3_close(database);    
}

So how do I modify the code to do what I want? It seemed like a simple sql statement change at first but obviously there must be more. I am new to Objective-C and iPhone programming. I am not new to using sql statements as I have been creating web apps in ASP for a number of years.

Any help will be greatly appreciated, this is driving me nuts!

A: 

Suggestions:

  • write an insert statement with hardcoded values to see if the insert works
  • your filename has no path. does it assume the current directory when executed? what directory is it running from?
  • write a message to the screen if possible to see what the values you're getting are. Are they correct?
Jay
Thanks for the reply,I have been using the FireFox plugin SQLite Manager to see if the data is input. if I use the sql statement with REPLACE in it the data replaces the old data in the database perfectly. If I remove the OR REPLACE the data in the table is unchanged.The app is running from Documents. The database is in /Users/dave/Library/Application Support/iPhone Simulator/User/Applications/32A716E5-F.../Documents/StData.sqlite3 (The numbered folder changes). I have imported the db into Xcode and copied it to Documents, but it still goes to the above path. But it replaces but not insert.
I think you misunderstand what that option does. If another matching record exists it will not insert, unless you add the 'or replace' option. There's fairly good documentation on sqlite.org that explains it.
Jay