操作数据库

#import<Foundation/Foundation.h>

#import<sqlite3.h>

#import "sqlTestList.h"

#define kFilename  @"testdb.db"

@class sqlTestList;

@interface sqlService : NSObject

{

sqlite3 *_database;

}

@property (nonatomic) sqlite3 *_database;

-(BOOL) createTestList:(sqlite3 *)db;//创建数据库

-(BOOL) insertTestList:(sqlTestList *)insertList;//插入数据

-(BOOL) updateTestList:(sqlTestList *)updateList;//更新数据

-(NSMutableArray*)getTestList;//获取全部数据

- (BOOL) deleteTestList:(sqlTestList *)deletList;//删除数据:

- (NSMutableArray*)searchTestList:(NSString*)searchString;//查询数据库,searchID为要查询数据的ID,返回数据为查询到的数据

@end


//

//  sqlService.m

//  操作数据库

//

//

#import "sqlService.h"

@implementation sqlService

@synthesize _database;

- (id)init

{

return self;

}

//获取document目录并返回数据库目录

- (NSString *)dataFilePath{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSLog(@"=======%@",documentsDirectory);

return [documentsDirectory stringByAppendingPathComponent:@"data.db"];//这里很神奇,可以定义成任何类型的文件,也可以不定义成.db文件,任何格式都行,定义成.sb文件都行,达到了很好的数据隐秘性

}

//创建,打开数据库

- (BOOL)openDB {

//获取数据库路径

NSString *path = [self dataFilePath];

//文件管理器

NSFileManager *fileManager = [NSFileManager defaultManager];

//判断数据库是否存在

BOOL find = [fileManager fileExistsAtPath:path];

//如果数据库存在,则用sqlite3_open直接打开(不要担心,如果数据库不存在sqlite3_open会自动创建)

if (find) {

NSLog(@"Database file have already existed.");

//打开数据库,这里的[path UTF8String]是将NSString转换为C字符串,因为SQLite3是采用可移植的C(而不是

//Objective-C)编写的,它不知道什么是NSString.

if(sqlite3_open([path UTF8String], &_database) != SQLITE_OK) {

//如果打开数据库失败则关闭数据库

sqlite3_close(self._database);

NSLog(@"Error: open database file.");

return NO;

}

//创建一个新表

[self createTestList:self._database];

return YES;

}

//如果发现数据库不存在则利用sqlite3_open创建数据库(上面已经提到过),与上面相同,路径要转换为C字符串

if(sqlite3_open([path UTF8String], &_database) == SQLITE_OK) {

//创建一个新表

[self createTestList:self._database];

return YES;

} else {

//如果创建并打开数据库失败则关闭数据库

sqlite3_close(self._database);

NSLog(@"Error: open database file.");

return NO;

}

return NO;

}

//创建表

- (BOOL) createTestList:(sqlite3*)db {

//这句是大家熟悉的SQL语句

char *sql = "create table if not exists testTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, testID int,testValue text,testName text)";// testID是列名,int 是数据类型,testValue是列名,text是数据类型,是字符串类型

sqlite3_stmt *statement;

//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法

NSInteger sqlReturn = sqlite3_prepare_v2(_database, sql, -1, &statement, nil);

//第一个参数跟前面一样,是个sqlite3 * 类型变量,

//第二个参数是一个 sql 语句。

//第三个参数我写的是-1,这个参数含义是前面 sql 语句的长度。如果小于0,sqlite会自动计算它的长度(把sql语句当成以\0结尾的字符串)。

//第四个参数是sqlite3_stmt 的指针的指针。解析以后的sql语句就放在这个结构里。

//第五个参数是错误信息提示,一般不用,为nil就可以了。

//如果这个函数执行成功(返回值是 SQLITE_OK 且 statement 不为NULL ),那么下面就可以开始插入二进制数据。

//如果SQL语句解析出错的话程序返回

if(sqlReturn != SQLITE_OK) {

NSLog(@"Error: failed to prepare statement:create test table");

return NO;

}

//执行SQL语句

int success = sqlite3_step(statement);

//释放sqlite3_stmt

sqlite3_finalize(statement);

//执行SQL语句失败

if ( success != SQLITE_DONE) {

NSLog(@"Error: failed to dehydrate:create table test");

return NO;

}

NSLog(@"Create table 'testTable' successed.");

return YES;

}

//插入数据

-(BOOL) insertTestList:(sqlTestList *)insertList {

//先判断数据库是否打开

if ([self openDB]) {

sqlite3_stmt *statement;

//这个 sql 语句特别之处在于 values 里面有个? 号。在sqlite3_prepare函数里,?号表示一个未定的值,它的值等下才插入。

static char *sql = "INSERT INTO testTable(testID, testValue,testName) VALUES(?, ?, ?)";

int success2 = sqlite3_prepare_v2(_database, sql, -1, &statement, NULL);

if (success2 != SQLITE_OK) {

NSLog(@"Error: failed to insert:testTable");

sqlite3_close(_database);

return NO;

}

//这里的数字1,2,3代表上面的第几个问号,这里将三个值绑定到三个绑定变量

sqlite3_bind_int(statement, 1, insertList.stuid);

sqlite3_bind_text(statement, 2, [insertList.stupwd UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(statement, 3, [insertList.stuname UTF8String], -1, SQLITE_TRANSIENT);

//执行插入语句

success2 = sqlite3_step(statement);

//释放statement

sqlite3_finalize(statement);

//如果插入失败

if (success2 == SQLITE_ERROR) {

NSLog(@"Error: failed to insert into the database with message.");

//关闭数据库

sqlite3_close(_database);

return NO;

}

//关闭数据库

sqlite3_close(_database);

return YES;

}

return NO;

}

//获取数据

- (NSMutableArray*)getTestList{

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

//判断数据库是否打开

if ([self openDB]) {

sqlite3_stmt *statement = nil;

//sql语句

char *sql = "SELECT testID, testValue ,testName FROM testTable";//从testTable这个表中获取 testID, testValue ,testName,若获取全部的话可以用*代替testID, testValue ,testName。

if (sqlite3_prepare_v2(_database, sql, -1, &statement, NULL) != SQLITE_OK) {

NSLog(@"Error: failed to prepare statement with message:get testValue.");

return NO;

}

else {

//查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值,注意这里的列值,跟上面sqlite3_bind_text绑定的列值不一样!一定要分开,不然会crash,只有这一处的列号不同,注意!

while (sqlite3_step(statement) == SQLITE_ROW) {

sqlTestList* sqlList = [[sqlTestList alloc] init] ;

sqlList.stuid    = sqlite3_column_int(statement,0);

char* strText  = (char*)sqlite3_column_text(statement, 1);

sqlList.stupwd = [NSString stringWithUTF8String:strText];

char *strName = (char*)sqlite3_column_text(statement, 2);

sqlList.stuname = [NSString stringWithUTF8String:strName];

[array addObject:sqlList];

}

}

sqlite3_finalize(statement);

sqlite3_close(_database);

}

return array;//定义了自动释放的NSArray,这样不是个好办法,会造成内存泄露,建议大家定义局部的数组,再赋给属性变量。

}

//更新数据

-(BOOL) updateTestList:(sqlTestList *)updateList{

if ([self openDB]) {

sqlite3_stmt *statement;//这相当一个容器,放转化OK的sql语句

//组织SQL语句

char *sql = "update testTable set testValue = ? and testName = ? WHERE testID = ?";

//将SQL语句放入sqlite3_stmt中

int success = sqlite3_prepare_v2(_database, sql, -1, &statement, NULL);

if (success != SQLITE_OK) {

NSLog(@"Error: failed to update:testTable");

sqlite3_close(_database);

return NO;

}

//这里的数字1,2,3代表第几个问号。这里只有1个问号,这是一个相对比较简单的数据库操作,真正的项目中会远远比这个复杂

//绑定text类型的数据库数据

sqlite3_bind_text(statement, 3, [updateList.stuname UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(statement, 2, [updateList.stupwd UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_int(statement, 1, updateList.stuid);

//执行SQL语句。这里是更新数据库

success = sqlite3_step(statement);

//释放statement

sqlite3_finalize(statement);

//如果执行失败

if (success == SQLITE_ERROR) {

NSLog(@"Error: failed to update the database with message.");

//关闭数据库

sqlite3_close(_database);

return NO;

}

//执行成功后依然要关闭数据库

sqlite3_close(_database);

return YES;

}

return NO;

}

//删除数据

- (BOOL) deleteTestList:(sqlTestList *)deletList{

if ([self openDB]) {

sqlite3_stmt *statement;

//组织SQL语句

static char *sql = "delete from testTable  where testID = ? and testValue = ? and testName = ?";

//将SQL语句放入sqlite3_stmt中

int success = sqlite3_prepare_v2(_database, sql, -1, &statement, NULL);

if (success != SQLITE_OK) {

NSLog(@"Error: failed to delete:testTable");

sqlite3_close(_database);

return NO;

}

//这里的数字1,2,3代表第几个问号。这里只有1个问号,这是一个相对比较简单的数据库操作,真正的项目中会远远比这个复杂

sqlite3_bind_int(statement, 1, deletList.stuid);

sqlite3_bind_text(statement, 2, [deletList.stupwd UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(statement, 3, [deletList.stuname UTF8String], -1, SQLITE_TRANSIENT);

//执行SQL语句。这里是更新数据库

success = sqlite3_step(statement);

//释放statement

sqlite3_finalize(statement);

//如果执行失败

if (success == SQLITE_ERROR) {

NSLog(@"Error: failed to delete the database with message.");

//关闭数据库

sqlite3_close(_database);

return NO;

}

//执行成功后依然要关闭数据库

sqlite3_close(_database);

return YES;

}

return NO;

}

//查询数据

- (NSMutableArray*)searchTestList:(NSString*)searchString{

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

//判断数据库是否打开

if ([self openDB]) {

sqlite3_stmt *statement = nil;

//sql语句

NSString *querySQL = [NSString stringWithFormat:@"SELECT * from testTable where testName like \"%@\"",searchString];

const char *sql = [querySQL UTF8String];

//        char *sql = "SELECT * FROM testTable WHERE testName like ?";//这里用like代替=可以执行模糊查找,原来是"SELECT * FROM testTable WHERE testName = ?"

if (sqlite3_prepare_v2(_database, sql, -1, &statement, NULL) != SQLITE_OK) {

NSLog(@"Error: failed to prepare statement with message:search testValue.");

return NO;

} else {

sqlTestList *searchList = [[sqlTestList alloc]init];

//            sqlite3_bind_int(statement, 1, searchID);

sqlite3_bind_text(statement, 3, [searchString UTF8String], -1, SQLITE_TRANSIENT);

//查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。

while (sqlite3_step(statement) == SQLITE_ROW) {

sqlTestList* sqlList = [[sqlTestList alloc] init] ;

sqlList.stuid  = sqlite3_column_int(statement,1);

char* strText  = (char*)sqlite3_column_text(statement, 2);

sqlList.stupwd = [NSString stringWithUTF8String:strText];

char *strName = (char*)sqlite3_column_text(statement, 3);

sqlList.stuname = [NSString stringWithUTF8String:strName];

[array addObject:sqlList];

}

}

sqlite3_finalize(statement);

sqlite3_close(_database);

}

return array;

}

@end


#import<Foundation/Foundation.h>

#import<sqlite3.h>

@interface sqlTestList : NSObject//重新定义了一个类,专门用于存储数据

{

int sqlID;

NSString *sqlText;

NSString *sqlname;

}

@property (nonatomic) int stuid;

@property (nonatomic, retain) NSString *stupwd;

@property (nonatomic, retain) NSString *stuname;

@end

#import "sqlTestList.h"

@implementation sqlTestList

@synthesize stuid;

@synthesize stupwd;

@synthesize stuname;

-(id) init

{

stuid = 0;

stupwd = @"";

stuname = @"";

return self;

};

@end


#import<UIKit/UIKit.h>

#import "sqlService.h"

@interface operateSqlViewController : UIViewController

{

UITextField *idValue;

UITextField *textValue;

UITextField *textName;

int oprateType;//区分数据插入与更新

sqlTestList *sqlValue;

}

@property (nonatomic, retain) UITextField *idValue;

@property (nonatomic, retain) UITextField *textValue;

@property (nonatomic, retain) UITextField *textName;

@property (nonatomic, retain) sqlTestList *sqlValue;

@property (nonatomic) int oprateType;

@end

#import "operateSqlViewController.h"

@interface operateSqlViewController ()

@end

@implementation operateSqlViewController

@synthesize idValue;

@synthesize textValue;

@synthesize oprateType;

@synthesize sqlValue;

@synthesize textName;

- (void)viewDidLoad {

[super viewDidLoad];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss:)];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]

initWithTitle:@"保存" style: UIBarButtonItemStyleBordered target:self action:@selector(saveValue:)];

[[self navigationItem] setLeftBarButtonItem:backButton];

[[self navigationItem] setRightBarButtonItem:saveButton];

if (oprateType == 0) {

[self.navigationItem setTitle:@"数据插入"];

}

else if(oprateType == 1){

[self.navigationItem setTitle:@"数据更新"];

idValue.text = [NSString stringWithFormat:@"%d", sqlValue.stuid];

textValue.text = sqlValue.stuname;

textName.text = sqlValue.stuname;

}

}

- (void)dismiss:(id)sender{

[[self parentViewController] dismissModalViewControllerAnimated:YES];

}

- (void)saveValue:(id)sender{

if (idValue.text.length == 0) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"请输入ID"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

return;

}

if (textValue.text.length == 0) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"请输入电话"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

return;

}

if (textName.text.length == 0) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"请输入姓名"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

return;

}

//初始化数据库

sqlService *sqlSer = [[sqlService alloc] init];

//数据库插入

if (oprateType == 0) {

sqlTestList *sqlInsert = [[sqlTestList alloc]init];

sqlInsert.stuid = [idValue.text intValue];

sqlInsert.stupwd = textValue.text;

sqlInsert.stuname = textName.text;

//调用封装好的数据库插入函数

if ([sqlSer insertTestList:sqlInsert]) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"插入数据成功"delegate:self cancelButtonTitle:@"好"otherButtonTitles:nil];

[alert show];

}

else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"插入数据失败"delegate:self cancelButtonTitle:@"好"otherButtonTitles:nil];

[alert show];

}

}

//数据库更新

if(oprateType == 1){

sqlTestList *newValue = [[sqlTestList alloc]init];

newValue.stuid = [idValue.text intValue];

newValue.stupwd = textValue.text;

newValue.stuname = textName.text;

//调用封装好的更新数据库函数

if ([sqlSer updateTestList:newValue]) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"更新数据成功"delegate:self cancelButtonTitle:@"好"otherButtonTitles:nil];

[alert show];

}

else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"更新数据失败"delegate:self cancelButtonTitle:@"好"otherButtonTitles:nil];

[alert show];

}

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


#import<UIKit/UIKit.h>

#import "sqlService.h"@interface SQLite3TestViewController : UIViewController{

UITableView *utableView;

NSArray *listData;

UISearchBar *searchBar;//搜索栏

}

@property (nonatomic, retain) UITableView *utableView;

@property (nonatomic, retain) UISearchBar *searchBar;

@property (nonatomic, retain) NSArray *listData;

- (void)insertValue;

- (void)updateValue;

- (void)getAllValue;

- (void)deleteValue;

- (void)searchValue;

@end

#import "SQLite3TestViewController.h"

#import "operateSqlViewController.h"

@interface SQLite3TestViewController ()

@end

@implementation SQLite3TestViewController

@synthesize utableView;

@synthesize listData;

@synthesize searchBar;

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

sqlService *sqlSer = [[sqlService alloc] init];

listData = [sqlSer getTestList];//先初始化那个专门用于存数据的类,才调用类获取数据的方法

self.utableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

self.utableView.backgroundColor = [UIColor colorWithRed:0.879 green:1.000 blue:0.467 alpha:1.000];

self.utableView.delegate = self;

self.utableView.dataSource = self;

[self.view addSubview:self.utableView];

}

- (void)viewDidAppear:(BOOL)animated{//在这里写是为了等待时间缩短一点,数据如果很多的,在这里写可以让数据提前加载

sqlService *sqlSer = [[sqlService alloc] init];

listData = [sqlSer getTestList];

[utableView reloadData];

}

- (void)viewDidUnload {

utableView = nil;

listData = nil;

searchBar = nil;

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (void)insertValue{

[searchBar resignFirstResponder];//触发这个insertValue方法时隐藏键盘

operateSqlViewController *operateController = [[operateSqlViewController alloc] init ];

UINavigationController *theNavController = [[UINavigationController alloc]

initWithRootViewController:operateController];//这里如果不初始化一个UINavigationController类的对象来存放operateSqlViewController类的UIViewController,就不会有最上面的导航栏了。

operateController.oprateType = 0;//optrateType为0时为数据插入

theNavController.navigationBar.tintColor = [UIColor blackColor];

[self presentModalViewController:theNavController animated:YES];

}

- (void)updateValue{

[searchBar resignFirstResponder];

NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];

if (indexPath == nil) {

//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"message:@"请选择要更新的项" delegate:selfcancelButtonTitle:@"好" otherButtonTitles:nil];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择要更新的项" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];

[alert show];

}

NSUInteger row = [indexPath row];

sqlTestList *sqlList = [[sqlTestList alloc]init];

sqlList = [listData objectAtIndex:(row - 1)];//在这里面获取点击的行,因为table的第一行没显示数据,所以这里要减1。

operateSqlViewController *operateController = [[operateSqlViewController alloc] init ];

UINavigationController *theNavController = [[UINavigationController alloc]

initWithRootViewController:operateController];

operateController.oprateType = 1;//optrateType为1时为数据更新

operateController.sqlValue = sqlList;

theNavController.navigationBar.tintColor = [UIColor blackColor];

[self presentModalViewController:theNavController animated:YES];

}

- (void)getAllValue{

[searchBar resignFirstResponder];

sqlService *sqlSer = [[sqlService alloc] init];

listData = [sqlSer getTestList];

[utableView reloadData];

}

- (void)deleteValue{

[searchBar resignFirstResponder];

NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];

if (indexPath == nil) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"请选择要删除的项"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

}

NSUInteger row = [indexPath row];

sqlTestList *sqlList = [[sqlTestList alloc]init];

sqlList = [listData objectAtIndex:(row - 1)];

sqlService *sqlSer = [[sqlService alloc] init];

if ([sqlSer deleteTestList:sqlList]) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"删除数据成功"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

//删除成功后重新获取数据更新列表

listData = [sqlSer getTestList];

[utableView reloadData];

}

else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"删除数据失败"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

}

}

- (void)searchValue{

if ([searchBar.text isEqualToString:@""]) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"请输入要查询数据的ID"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

}

else {

//        int idNum = [searchBar.text intValue];

NSString *str = searchBar.text;

sqlService *sqlSer = [[sqlService alloc] init];

listData = [sqlSer searchTestList:str];

if ([listData  count] == 0) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"

message:@"sorry,未查询到数据,请查看name是否有误"

delegate:self

cancelButtonTitle:@"好"

otherButtonTitles:nil];

[alert show];

}

[searchBar resignFirstResponder];

[utableView reloadData];

}

}

#pragma mark -

#pragma mark Table View Data Source Methods

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

{

// Return the number of rows in the section.

return [listData count] + 1;//从第二行开始,第一行不显示数据

}

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

{

NSString *CustomIdentifier =  [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];

//cell不重用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomIdentifier];

if (indexPath.row == 0)

cell.selectionStyle = UITableViewCellSelectionStyleNone;

if ( cell == nil ) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CustomIdentifier];

cell.backgroundColor = [UIColor clearColor];

}

if (indexPath.row > 0)

{

NSUInteger row = [indexPath row];

sqlTestList *sqlList = [[sqlTestList alloc] init] ;

if (listData != nil)

sqlList = [listData objectAtIndex: (row - 1)];//读取数据的时候也要减一行,从第二行开始

UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0+40, 10, 70, 30)];

UILabel *IDLabel = [[UILabel alloc]initWithFrame:CGRectMake(90+40, 10, 70, 30)];

UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(180+40, 10, 70, 30)];

nameLabel.text = sqlList.stuname;

IDLabel.text = sqlList.stupwd;

valueLabel.text = [NSString stringWithFormat:@"%d",sqlList.stuid];

[cell.contentView addSubview:nameLabel];

[cell.contentView addSubview:IDLabel];

[cell.contentView addSubview:valueLabel];

}

else

{

for (int i = 0; i < 3; i ++) {

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(90 * i + 40, 10, 70 , 30)];

NSArray *array = [NSArray arrayWithObjects:@"姓名",@"ID",@"电话", nil];

label.text = [array objectAtIndex:i];

label.backgroundColor = [UIColor clearColor];

[cell.contentView addSubview:label];

}

}

return cell;

}

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[searchBar resignFirstResponder];

if (indexPath.row == 0) {

return nil;//让第一行不能点击

}

else

return indexPath;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end


#import<UIKit/UIKit.h>

#import "SQLite3TestViewController.h"@interface AppDelegate : UIResponder@property (strong, nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[SQLite3TestViewController alloc] init]];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249

推荐阅读更多精彩内容