I've got a big problem.
I'm started to wor on a tour log for myself.
Now i have a problem with my data read and save.
I have several textboxes with different content.
I used the Sqlite books example from Apple iPhone Dev.
my fields in the view are
ship (int), crewlistid (int), name(text), date(date), description(text), wetter(text), wasser(text), gpsset(int).
the values for these fields are pre initialized in the database.
if i open the database and read the data i get the same values for description, wetter and wasser. all other values are read and written correctly, except these three. I dont know why.
here the code for read and write.
Code:
// Brings the rest of the object data into memory. If already in memory, no action is taken (harmless no-op).
- (void)hydrate {
NSLog(@"Tour hydrate");
// Check if action is necessary.
if (hydrated) return;
// Compile the hydration statement, if needed.
if (hydrate_statement == nil) {
const char *sql = "SELECT ship, crewlistid, date, description, wetter, wasser, gpsset FROM tour WHERE pk=?";
if (sqlite3_prepare_v2(database, sql, -1, &hydrate_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
// Bind the primary key variable.
sqlite3_bind_int(hydrate_statement, 1, primaryKey);
// Execute the query.
int success =sqlite3_step(hydrate_statement);
if (success == SQLITE_ROW) {
self.ship = sqlite3_column_int(hydrate_statement, 0);
NSLog(@"%i",self.ship);
self.crewlistid = sqlite3_column_int(hydrate_statement, 1);
NSLog(@"%i",self.crewlistid);
self.date = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(hydrate_statement, 2)];
NSLog(@"%@",self.date);
char *str = (char *)sqlite3_column_blob(hydrate_statement, 3);
self.description = (str) ? [NSString stringWithUTF8String:str] : @"";
NSLog(@"%@",self.description);
char *str2 = (char *)sqlite3_column_blob(hydrate_statement, 4);
self.wetter = (str2) ? [NSString stringWithUTF8String:str] : @"";
NSLog(@"%@",self.wetter);
char *str3 = (char *)sqlite3_column_blob(hydrate_statement, 5);
self.wasser = (str3) ? [NSString stringWithUTF8String:str] : @"";
NSLog(@"%@",self.wasser);
self.gpsset = sqlite3_column_int(hydrate_statement, 6);
NSLog(@"%i",self.gpsset);
NSLog(@"success hydrate %S", hydrate_statement);
} else {
// The query did not return
self.ship = 1;
self.date = [NSDate date];
self.description = @"No Description available";
self.crewlistid = 1;
self.gpsset =1;
self.wasser = @"Keine Wasserdaten vorhanden";
self.wetter = @"Keine Wetterdaten vorhanden";
NSLog(@"No success hydrate");
}
// Reset the query for the next use.
sqlite3_reset(hydrate_statement);
sqlite3_finalize(hydrate_statement);
hydrate_statement = nil;
// Update object state with respect to hydration.
hydrated = YES;
NSLog(@"end hydrated");
}
// Flushes all but the primary key and title out to the database.
- (void)dehydrate {
NSLog(@"dehydrate tour");
if (dirty) {
NSLog(@"dehydrate notwendig");
// Write any changes to the database.
// First, if needed, compile the dehydrate query.
if (dehydrate_statement == nil) {
const char *sql = "UPDATE tour SET name=?, ship=?, crewlistid=?, date=?, description=?, wetter=?, wasser=?, gpsset=? WHERE pk=?";
if (sqlite3_prepare_v2(database, sql, -1, &dehydrate_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
NSLog(@"dehydrate error 1");
}
}
// Bind the query variables.
sqlite3_bind_text(dehydrate_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(dehydrate_statement, 2, ship);
sqlite3_bind_int(dehydrate_statement, 3, crewlistid);
sqlite3_bind_double(dehydrate_statement, 4, [date timeIntervalSince1970]);
sqlite3_bind_text(dehydrate_statement, 5, [description UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(dehydrate_statement, 6, [wetter UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(dehydrate_statement, 7, [wasser UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(dehydrate_statement, 8, gpsset);
sqlite3_bind_int(dehydrate_statement, 9, primaryKey);
NSLog(@"Dehydrate statement vor exec %S", dehydrate_statement);
// Execute the query.
int success = sqlite3_step(dehydrate_statement);
NSLog(@"Dehydrate statement int %i", dehydrate_statement);
NSLog(@"Dehydrate statement ins %s", dehydrate_statement);
NSLog(@"Dehydrate statement inu %S", dehydrate_statement);
NSLog(@"%i" , success);
int second = sqlite3_step(dehydrate_statement);
NSLog(@"%i" , second);
// Reset the query for the next use.
sqlite3_reset(dehydrate_statement);
// Handle errors.
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error: failed to dehydrate with message '%s'.", sqlite3_errmsg(database));
NSLog(@"dehydrate error2");
}
NSLog(@"error from DB is by dehydrate, %s", sqlite3_errmsg(database));
NSLog(@"Dehydrate statement int %i", dehydrate_statement);
NSLog(@"Dehydrate statement ins %s", dehydrate_statement);
NSLog(@"Dehydrate statement inu %u", dehydrate_statement);
//NSLog(@"Dehydrate statement int %i", dehydrate_statement);
//NSLog(@"Dehydrate statement int %i", dehydrate_statement);
sqlite3_reset(dehydrate_statement);
sqlite3_finalize(dehydrate_statement);
dehydrate_statement = nil;
// Update the object state with respect to unwritten changes.
dirty = NO;
NSLog(@"dehydrate feddich");
}
// Release member variables to reclaim memory. Set to nil to avoid over-releasing them
// if dehydrate is called multiple times.
[name release];
name = nil;
[date release];
date = nil;
[description release];
description = nil;
[wetter release];
wetter = nil;
[wasser release];
wasser = nil;
[data release];
data = nil;
// Update the object state with respect to hydration.
hydrated = NO;
}
ah and there is again a problem i had to set the hydrate and the dehydrate statement to nil that im able to finalize the statement. why?
maybe im blind. please help me.
ive every three times checked str not str , str2, str3.
like ive said. im blind.
Sorry for that
but there is one thing , what wonders me.
Why i had to set my statement to nil that i can finalize my statement to close the DB ??? i have an another object with the same code in it but no text fileds. and there i dont need to set the statements to nil. ???????