#define kFileName @"archive"
#define kDataKey @"Data"
#define kSqliteFileName @"data.db3"
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,retain)IBOutlet UITextField *name;
@property(nonatomic,retain)IBOutlet UITextField *gender;
@property(nonatomic,retain)IBOutlet UITextField *age;
@property(nonatomic,retain)IBOutlet UITextField *education;
-(NSString *)dataFilePath;
-(void)applicationWillResignActive:(NSNotification *)nofication;
@end
#import "ViewController.h"
#import "Person.h"
#import <sqlite3.h>
@implementation ViewController
@synthesize name,gender,age,education;
-(NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//return [documentsDirectory stringByAppendingPathComponent:kFileName];
return [documentsDirectory stringByAppendingPathComponent:kSqliteFileName];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
NSString *filePath = [self dataFilePath];
NSLog(@"filePath=%@",filePath);
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
//属性列表
/*
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
= [array objectAtIndex:0];
= [array objectAtIndex:1];
= [array objectAtIndex:2];
= [array objectAtIndex:3];
[array release];*/
//归档
/*
NSData *data = [[NSMutableData alloc]initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
Person *person = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];
= person.name;
= der;
= person.age;
= person.education;
[unarchiver release];
[data release];*/
//sqlite3
sqlite3 *database;
//打开数据库
if(sqlite3_open([filePath UTF8String], &database)!=SQLITE_OK){//备注1
//数据库打开失败,关闭数据库
sqlite3_close(database);
NSAssert(0,@"打开数据库失败");
}
char* errorMsg;
NSString *createSQL = @"CREATE TABLE IF NOT EXISTS PERSON (name TEXT PRIMARY KEY,gender TEXT,age TEXT,education TEXT);";
//创建表
if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg)!=SQLITE_OK){//备注2
//创建表失败,关闭数据库
sqlite3_close(database);
NSAssert1(0, @"创建表失败:%s", errorMsg);
}
//查询表
NSString *querySQL = @"SELECT name,gender,age,education FROM PERSON ORDER BY name";
//执行查询,遍历查询结果
sqlite3_stmt *statment;
if(sqlite3_prepare_v2(database, [querySQL UTF8String], -1, &statment, nil) == SQLITE_OK){//备注3
//查询成功,执行遍历操作
while(sqlite3_step(statment) == SQLITE_ROW){//备注4
const char* pName = (char*)sqlite3_column_text(statment, 0);//备注5
if(pName!=NULL){
= [[NSString alloc]initWithUTF8String:pName];
}
char* pGender = (char*)sqlite3_column_text(statment, 1);
if(pGender!=NULL){
= [[NSString alloc]initWithUTF8String:pGender];
}
char* pAge = (char*)sqlite3_column_text(statment, 2);
if(pAge!=NULL){
= [NSString alloc]nitWithUTF8String:pAge];
}
char* pEducation = (char*)sqlite3_column_text(statment, 3);
if(pEducation!=NULL){
= [[NSString alloc]initWithUTF8String:pEducation];
}
}
sqlite3_finalize(statment);//备注6
}
//关闭数据库
sqlite3_close(database);//备注7
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
[super viewDidLoad];
}
-(void)applicationWillResignActive:(NSNotification *)nofication{
//属性列表
/*
NSMutableArray *array = [[NSMutableArray alloc]init];
[array ];
[array ];
[array ];
[array ];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];*/
//归档
/*
Person *person = [[Person alloc]init];
person.name = ;
der = ;
person.age = ;
person.education = ;
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:person forKey:kDataKey];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[person release];
[archiver release];
[data release];*/
//sqlite3
sqlite3 *database;
//打开数据库
if(sqlite3_open([[self dataFilePath] UTF8String], &database)!=SQLITE_OK){
//数据库打开失败,关闭数据库
sqlite3_close(database);
NSAssert(0,@"打开数据库失败");
}
char* errorMsg;
NSString *updateSQL = @"INSERT OR REPLACE INTO PERSON(name,gender,age,education) VALUES(?,?,?,?);";
//执行插入或更新操作
sqlite3_stmt *statment;
if(sqlite3_prepare_v2(database, [updateSQL UTF8String], -1, &statment, nil) == SQLITE_OK){
//绑定变量
sqlite3_bind_text(statment, 1, [ UTF8String], -1, NULL);//备注8
sqlite3_bind_text(statment, 2, [ UTF8String], -1, NULL);
sqlite3_bind_text(statment, 3, [ UTF8String], -1, NULL);
sqlite3_bind_text(statment, 4, [ UTF8String], -1, NULL);
}
if(sqlite3_step(statment)!=SQLITE_DONE){
NSAssert1(0, @"更新表失败:%s", errorMsg);
}
sqlite3_finalize(statment);
//关闭数据库
sqlite3_close(database);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. Outlet = nil;
self.name = nil;
der = nil;
self.age = nil;
self.education = nil;
}
-(void)dealloc{
[name release];
[gender release];
[age release];
[education release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
本文发布于:2024-02-05 04:00:07,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170723572562880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |