iOSでDatabase SQLiteを使用しようとしています。 次のコードにはいくつか質問があります。上記のコードでObjective Cのデータベースクラスとそのメソッドの使い方iOS
#import "DBManager.h"
static DBManager *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;
@implementation DBManager
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[ super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"calc.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"create table if not exists resultDetail (results integer)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
- (BOOL) saveData:(int)resultValue ;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into resultDetail (results) values (\"%d\")",resultValue];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_reset(statement);
return YES;
}
else
{
return NO;
}
// sqlite3_reset(statement);
}
sqlite3_reset(statement);
return NO;
}
- (int) findResult:(int)resultValue
{
int n=0;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"select results from resultDetail where results=\"%d\"",resultValue];
// NSString *querySQL = [NSString stringWithFormat:@"select * from resultDetail"];
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)
{
int n = sqlite3_column_int(statement, 0);
NSLog(@"%d let's see",n);
sqlite3_reset(statement);
return n;
}
else{
NSLog(@"Not found");
return 0;
} */
while(sqlite3_step(statement) == SQLITE_ROW){
n = sqlite3_column_int(statement, 0);
NSLog(@"%d is the value",n);
}
return n;
// sqlite3_reset(statement);
}
sqlite3_reset(statement);
}
sqlite3_reset(statement);
return 0;
}
-(BOOL)isEmpty{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"select * from resultDetail"];
// NSString *querySQL = [NSString stringWithFormat:@"select * from resultDetail"];
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){
sqlite3_reset(statement);
NSLog(@"Not Empty");
return NO;
}
}
sqlite3_reset(statement);
}
sqlite3_reset(statement);
return YES;
}
-(BOOL)isDuplicate:(int)resultValue{
BOOL n=NO;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"select results from resultDetail where results=\"%d\"",resultValue];
// NSString *querySQL = [NSString stringWithFormat:@"select * from resultDetail"];
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){
sqlite3_reset(statement);
n = YES;
}
sqlite3_reset(statement);
return n;
// sqlite3_reset(statement);
}
sqlite3_reset(statement);
}
sqlite3_reset(statement);
return NO;
}
@end
、この
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;
は、そのクラスやメソッドとは何かありますか?
なぜ私たちはgetSharedInstanceを作成していますか?その使用方法は何ですか?
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[ super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
私たちが実際にこれらに何をすべきか、この中にNULL引数は何ですか?
sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK
私は私は何も悪いことを依頼された場合ので、私を修正してくださいIOSに新しいです。おかげで時間(のために: