Pas en mesure de récupérer les données de base de données dans ios
-
27-10-2019 - |
Question
Je veux enregistrer des données dans la base de données SQLite et lu, il n'y a pas de problème lors de l'enregistrement des données, mais je ne suis pas en mesure de récupérer des données de base de données.
Voici le code que j'utilise.
//>>>>>> 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);
}
}
S'il vous plaît fournir la solution.
Merci à l'avance
La solution
Créer la base de données lors de l'exécution, et utilisez la logique ci-dessous,
pour écrire les données dans SQLite.
sqlite3 *pDb;
char *databaseName;
databaseName = @"TempDatabase.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
//databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
databasePath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];
//[self copyDatabaseIfNeeded];
if(sqlite3_open([databasePath UTF8String], &pDb) == SQLITE_OK)
{
const char *sqlStatement = "INSERT INTO tablename (name) VALUES(?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(pDb, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text( compiledStatement, 1, [strTxtFldValue UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(compiledStatement) != SQLITE_DONE )
{
NSLog( @"~~~~~~~~~~~ 1 Error: %s", sqlite3_errmsg(pDb) );
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Name name is not added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(pDb);
Pour lire les données de SQLite
sqlite3 *database;
const char *dbpath = [appDel.databasePath UTF8String];
sqlite3_stmt *statement;
NSMutableArray *names = [[NSMutableArray alloc]init];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *sqlStatement = "select name from tablename";
if (sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *cName = [NSString stringWithUTF8String:(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);
Essayez et me voter si cela fonctionne bien pour vous.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow