gpt4 book ai didi

ios - 数据库类的使用及其在 Objective C iOS 中的方法

转载 作者:行者123 更新时间:2023-11-29 00:52:21 25 4
gpt4 key购买 nike

我正在尝试在 iOS 中使用数据库 SQLite。我在下面的代码中有几个问题。

#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新手,所以如果我问的有什么问题请纠正我。感谢您抽出时间(:

最佳答案

答案太长,所以请阅读本教程,您可以了解 SQLite 如何在 iOS 中工作

http://www.appcoda.com/sqlite-database-ios-app-tutorial/

关于ios - 数据库类的使用及其在 Objective C iOS 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37984378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com