3

I want to insert some data to mydatabase.sqlite but I have a problem. There is no error message when inserting data but then I open database and I cant see any data. There isnt any data.

this is addCoffee method in MyClass:

if(addStmt == nil) {
    const char *sql = "insert into records(Date, Latitude, Longitude) Values(?, ?, ?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [Date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(addStmt, 2, [Latitude doubleValue] );
sqlite3_bind_double(addStmt, 3, [Longitude doubleValue] );

if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    recordID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);

sending data:

iDailyAppDelegate *appDelegate = (iDailyAppDelegate *)[[UIApplication sharedApplication] delegate];

//Create a MyClass Object.
MyClass *coffeeObj = [[MyClass alloc] initWithPrimaryKey:0];
coffeeObj.Date = dateInString;
NSDecimalNumber *temp = [[NSDecimalNumber alloc] initWithString:first];
coffeeObj.Latitude = temp;
NSDecimalNumber *temp2 = [[NSDecimalNumber alloc] initWithString:second];
coffeeObj.Longitude = temp2;
[temp release];
coffeeObj.isDirty = NO;
coffeeObj.isDetailViewHydrated = YES;

//Add the object
[appDelegate addCoffee:coffeeObj];
5
  • Is it possible that you executed 'BEGIN TRANSACTION' somewhere and didn't execute a corresponding 'COMMIT'? Commented Oct 24, 2011 at 6:23
  • sorry what can I do, I didnt understand you??? Commented Oct 24, 2011 at 6:28
  • What types are Date, Latitude and Longitude? Commented Oct 24, 2011 at 8:57
  • date is string, latitude and longitude are double Commented Oct 24, 2011 at 9:54
  • thnx you everbody, I fixed my problem :) Commented Oct 24, 2011 at 11:06

2 Answers 2

1
- (void)inserting{

    database=nil;
    if (sqlite3_open([[DBAction getSqlitePath] UTF8String], &database) == SQLITE_OK){
        const char *sql = [[NSString stringWithFormat:@"insert  into SiteTable set Site_Address = '%@'",someString] UTF8String];
        sqlite3_stmt *updateStmt = nil;
        if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
        {
            //NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
        }
        if (SQLITE_DONE != sqlite3_step(updateStmt)){
            //NSAssert1(0, @"Error while creating database. '%s'", sqlite3_errmsg(database));
        }
        sqlite3_reset(updateStmt);
        sqlite3_finalize(updateStmt);
    }
    else{
        //NSAssert1(0, @"Error while opening database '%s'", sqlite3_errmsg(database));
    }
    sqlite3_close(database);

}
Sign up to request clarification or add additional context in comments.

3 Comments

why you are using update statement instead of insert? Also, I tried this way but it is not record, too :S
Ok, I modified my code. Also, I running my application on device. Because of location update. How can I find my database file? for simulator the way is ~/Library/Application Support/iPhone Simulator/{verison}/Applications/{app id}. But How can I see my database file when running on device? I cant see at this path?
I found it at organizer, thnx for all
1

Create NSLog for all sql commands that you send to database and then try to execute them from the terminal and you will see all errors and be able to fix them

1 Comment

there are nslog after every sql statement..it is not give me any error message!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.