#import "DBManager.h"
static DBManager *sharedInstance = nil; //Class Object
static sqlite3 *database = nil; // Sqlite3 database object
static sqlite3_stmt *statement = nil; // sql query object
@implementation DBManager
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init]; //memory allocation
[sharedInstance createDB];
}
return sharedInstance;
}
这就是我创建数据库的方式
-(BOOL)createDB{
NSLog(@"createDB");
NSFileManager *nsFileManager = [NSFileManager defaultManager];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"document directory: %@",array);
NSString *documentPath = [array objectAtIndex:0];
NSLog(@"documentPath: %@",documentPath);
databasePath = [documentPath stringByAppendingString:@"/Database"];
if (![nsFileManager fileExistsAtPath:databasePath]) {
[nsFileManager createDirectoryAtPath:databasePath withIntermediateDirectories:NO attributes:nil error:nil];
}
NSLog(@"database path : %@",databasePath);
//NSLog(@"imageData : %@",imageData);
databasePath = [databasePath stringByAppendingString:@"/database.db"];
NSLog(@"databaseFilePath : %@",databasePath);
BOOL isSuccess = YES;
if (![nsFileManager fileExistsAtPath:databasePath]) {
const char *dbpath = [databasePath UTF8String];
NSLog(@"dbpath : %s",dbpath);
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
NSLog(@"database creation successfull");
char *errMsg;
const char *sql_stmt = "create table if not exists buddyMessage (message text)";
NSLog(@"sql is %s",sql_stmt);
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table : %s",errMsg);
}
else{
NSLog(@"table successful creation");
}
sqlite3_close(database);
return isSuccess;
}
}
return isSuccess;
}
这就是我将数据插入数据库的方式
-(void)insertText:(NSString *)messageText{
NSLog(@"insertBuddyMessages : %@",databasePath);
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK){
NSLog(@"database open");
NSString *insertSQL = [NSString stringWithFormat:@"insert into buddyMessage (message) values (\"%@\")",messageText];
const char *insert_stmt = [insertSQL UTF8String];
NSLog(@"insert_stmt : %s",insert_stmt);
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"inserted ");
}
else {
NSLog(@"error");
}
}
}
这里我使用的是一个Mutable数组resultarray,其中我添加了名称但是当我使用以下方法获取数据时,我每次都从数据库中获取第一个元素。当我每次NSLog resultarray显示数据库的第一个值时。
- (NSArray*) findmessage:(NSString*)message;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"select message from buddyMessage"];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:name];
NSLog(@"array is %@",resultArray);
NSLog(@"array count %lu",(unsigned long)[resultArray count]);
NSString *last = [resultArray lastObject];
NSLog(@"last is %@ ",last);
// [[NSUserDefaults standardUserDefaults] setObject:last forKey:@"preferenceName"];
// [[NSUserDefaults standardUserDefaults] synchronize];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:last forKey:@"keyToLookupString"];
NSLog(@"prefs is %@", prefs);
}
else{
NSLog(@"Not found");
// return nil;
}
sqlite3_reset(statement);
}
}
return nil;
}
@end
答案 0 :(得分:1)
您必须在循环中调用sqlite3_step
并在每次迭代时填充数组。
while (sqlite3_step(statement) == SQLITE_ROW) {
// store value here
}