This is my code :
@interface DBHander : NSObject
{
sqlite3 *_database;
AppDelegate *_appDeledate;
NSString *_strDBName;
}
@end
-(NSString *)copyDB
{
NSArray *arrDicePath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDocPath=[arrDicePath objectAtIndex:0];
strDBPath=[strDocPath stringByAppendingPathComponent:@"data.sqlite"];
NSLog(@"%@",strDBPath);
NSString *strResorcePath=[[NSBundle mainBundle] pathForResource:@"data" ofType:@"sqlite"];
if(![[NSFileManager defaultManager] fileExistsAtPath:strDBPath])
{
[[NSFileManager defaultManager] copyItemAtPath:strResorcePath toPath:strDBPath error:nil];
}
return strResorcePath;
}
-(BOOL)InsetData:(NSString *)query
{
BOOL result=NO;
if(sqlite3_open([_strDBName UTF8String], &_database)==SQLITE_OK)
{
sqlite3_stmt *complstmp;
if(sqlite3_prepare_v2(_database, [query UTF8String], -1, &complstmp, nil)==SQLITE_OK)
{
sqlite3_step(complstmp);
result=YES;
}
sqlite3_finalize(complstmp);
}
sqlite3_close(_database);
return result;
}
Data insert successfully done, but can not retrieve my data from database!
-(NSMutableArray *)GetData:(NSString *)query
{
NSMutableArray *arrData=[[NSMutableArray alloc] init];
if(sqlite3_open([_strDBName UTF8String], &_database)==SQLITE_OK)
{
sqlite3_stmt *complstmp;
if(sqlite3_prepare_v2(_database, [query UTF8String], -1, &complstmp, nil)==SQLITE_OK)
{
while (sqlite3_step(complstmp)==SQLITE_OK)
{
NSMutableDictionary *dictData=[[NSMutableDictionary alloc] init];
for(int i=0;i<sqlite3_column_count(complstmp);i++)
{
NSString *str;
if(sqlite3_column_text(complstmp, i)!=NULL)
{
str=[NSString stringWithUTF8String:(char *)sqlite3_column_text(complstmp, i)];
}
else
{
str=@"";
}
[dictData setValue:str forKey:[NSString stringWithUTF8String:(char *)sqlite3_column_name(complstmp, i)]];
}
[arrData addObject:dictData];
}
}
sqlite3_finalize(complstmp);
}
sqlite3_close(_database);
return arrData;
}
Unable to get data from database:
Data insert successfully done, but can not retrieve my data from database!
Data insert successfully done, but can not retrieve my data from database!
Aucun commentaire:
Enregistrer un commentaire