無標題文章

Mode================SqlData.h.

首先 添加頭文件#import <sqlite3.h>

{

sqlite3 *db;

}

然后

//單例方法

+(instancetype)initData;

//初始化數(shù)據(jù)庫

-(void)initSql;

//初始化數(shù)據(jù)庫表格

-(void)initTable;

//添加數(shù)據(jù)

-(void)AddData:(ClassMessage *)data;

//修改數(shù)據(jù)

-(void)upData:(ClassMessage *)data;

//刪除數(shù)據(jù)

-(void)deletaData:(NSInteger)theId;

//查詢數(shù)據(jù)

-(NSMutableArray *)showData;

//關閉數(shù)據(jù)

-(void)closeSql;

Mode==============SqlData.m

/創(chuàng)建靜態(tài)變量

static SqlData *sqlData;

@implementation SqlData

//單例方法

+(instancetype)initData

{

if (!sqlData) {

sqlData = [[SqlData alloc]init];

}

return sqlData;

}

//初始化數(shù)據(jù)庫

-(void)initSql

{

//Documents 目錄 (路徑)

NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

//拼接

NSString *strName = [str stringByAppendingString:@"/1511E.db"];

//打開數(shù)據(jù)庫

if (sqlite3_open([strName UTF8String], &db) == SQLITE_OK) {

NSLog(@"數(shù)據(jù)庫打開成功");

[self initTable];

}else

{

NSLog(@"數(shù)據(jù)庫打開失敗");

}

}

//初始化數(shù)據(jù)庫表格

-(void)initTable

{

//sql 語句

//初始化數(shù)據(jù)庫表格的格式:create table if not exists? 表名(主鍵id integer primary key,所有的數(shù)據(jù)類型);

// const char *sql = "create table if not exists ClassMessage(classid integer primary key,name text,age text,sex text,heigt text,weitht text)";

//*name,*age,*sex,*heigt,*weitht;

const char *sql = "create table if not exists ClassMessage(classid integer primary key,name text,age text,sex text,heigt text,weitht text)";

//預編譯數(shù)據(jù)庫的指針

sqlite3_stmt *stmt;

//綁定數(shù)據(jù)庫指針的一個接口? -1 自動匹配長度

sqlite3_prepare_v2(db, sql, -1, &stmt, nil);

//執(zhí)行預編譯接口

//一行一行的去判斷是否執(zhí)行完成

if (sqlite3_step(stmt) == SQLITE_DONE) {

NSLog(@"數(shù)據(jù)庫表格創(chuàng)建成功");

}else

{

NSLog(@"數(shù)據(jù)框表格創(chuàng)建失敗");

}

//銷毀接口

sqlite3_finalize(stmt);

}

//添加數(shù)據(jù)

-(void)AddData:(ClassMessage *)data

{

//添加數(shù)據(jù)的sql語句: insert into 表明 values(null,?,?,?,?,?);

//? ? const char *sql = "insert into ClassMessage values(null,?,?,?,?,?)";

const char *sql = "insert into ClassMessage values(null,?,?,?,?,?)";

//預編譯數(shù)據(jù)庫的指針

sqlite3_stmt *stmt;

//綁定數(shù)據(jù)庫指針的一個接口? -1 自動匹配長度

sqlite3_prepare_v2(db, sql, -1, &stmt, nil);

//添加數(shù)據(jù)庫的接口

//綁定數(shù)據(jù)庫接口

sqlite3_bind_text(stmt, 1, [data.name UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 2, [data.age UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 3, [data.sex UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 4, [data.heigt UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 5, [data.weitht UTF8String], -1 , SQLITE_TRANSIENT);

//執(zhí)行預編譯接口 sqlite3_step(stmt);

sqlite3_step(stmt);

//銷毀接口

sqlite3_finalize(stmt);

}

//修改數(shù)據(jù)

-(void)upData:(ClassMessage *)data

{

//sql 語句的格式:update 表名 set 所有數(shù)據(jù) where 主鍵 = ?

const char *sql = "update ClassMessage set name = ?,age = ?,sex = ?,heigt = ?,weitht = ? where classid = ?";

//預編譯指針 (鏈接到數(shù)據(jù)庫)

sqlite3_stmt *stmt;

//綁定數(shù)據(jù)庫指針的接口

sqlite3_prepare_v2(db, sql, -1, &stmt, nil);

//添加數(shù)據(jù)庫的接口

//綁定數(shù)據(jù)庫接口

sqlite3_bind_text(stmt, 1, [data.name UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 2, [data.age UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 3, [data.sex UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 4, [data.heigt UTF8String], -1 , SQLITE_TRANSIENT);

sqlite3_bind_text(stmt, 5, [data.weitht UTF8String], -1 , SQLITE_TRANSIENT);

//綁定主鍵 id

sqlite3_bind_int(stmt, 6,(int)(data.classid));

//執(zhí)行預編譯接口 sqlite3_step(stmt);

sqlite3_step(stmt);

//銷毀接口

sqlite3_finalize(stmt);

}

//刪除數(shù)據(jù)

-(void)deletaData:(NSInteger)theId

{

//sql 語句: delete from 表名 where 表明的主鍵 id = ?

const char *sql = "delete from ClassMessage where classid = ?";

//預編譯指針 (鏈接到數(shù)據(jù)庫)

sqlite3_stmt *stmt;

//綁定數(shù)據(jù)庫指針的接口

sqlite3_prepare_v2(db, sql, -1, &stmt, nil);

//刪除綁定主鍵 id

sqlite3_bind_int(stmt, 1, (int)theId);

//執(zhí)行預編譯接口 sqlite3_step(stmt);

sqlite3_step(stmt);

//銷毀接口

sqlite3_finalize(stmt);

}

//查詢數(shù)據(jù)

-(NSMutableArray *)showData

{

//sql 語句格式 :select *from 表名

// const char *sql = "select *from ClassMessage";

const char *sql = "select *from ClassMessage";

//預編譯指針 (鏈接到數(shù)據(jù)庫)

sqlite3_stmt *stmt;

//綁定數(shù)據(jù)庫指針的接口

sqlite3_prepare_v2(db, sql, -1, &stmt, nil);

NSMutableArray *arr = [NSMutableArray array];

//執(zhí)行數(shù)據(jù)庫中的預編譯接口

//SQLITE_ROW一行一行的去查詢數(shù)據(jù)庫中的數(shù)據(jù)

while (sqlite3_step(stmt) == SQLITE_ROW) {

ClassMessage *classData = [[ClassMessage alloc]init];

//找到表格中的主鍵

//sqlite3_column_xxx 標識返回當前的行(指的是列的數(shù)據(jù))

classData.classid = sqlite3_column_int(stmt, 0);

classData.name = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];

classData.age = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)];

classData.sex = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 3)];

classData.heigt = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 4)];

classData.weitht = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 5)];

[arr addObject:classData];

}

//銷毀接口

sqlite3_finalize(stmt);

return arr;

}

//關閉數(shù)據(jù)

-(void)closeSql

{

sqlite3_close(db);

}


ClassMessage====================ClassMessage.h

//唯一標識 數(shù)據(jù)庫必須得有一個主鍵

@property(nonatomic,assign)NSInteger classid;

@property(nonatomic,strong) NSString *name,*age,*sex,*heigt,*weitht;

View======================.h

@property(nonatomic,strong) UITextField *nameTf,*ageTf,*sexTf,*heightTf,*weightTf;

View======================.m

#import "ClassView.h"

@implementation ClassView

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame]) {

[self addSubview:self.nameTf];

[self addSubview:self.ageTf];

[self addSubview:self.sexTf];

[self addSubview:self.heightTf];

[self addSubview:self.weightTf];

}

return self;

}

//懶加載

-(UITextField *)nameTf

{

if (!_nameTf) {

_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 80, self.frame.size.width, 50)];

_nameTf.borderStyle = UITextBorderStyleRoundedRect;

_nameTf.placeholder = @"姓名";

_nameTf.textColor = [UIColor lightGrayColor];

_nameTf.textAlignment = NSTextAlignmentCenter;

}

return _nameTf;

}

-(UITextField *)ageTf

{

if (!_ageTf) {

_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 140, self.frame.size.width, 50)];

_ageTf.borderStyle = UITextBorderStyleRoundedRect;

_ageTf.placeholder = @"年齡";

_ageTf.textColor = [UIColor lightGrayColor];

_ageTf.textAlignment = NSTextAlignmentCenter;

}

return _ageTf;

}

-(UITextField *)sexTf

{

if (!_sexTf) {

_sexTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 200, self.frame.size.width, 50)];

_sexTf.borderStyle = UITextBorderStyleRoundedRect;

_sexTf.placeholder = @"性別";

_sexTf.textColor = [UIColor lightGrayColor];

_sexTf.textAlignment = NSTextAlignmentCenter;

}

return _sexTf;

}

-(UITextField *)heightTf

{

if (!_heightTf) {

_heightTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 260, self.frame.size.width, 50)];

_heightTf.borderStyle = UITextBorderStyleRoundedRect;

_heightTf.placeholder = @"身高";

_heightTf.textColor = [UIColor lightGrayColor];

_heightTf.textAlignment = NSTextAlignmentCenter;

}

return _heightTf;

}

-(UITextField *)weightTf

{

if (!_weightTf) {

_weightTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 320, self.frame.size.width, 50)];

_weightTf.borderStyle = UITextBorderStyleRoundedRect;

_weightTf.placeholder = @"體重";

_weightTf.textColor = [UIColor lightGrayColor];

_weightTf.textAlignment = NSTextAlignmentCenter;

}

return _weightTf;

}

Contriller=================ViewController.m

#import "ViewController.h"

#import "SqlData.h"

#import "ClassMessage.h"

#import "sectionViewController.h"

@interface ViewController ()

{

NSMutableArray *array;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//? ? //1.先調(diào)用類方法 2.通過類方法 3.調(diào)用實例方法

//? ? [[SqlData initData]initSql];

//標題

self.title = @"數(shù)據(jù)庫";

//初始化

array = [NSMutableArray array];

self.tableView.rowHeight = 150;

//導航右按鈕

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];

}

//按鈕點擊事件跳轉(zhuǎn)

-(void)click

{

sectionViewController *section = [[sectionViewController alloc]init];

[self.navigationController pushViewController:section animated:YES];

}

//視圖將要顯示

-(void)viewWillAppear:(BOOL)animated

{

//調(diào)用數(shù)據(jù)庫單例方法

[[SqlData initData]initSql];

//調(diào)用數(shù)據(jù)庫查詢方法

array = [[SqlData initData]showData];

//關閉數(shù)據(jù)方法

[[SqlData initData]closeSql];

//刷新表格

[self.tableView reloadData];

}

#pragma mark -

#pragma mark UITableViewDataSource

//表格行數(shù)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return array.count;

}

//表格內(nèi)容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];

}

ClassMessage *classmsg = array[indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%ld\n%@\n%@\n%@\n%@\n%@",classmsg.classid,classmsg.name,classmsg.age,classmsg.sex,classmsg.heigt,classmsg.weitht];

cell.textLabel.numberOfLines = 0;

return cell;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

//調(diào)用數(shù)據(jù)庫

[[SqlData initData]initSql];

//是刪除主鍵ID 獲取數(shù)據(jù)庫中的數(shù)據(jù)

[[SqlData initData]deletaData:[array[indexPath.row]classid]];

//關閉數(shù)據(jù)庫

[[SqlData initData]closeSql];

//刪除數(shù)據(jù)

[array removeObject:array[indexPath.row]];

//刷新表格

[self.tableView reloadData];

}

//代理方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

sectionViewController *secVc = [sectionViewController new];

//屬性傳值

secVc.msg = array[indexPath.row];

[self.navigationController pushViewController:secVc animated:YES];

}

Controller===============sectionViewController.h

@property(nonatomic,strong)ClassMessage *msg;


Controller===============sectionViewController.m

#import "sectionViewController.h"

#import "ClassView.h"

#import "ClassMessage.h"

#import "SqlData.h"

@interface sectionViewController ()

{

ClassView *theview;

}

@end

@implementation sectionViewController

- (void)viewDidLoad {

[super viewDidLoad];

theview = [[ClassView alloc]initWithFrame:self.view.frame];

theview.backgroundColor = [UIColor blackColor];

self.view = theview;

theview.nameTf.text = self.msg.name;

theview.ageTf.text = self.msg.age;

theview.sexTf.text = self.msg.sex;

theview.heightTf.text = self.msg.heigt;

theview.weightTf.text = self.msg.weitht;

//標題

if (theview.nameTf.text.length <=0) {

self.title = @"添加數(shù)據(jù)";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];

}else

{

self.title = @"修改數(shù)據(jù)";

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(Edit)];

}

}

-(void)save

{

ClassMessage *message = [ClassMessage new];

message.name =? theview.nameTf.text;

message.age =? theview.ageTf.text;

message.sex =? theview.sexTf.text;

message.heigt =? theview.heightTf.text;

message.weitht =? theview.weightTf.text;

//調(diào)用數(shù)據(jù)庫方法

[[SqlData initData]initSql];

//調(diào)用添加數(shù)據(jù)庫方法

[[SqlData initData]AddData:message];

//調(diào)用關閉數(shù)據(jù)庫方法

[[SqlData initData]closeSql];

//跳轉(zhuǎn)到上一試圖

[self.navigationController popViewControllerAnimated:YES];

}

-(void)Edit

{

self.msg.name =? theview.nameTf.text;

self.msg.age =? theview.ageTf.text;

self.msg.sex =? theview.sexTf.text;

self.msg.heigt =? theview.heightTf.text;

self.msg.weitht =? theview.weightTf.text;

//調(diào)用數(shù)據(jù)庫方法

[[SqlData initData]initSql];

[[SqlData initData]upData:self.msg];

[[SqlData initData]closeSql];

//跳轉(zhuǎn)到上一試圖

[self.navigationController popViewControllerAnimated:YES];

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容