Я довольно новичок в Xcode и у меня огромная проблема с учебником, над которым я работаю.
Я пытаюсь написать простой список покупок, используя базу данных sql.
На самом деле я закончил с проектом.Я трижды проверил каждую строку кода, но по какой-то причине он не хочет показывать мне содержимое БД или писать что-то внутри.
На самом деле я понятия не имею, что не так с кодом ...Запись в БД:
-(IBAction)addShoppingListItem:(id)sender {
//apriamo il database
if (([itemNameField.text length] == 0) || ([priceField.text length] == 0) || ([priceField.text doubleValue] == 0.0)) {
return;
}
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
NSLog(@"Impossibile aprire il Database!");
return;
}
//database aperto! Inseriamo valori nel database.
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *insertStatementsNS = [NSString stringWithFormat: @"insert into \"shoppinglist\" (item, price, groupid, dateadded) values (\"%@\", %@, %d, DATETIME('NOW'))", name_field, price_field, [groupPicker selectedRowInComponent:0]];
const char *insertStatement = [insertStatementsNS UTF8String];
dbrc = sqlite3_prepare_v2(db, insertStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);
//faccio pulizia rilasciando i database
sqlite3_finalize(dbps);
sqlite3_close(db);
// Pulisci i campi e indica successo nello status
statusLabel.text = [[NSString alloc] initWithFormat: @"Aggiunto %@", itemNameField.text];
statusLabel.hidden = NO;
itemNameField.text = @"";
priceField.text = @"";
}
Загрузка из базы данных:
-(void)loadDataFromDb {
//apriamo il database
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate *) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
NSLog(@"Impossibile aprire il Database!");
return;
}
//database aperto! Prendiamo i valori dal database.
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *queryStatementNS = @"select key, item, price, groupid, dateadded from shoppinglist order by dateadded";
const char *queryStatement = [queryStatementNS UTF8String];
dbrc = sqlite3_prepare_v2(db, queryStatement, -1, &dbps, NULL);
//Richiamo la funzione sqlite3_step() finché ho righe nel database
while ((dbrc = sqlite3_step(dbps)) == SQLITE_ROW) {
int primaryKeyValueI = sqlite3_column_int(dbps, 0);
NSNumber *primaryKeyValue = [[NSNumber alloc] initWithInt:primaryKeyValueI];
NSString *itemValue = [[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(dbps, 1)];
double priceValueD = sqlite3_column_double(dbps, 2);
NSNumber *priceValue = [[NSNumber alloc] initWithDouble:priceValueD];
int groupValueI = sqlite3_column_int(dbps, 3);
NSNumber *groupValue = [[NSNumber alloc] initWithInt:groupValueI];
NSString *dateValueS = [[NSString alloc] initWithUTF8String:(char*) sqlite3_column_text(dbps, 4)];
NSDate *dateValue = [dateFormatter dateFromString: dateValueS];
NSMutableDictionary *rowDict = [[NSMutableDictionary alloc] initWithCapacity:5];
[rowDict setObject:primaryKeyValue forKey: ID_KEY];
[rowDict setObject:itemValue forKey: ITEM_KEY];
[rowDict setObject:priceValue forKey: PRICE_KEY];
[rowDict setObject:groupValue forKey: GROUP_ID_KEY];
[rowDict setObject:dateValue forKey: DATE_ADDED_KEY];
[shoppingListItems addObject: rowDict];
//rilascio tutti gli elementi
[dateValue release];
[primaryKeyValue release];
[itemValue release];
[priceValue release];
[groupValue release];
[rowDict release];
}
}
Для смелых, желающих проверить весь проект (есть просто еще один класс) здесь есть ссылкасо всей моей работой.http://www.mediafire.com/?qxfde723r29nhtb
Заранее спасибо.