0
我想將一些數據保存到sqlite數據庫並讀取它,保存數據時沒有問題,但我無法從數據庫檢索數據。無法從ios中的數據庫中檢索數據
這是我正在使用的代碼。
//>>>>>> to save in sqlite Database
-(void)saveData
{
NSString *strTxtFldValue = txtFldName.text;
sqlite3_stmt *statement;
const char *dbpath = [appDel.databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO TESTTABLE (NAME) VALUES (\"%@\")",strTxtFldValue];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Data is added succesfully ");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"Name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
NSLog(@"Failed to add data");
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
//>>>>>> to read from database
//here the control comes till second NSLog and the "sqlite3_prepare_v2" statement doesn't get execute.
-(void)readData{
const char *dbpath = [appDel.databasePath UTF8String];
sqlite3_stmt *statement;
NSLog(@"^^^^^^^ 1");
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(@"^^^^^^^ 2");
// NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM TESTTABLE"];
NSString *querySQL = [NSString stringWithFormat:@"SELECT NAME FROM TESTTABLE"];
const char *query_stmt = [querySQL UTF8String];
**if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)**
{
NSLog(@"^^^^^^^ 3");
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(@"^^^^^^^ 4");
NSString *cName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSLog(@"~~~~### cName = %@",cName);
Names *name = [[Names alloc] initWithName:cName];
[names addObject:name];
[name release];
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
}
請提供解決方案。
預先感謝