iOS Sqlite数据库增删改查基本操作

2023-11-19

Sqlite是ios上最常用的数据库之一,大家还是有必要了解一下的。实现效果如图:


先来看看数据库方法类,将各个操作都封装在一个类里面,达到代码重用的目的,这是程序员都应该努力去实现的目标,这样在下一次用到同样的方法和类的时候,就可以直接使用封装好的类,可以节约大量的时间。

先来看看.h文件

<span style="font-family:FangSong_GB2312;font-size:14px;">#import <Foundation/Foundation.h>
#import <sqlite3.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

@interface sqlTestList : NSObject//重新定义了一个类,专门用于存储数据
{
    int sqlID;
    NSString *sqlText;
    NSString *sqlname;
}

@property (nonatomic) int sqlID;
@property (nonatomic, retain) NSString *sqlText;
@property (nonatomic, retain) NSString *sqlname;

@end</span>

下面再看看 .m文件
<span style="font-family:FangSong_GB2312;font-size:14px;">//
//  sqlService.m
//  SQLite3Test
//
//  Created by fengxiao on 11-11-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "sqlService.h"


@implementation sqlService

@synthesize _database;

- (id)init
{
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

//获取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.sqlID);
        sqlite3_bind_text(statement, 2, [insertList.sqlText UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [insertList.sqlname 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.sqlID    = sqlite3_column_int(statement,0);
                char* strText   = (char*)sqlite3_column_text(statement, 1);
                sqlList.sqlText = [NSString stringWithUTF8String:strText];
                char *strName = (char*)sqlite3_column_text(statement, 2);
                sqlList.sqlname = [NSString stringWithUTF8String:strName];
                [array addObject:sqlList];
                [sqlList release];
            }
        }
        sqlite3_finalize(statement);
        sqlite3_close(_database);
    }
    
    return [array retain];//定义了自动释放的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.sqlname UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 2, [updateList.sqlText UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(statement, 1, updateList.sqlID);
        
        //执行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.sqlID);
        sqlite3_bind_text(statement, 2, [deletList.sqlText UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [deletList.sqlname 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.sqlID   = sqlite3_column_int(statement,1);
                char* strText   = (char*)sqlite3_column_text(statement, 2);
                sqlList.sqlText = [NSString stringWithUTF8String:strText];
                char *strName = (char*)sqlite3_column_text(statement, 3);
                sqlList.sqlname = [NSString stringWithUTF8String:strName];
                [array addObject:sqlList];
                [sqlList release];
            }
            [searchList release];                
        }
        sqlite3_finalize(statement);
        sqlite3_close(_database);
    }
    
    return [array retain];
}

@end


@implementation sqlTestList//刚才.h文件里定义的类在这实现

@synthesize sqlID;
@synthesize sqlText;
@synthesize sqlname;
-(id) init
{
    sqlID = 0;
    sqlText = @"";
    sqlname = @"";
    return self;
};
-(void) dealloc
{
    if ((sqlText != nil) && (sqlname != nil)) {
        [sqlText release];
        [sqlname release];
    }

    [super dealloc];
}

@end</span>

这就是封装好的类,可以重用哦!

下面是添加数据页面


来看.h文件

<span style="font-family:FangSong_GB2312;font-size:14px;">#import <UIKit/UIKit.h>
#import "sqlService.h"

@interface operateSqlViewController : UIViewController {
    
    UITextField *idValue;
    UITextField *textValue;
    UITextField *textName;
    int oprateType;//区分数据插入与更新
    sqlTestList *sqlValue;
}

@property (nonatomic, retain) IBOutlet UITextField *idValue;
@property (nonatomic, retain) IBOutlet UITextField *textValue;
@property (nonatomic, retain) IBOutlet UITextField *textName;
@property (nonatomic, retain) sqlTestList *sqlValue;
@property (nonatomic) int oprateType;

@end</span>

再来看看.m文件
<span style="font-family:FangSong_GB2312;font-size:14px;">#import "operateSqlViewController.h"


@implementation operateSqlViewController
@synthesize idValue;
@synthesize textValue;
@synthesize oprateType;
@synthesize sqlValue;
@synthesize textName;
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)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];
    
    [backButton release];
    [saveButton release];
    
    if (oprateType == 0) {
        [self.navigationItem setTitle:@"数据插入"];
    }
    else if(oprateType == 1){
        [self.navigationItem setTitle:@"数据更新"];
        idValue.text = [NSString stringWithFormat:@"%d", sqlValue.sqlID];
        textValue.text = sqlValue.sqlText;
        textName.text = sqlValue.sqlname;
    }
}

- (void)viewDidUnload {
    idValue = nil;
    textValue = nil;
    textName = nil;
    sqlValue = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [idValue release];
    [textValue release];
    [sqlValue release];
    [textName release];
    [super dealloc];
}

- (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];
            [alert release];
        return;
    }
    if (textValue.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请输入电话" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    if (textName.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请输入姓名" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    
    
    //初始化数据库
    sqlService *sqlSer = [[sqlService alloc] init];
    
    //数据库插入
    if (oprateType == 0) {
        
        sqlTestList *sqlInsert = [[sqlTestList alloc]init];
        sqlInsert.sqlID = [idValue.text intValue];
        sqlInsert.sqlText = textValue.text;
        sqlInsert.sqlname = textName.text;
        
        //调用封装好的数据库插入函数
        if ([sqlSer insertTestList:sqlInsert]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                            message:@"插入数据成功" 
                                                           delegate:self
                                                  cancelButtonTitle:@"好" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                            message:@"插入数据失败" 
                                                           delegate:self
                                                  cancelButtonTitle:@"好" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
            
        }
        [sqlInsert release];

    }
    //数据库更新
    if(oprateType == 1){
        
        sqlTestList *newValue = [[sqlTestList alloc]init];
        newValue.sqlID = [idValue.text intValue];
        newValue.sqlText = textValue.text;
        newValue.sqlname = textName.text;
        
        //调用封装好的更新数据库函数
        if ([sqlSer updateTestList:newValue]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                            message:@"更新数据成功" 
                                                           delegate:self
                                                  cancelButtonTitle:@"好" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                            message:@"更新数据失败" 
                                                           delegate:self
                                                  cancelButtonTitle:@"好" 
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
            
        }
        
        [newValue release];
    }

}

@end</span>

代码写的有些啰嗦,不过不难容易看懂,不多解释了,要在xib文件中添加3个UITextField和ULabel,要记得连线。

在主界面的.h文件

<span style="font-family:FangSong_GB2312;font-size:14px;">#import <UIKit/UIKit.h>
#import "sqlService.h"
@interface SQLite3TestViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {

    UITableView *utableView;
    NSArray *listData;
    UISearchBar *searchBar;//搜索栏
    
}

@property (nonatomic, retain) IBOutlet UITableView *utableView;
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) NSArray *listData;

- (IBAction)insertValue;
- (IBAction)updateValue;
- (IBAction)getAllValue;
- (IBAction)deleteValue;
- (IBAction)searchValue;


@end</span>

.m文件
<span style="font-family:FangSong_GB2312;font-size:14px;">//
//  SQLite3TestViewController.m
//  SQLite3Test
//
//  Created by fengxiao on 11-11-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SQLite3TestViewController.h"
#import "operateSqlViewController.h"

@implementation SQLite3TestViewController
@synthesize utableView;
@synthesize listData;
@synthesize searchBar;


- (void)viewDidLoad{
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];//先初始化那个专门用于存数据的类,才调用类获取数据的方法
}

- (void)viewDidAppear:(BOOL)animated{//在这里写是为了等待时间缩短一点,数据如果很多的,在这里写可以让数据提前加载
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];
    [sqlSer release];
    [utableView reloadData];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    utableView = nil;
    listData = nil;
    searchBar = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [utableView release];
    [listData release];
    [searchBar release];
    [super dealloc];
}


- (IBAction)insertValue{
    
    [searchBar resignFirstResponder];//触发这个insertValue方法时隐藏键盘
    operateSqlViewController *operateController = [[operateSqlViewController alloc] init ];
    UINavigationController *theNavController = [[UINavigationController alloc]
                                                initWithRootViewController:operateController];//这里如果不初始化一个UINavigationController类的对象来存放operateSqlViewController类的UIViewController,就不会有最上面的导航栏了。
    operateController.oprateType = 0;//optrateType为0时为数据插入
    [operateController release];
    theNavController.navigationBar.tintColor = [UIColor blackColor];
    [self presentModalViewController:theNavController animated:YES];
    [theNavController release];
}

- (IBAction)updateValue{
    
    [searchBar resignFirstResponder];
    NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];    
    if (indexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请选择要更新的项" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    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];
    [sqlList release];
    [operateController release];
    [theNavController release];
}

- (IBAction)getAllValue{
    
    [searchBar resignFirstResponder];
    
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];
    [utableView reloadData];
    [sqlSer release];
    
}
- (IBAction)deleteValue{
    
    [searchBar resignFirstResponder];
    
    NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];
    
    if (indexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请选择要删除的项" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    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];
         [alert release];
         
         //删除成功后重新获取数据更新列表
         listData = [sqlSer getTestList];
         [utableView reloadData];

     }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"删除数据失败" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    [sqlList release];
    [sqlSer release];
}
- (IBAction)searchValue{
    
    if ([searchBar.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请输入要查询数据的ID" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    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];
            [alert release];
            return;
        }
        [searchBar resignFirstResponder];
        [utableView reloadData];
        [sqlSer release];
    
    }
}

#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%d",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] autorelease];
        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.sqlname;
        IDLabel.text = sqlList.sqlText;
        valueLabel.text = [NSString stringWithFormat:@"%d",sqlList.sqlID];
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:IDLabel];
        [cell.contentView addSubview:valueLabel];
        [nameLabel release];
        [IDLabel release];
        [valueLabel release];
    }
    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];
            [label release];
        }
    }
    return cell;
}

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [searchBar resignFirstResponder];
    if (indexPath.row == 0) {
        return nil;//让第一行不能点击
    }
    else
        return indexPath;
}

@end//
//  SQLite3TestViewController.m
//  SQLite3Test
//
//  Created by fengxiao on 11-11-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SQLite3TestViewController.h"
#import "operateSqlViewController.h"

@implementation SQLite3TestViewController
@synthesize utableView;
@synthesize listData;
@synthesize searchBar;


- (void)viewDidLoad{
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];//先初始化那个专门用于存数据的类,才调用类获取数据的方法
}

- (void)viewDidAppear:(BOOL)animated{//在这里写是为了等待时间缩短一点,数据如果很多的,在这里写可以让数据提前加载
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];
    [sqlSer release];
    [utableView reloadData];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    utableView = nil;
    listData = nil;
    searchBar = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [utableView release];
    [listData release];
    [searchBar release];
    [super dealloc];
}


- (IBAction)insertValue{
    
    [searchBar resignFirstResponder];//触发这个insertValue方法时隐藏键盘
    operateSqlViewController *operateController = [[operateSqlViewController alloc] init ];
    UINavigationController *theNavController = [[UINavigationController alloc]
                                                initWithRootViewController:operateController];//这里如果不初始化一个UINavigationController类的对象来存放operateSqlViewController类的UIViewController,就不会有最上面的导航栏了。
    operateController.oprateType = 0;//optrateType为0时为数据插入
    [operateController release];
    theNavController.navigationBar.tintColor = [UIColor blackColor];
    [self presentModalViewController:theNavController animated:YES];
    [theNavController release];
}

- (IBAction)updateValue{
    
    [searchBar resignFirstResponder];
    NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];    
    if (indexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请选择要更新的项" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    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];
    [sqlList release];
    [operateController release];
    [theNavController release];
}

- (IBAction)getAllValue{
    
    [searchBar resignFirstResponder];
    
    sqlService *sqlSer = [[sqlService alloc] init];
    listData = [sqlSer getTestList];
    [utableView reloadData];
    [sqlSer release];
    
}
- (IBAction)deleteValue{
    
    [searchBar resignFirstResponder];
    
    NSIndexPath *indexPath = [utableView  indexPathForSelectedRow];
    
    if (indexPath == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请选择要删除的项" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    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];
         [alert release];
         
         //删除成功后重新获取数据更新列表
         listData = [sqlSer getTestList];
         [utableView reloadData];

     }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"删除数据失败" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    [sqlList release];
    [sqlSer release];
}
- (IBAction)searchValue{
    
    if ([searchBar.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" 
                                                        message:@"请输入要查询数据的ID" 
                                                       delegate:self
                                              cancelButtonTitle:@"好" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    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];
            [alert release];
            return;
        }
        [searchBar resignFirstResponder];
        [utableView reloadData];
        [sqlSer release];
    
    }
}

#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%d",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] autorelease];
        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.sqlname;
        IDLabel.text = sqlList.sqlText;
        valueLabel.text = [NSString stringWithFormat:@"%d",sqlList.sqlID];
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:IDLabel];
        [cell.contentView addSubview:valueLabel];
        [nameLabel release];
        [IDLabel release];
        [valueLabel release];
    }
    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];
            [label release];
        }
    }
    return cell;
}

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [searchBar resignFirstResponder];
    if (indexPath.row == 0) {
        return nil;//让第一行不能点击
    }
    else
        return indexPath;
}

@end</span>


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS Sqlite数据库增删改查基本操作 的相关文章

