您好,我正在尝试构建一个 sqlite 数据库教程,但它不起作用,任何人都可以帮助我:
在下面找到我的代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "PersonInfo.h"
#import "PersonDatabase.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *personInfo=[[PersonDatabase database]getAllPersons];
for (PersonInfo*info in personInfo) {
NSLog(@"%d-%@",info.unique,info.nam);
}// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
#import <Foundation/Foundation.h>
@interface PersonInfo : NSObject{
int unique;
NSString * nam;
}
@property(nonatomic, assign)int unique;
@property(nonatomic, copy)NSString * nam;
-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name;
@end
#import "PersonInfo.h"
@implementation PersonInfo
@synthesize unique,nam;
-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name{
self=[super init];
if (self) {
self.unique=uniqueId;
self.nam=name;
NSLog(@"a");
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "sqlite3.h"
@interface PersonDatabase : NSObject{
sqlite3 * database;
}
+(PersonDatabase *)database;
-(NSArray *)getAllPersons;
@end
#import "PersonDatabase.h"
#import "PersonInfo.h"
@implementation PersonDatabase
static PersonDatabase * database;
+(PersonDatabase *)database{
if (database ==nil) {
database =[[PersonDatabase alloc]init];
}
return database;
}
-(id)init{
self=[super init];
if (self) {
NSString *sqliteDb=[[NSBundle mainBundle]pathForResource:@"admin" ofType:@"sqlite3"];
if(sqlite3_open([sqliteDb UTF8String],&database)!=SQLITE_OK){
NSLog(@"FAILED");
}else {
NSLog(@"passed");
}
}
return self;
}
-(NSArray *)getAllPersons{
NSMutableArray * returnArray=[[NSMutableArray alloc]init];
NSString * query= @"SELECT * FROM names";
NSLog(@"%@",query);
sqlite3_stmt * statement;
if(sqlite3_prepare_v2(database, [query UTF8String],-1, &statement,NULL)==SQLITE_OK){
while (sqlite3_step(statement)==SQLITE_OK) {
int uniqueId = sqlite3_column_int(statement, 0);
char *nameChars=(char *)sqlite3_column_text(statement,1);
NSString *name=[[NSString alloc]initWithUTF8String:nameChars];
PersonInfo *info=[[PersonInfo alloc]initWithUniqueId:uniqueId name:name];
[returnArray addObject:info];
}
sqlite3_finalize(statement);
}
else {
NSLog(@"a");
}
return returnArray;
}
@end
我发现问题是,它没有通过下面的函数,但我不知道为什么?..它也没有给出任何错误????
if(sqlite3_prepare_v2(database, [query UTF8String],-1, &statement,NULL)==SQLITE_OK)
提前致谢!!