随机推荐

  • 2D/3D人体姿态估计 (2D/3D Human Pose Estimation)

    1 基本概念 算法改进入口 网络设计 特征流 损失函数 数据集的重要性 只要有一个好的 针对性的数据集 问题都可以解决 过集成新一代AutoML技术 可降低算法试错成本 人体姿态估计 Human Pose Estimation 是指图像或视
  • 在C语言中,“>>=”;“<<=”;“&=”;“

    C语言中 gt gt 的意思是 右移后赋值 示例 x 8 1000 x gt gt 3 结果为 0001 C语言中 lt lt 的意思是 左移后赋值 示例 x 1 0001 x lt lt 3 结果为 1000 C语言中 的意思是 按位与后
  • pycharm 代码上传到gitee仓库里

    目录 一 准备 二 1 新建仓库 2 commit和push 一 准备 1 在pycharm中安装插件 github git 2 在本地安装git 二 1 新建仓库 首先在pycharm中打开自己的本地项目 如果没有配置过 主界面应该是这样
  • 2023年数学建模B组:利用AHP层次分析法解决实际问题(Matlab)

    目录 利用AHP层次分析法解决实际问题 Matlab实现 介绍 案例背景 步骤1 建立层次结构模型
  • Flutter酷炫的路由动画效果

    现在Flutter的路由效果已经非常不错了 能满足大部分App的需求 但是谁不希望自己的App更酷更炫那 下面介绍几个酷炫的路由动画 其实路由动画的原理很简单 就是重写并继承PageRouterBuilder这个类里的transitions
  • 详解JS前端异步文件加载篇之Async与Defer区别

    目录 同步 异步及推迟的概念 async和defer解决文件加载阻塞问题 在了解async和defer的区别之前 我们需要先了解同步 异步和推迟的概念 同步 异步及推迟的概念 假如现在有一条非常狭隘的胡同 里面有两个人挨着走 那么现在请问后
  • Java集合的两种遍历方式

    Java集合共有两种遍历方式 增强for循环 foreach 迭代器 Main方法 public static void main String args 创建集合 Collection collection new ArrayList 添
  • XXL-JOB分布式任务调度平台配置详解

    XXL JOB是一个分布式任务调度平台 其核心设计目标是开发迅速 学习简单 轻量级 易扩展 个人建议 对于需要定时调度任务开箱即用的小伙伴来说 完全可以学习参考下 本文主要介绍了Xxl Job分布式任务调度框架的配置信息详解 以及路由策略
  • git clone下新项目后运行报错‘vue-cli-service‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。

    报错 vue cli service 不是内部或外部命令 也不是可运行的程序 或批处理文件 因为项目里还没有node modules这个包 需要运行npm install 运行后没有报错但是有个警告 npm WARN read shrink
  • MySQL导入导出数据mysqldump,mysql,select into file,load data

    研发人员往往需要从数据库中导出数据 或者将数据导入到数据库中 一些客户端工具提供了简单方便的功能 可以让他们不用使用命令进行操作 但是客户端工具可能会受到环境的限制而不能使用 所以 研发人员有必要掌握一些常用的命令来进行操作数据 MySQL
  • 残差神经网络的研究

    目录 一 ResNet残差神经网络 1 1 提出 1 2 作用 1 3 应用场景 1 4 残差单元的结构 1 4 1 残差网络得名的原因 1 4 2 残差网络可以有效缓解退化现象的原因 1 4 3 数学原理 二 附录 2 1 残差神经网络可
  • GD32F303调试小记(一)之USART(接收中断、接收空闲中断+DMA、发送DMA)

    前言 之前写了GD32F103调试小记 二 之USART 接收中断 接收空闲中断 DMA 发送DMA 一文 这次我们来看看GD32F303的USART是如何配置的 结合这两篇文章 相信大家GD32的USART配置流程会十分熟悉 DMA 能大
  • SpringBoot项目实战,附源码

    SpringBoot2 0笔记 一 SpringBoot基本操作 环境搭建及项目创建 有demo 二 SpringBoot基本操作 使用IDEA打war包发布及测试 三 SpringBoot基本操作 SpringBoot整合SpringDa
  • 逆向爬虫09 协程 & 异步编程(asyncio)

    逆向爬虫09 协程 异步编程 asyncio 1 什么是协程 What 协程 Coroutine 也可以被称为微线程 是一种用户态内的上下文切换技术 简而言之 其实就是通过一个线程实现代码块相互切换执行 def func1 print 1
  • Unity 拖尾(Trail Renderer)效果的实现

    1 新建场景 创建一个球 在球上添加组件Trail Renderer 2 在Trail Renderer组件设置Time为0 5 Materials材质 3 Width下点击右键 Add key 添加控制点 起始宽度为1 0 结束宽度为0
  • Nginx配置https网站

    1 什么是https https超文本传输安全协议是http ssl安全套接层和tls传输层安全的组合 用于提供加密通信和鉴定网络服务器的身份 网上的支付交易 个人隐私和企业中的敏感信息等越来越受到人们的关注和保护 因此https目前已经是
  • ICCV 2021: AdaAttN: Revisit Attention Mechanism in Arbitrary Neural Style Transfer 阅读笔记

    ICCV 2021 AdaAttN Revisit Attention Mechanism in Arbitrary Neural Style Transfer 论文 https arxiv org pdf 2108 03647 pdf 代
  • app上架流程的整理

    app的上架流程 一 准备工作 首先需要有开发者账号 企业级的账号是299 个人开发者账号是99 没有的话可以登录http developer apple com 自行申请 假如你已经有账号了 进入苹果官网点击Accout登录 二 申请证书
  • Android课设——理财小助手

    一 app介绍 理财小助手是一款利用Android studio软件实现的APP 可以录入每天的消费项目以及消费金额 同时也可以查找消费记录 统计消费总额 我用到的Android studio版本如下 二 模块设计 下面是我实现的一些模块
  • iOS Sqlite数据库增删改查基本操作

    Sqlite是ios上最常用的数据库之一 大家还是有必要了解一下的 实现效果如图 先来看看数据库方法类 将各个操作都封装在一个类里面 达到代码重用的目的 这是程序员都应该努力去实现的目标 这样在下一次用到同样的方法和类的时候 就可以直接使用