用了sqlite数据库的通讯录

源文件支持交流修改使用

开发语言:

分类:其他系统

    分享到:

源码搜索

  • 详情介绍 - 【用了sqlite数据库的通讯录】
+ (DataBaseManager *)shareDataBaseManager
{
 
    if (dataBase == nil) {
        dataBase = [[DataBaseManager alloc] init];
    }
    return dataBase;
 
 
}
 
//打开数据库
-(void)openDataBaseManager
{
     
    //如果数据库已经存在,不做任何操作
    if (db) {
        return;
    }
    int result = sqlite3_open(self.dbPath.UTF8String, &db);
     
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
         
    }else{
         
        NSLog(@"打开失败,是白带吗为%d",result);
         
    }
 
 
}
 
//关闭数据库
 
-(void)closeDataBaseManager
{
 
    int result = sqlite3_close(db);
    if (result == SQLITE_OK) {
        db = nil;
        NSLog(@"关闭成功");
    }else{
         
        NSLog(@"关闭失败,错误代码为%d",result);
    }
 
     
}
 
//创建表
 
-(void)creatTable
{
 
    //准备SQL语句
     
    NSString *sqlWord = @"CREATE TABLE IF NOT EXISTS 'person' ('number' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'name' TEXT, 'gender' TEXT, 'phoneNumber' TEXT, 'key' TEXT,'QQNumber' TEXT)";
    //执行SQL语句
    int result = sqlite3_exec(db, sqlWord.UTF8String, NULL, NULL, NULL);
     
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
         
        NSLog(@"创建表失败,错误代码为%d",result);
         
    }
     
     
 
}
 
//增
-(void)insertDataFromPerson:(Person *)person;
{
 
     
    //插入sql语句,数据不确定,所以在value里使用?
    NSString *sqlWord = @"insert into person (name, gender, phoneNumber, key, QQNumber) values (?, ?, ?, ?, ?)";
    sqlite3_stmt *stmt = nil;
     
    int result = sqlite3_prepare(db, sqlWord.UTF8String, -1, &stmt, NULL);
     
    //如果预执行成功的话,那么就往里放数据了
    if (result == SQLITE_OK) {
        
        sqlite3_bind_text(stmt, 1, person.name.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 2, person.gender.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 3, person.phoneNumber.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 4, person.key.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 5, person.QQNumber.UTF8String, -1, NULL);
         
        // sql已经全了
        // 伴随指针执行,如果为SQLITE_DONE 代表执行成功,并且成功插入数据
        if (sqlite3_step(stmt) == SQLITE_DONE) {
             
            NSLog(@"插入成功");
             
        }else
        {
            NSLog(@"插入失败");
             
        }
    }else
    {
         
        NSLog(@"%d",result);
         
    }
    //释放伴随指针
    sqlite3_finalize(stmt);
     
    NSLog(@"%@",_dbPath);
 
     
 
}
 
//删
-(void)deleteDataWithNumber:(NSInteger)number
{
 
    NSString *sqlWord = [NSString stringWithFormat:@"DELETE FROM 'person' WHERE number = '%lu'",number];
    int result = sqlite3_exec(db, sqlWord.UTF8String, NULL, NULL, NULL);
     
    if (result == SQLITE_OK) {
        NSLog(@"删除数据成功");
    }else{
         
        NSLog(@"删除数据失败,错误代码为%d",result);
         
    }
     
 
}
 
//查
-(NSArray *)selectedDataWithKey:(NSString *)key
{
 
    //准备步骤
    //1.创建可变数组
    NSMutableArray *dataArray = nil;
    //2.创建伴随指针
    sqlite3_stmt *stmt = NULL;
     
    //1.准备sql语句
    NSString *sqlWord = [NSString stringWithFormat:@"SELECT *FROM 'person' WHERE key = '%@'",key];
     
    //2.预执行
    int result = sqlite3_prepare(db, sqlWord.UTF8String, -1, &stmt, NULL);
     
    //执行查询语句
    if (result == SQLITE_OK) {
        dataArray = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            Person *person = [Person new];
            //赋值顺序一定要按照字段顺序来匹配
            person.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
           person.gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            person.phoneNumber = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            person.key = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            person.QQNumber = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
            [dataArray addObject:person];
        }
    }
    //释放伴随指针
    sqlite3_finalize(stmt);
     NSLog(@"%@",_dbPath);
     
    return dataArray;
 
 
}
 
- (NSArray *)searchAll
{
     
    //准备步骤
    //1.创建可变数组
    NSMutableArray *dataArray = nil;
    //2.创建伴随指针
    sqlite3_stmt *stmt = nil;
     
    //1.准备sql语句
    NSString *sqlWord = @"select * from person";
     
    //2.预执行
    int result = sqlite3_prepare(db, sqlWord.UTF8String, -1, &stmt, NULL);
     
    //执行查询语句
    if (result == SQLITE_OK) {
        dataArray = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            Person *person = [Person new];
            //赋值顺序一定要按照字段顺序来匹配
            person.number = sqlite3_column_int64(stmt, 0);
            person.name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            person.gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
            person.phoneNumber = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            person.key = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
            person.QQNumber = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 5)];
            NSLog(@"_______%@",person.key);
            [dataArray addObject:person];
        }
    }
    //释放伴随指针
    sqlite3_finalize(stmt);
    NSLog(@"%@",_dbPath);
     
    return dataArray;
     
     
}
 
 
- (NSString *)dbPath
{
     
    if (_dbPath == nil) {
         
        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        _dbPath = [documentPath stringByAppendingPathComponent:@"person.sqlite"];
         
         
    }
    return _dbPath;
     
}
 
- (void)upDateName:(NSString *)name phoneNumber:(NSString *)phoneNumber key:(NSString *)key QQNumber:(NSString *)QQNumber gender:(NSString *)gender WithNumber:(NSInteger)number;
{
 
//    NSString *sqlWord = [NSString stringWithFormat:@"update person set name = '%@' gender = '%@' phoneNumber = '%@' key = '%@' QQNumber = '%@' WHERE number = '%ld'",name,gender,phoneNumber,key,QQNumber,number];
//    
//    int result = sqlite3_exec(db, sqlWord.UTF8String, NULL, NULL, NULL);
//    
//    if (result == SQLITE_OK) {
//        NSLog(@"修改数据成功");
//    }else{
//        
//        NSLog(@"修改数据失败,错误代码为%d",result);
//    }
     
    NSString *upDataString = @"update person set name = ? gender = ? phoneNumber = ? key = ? QQNumber = ? where number = ?";
    sqlite3_stmt *stmt = nil;
     
    int result = sqlite3_prepare(db, upDataString.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
         
        sqlite3_bind_int64(stmt, 1, number);
        sqlite3_bind_text(stmt, 2, name.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 3, gender.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 4, phoneNumber.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 5, key.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 6, QQNumber.UTF8String, -1, NULL);
         
        if (sqlite3_step(stmt) == SQLITE_DONE) {
             
            NSLog(@"修改成功");
        }else
        {
         
            NSLog(@"修改失败");
         
        }
         
    }else
    {
     
        NSLog(@"错误代码----%d",result);
     
    }
     
    sqlite3_finalize(stmt);
     
 
 
}

特别说明:
☉如果服务器暂不能下载请稍后重试!总是不能下载,请联系客服QQ:597260440,谢谢合作!
☉本站所有源码仅供学习与参考,请勿用于商业用途。
模板说明:
源码-用了sqlite数据库的通讯录,其版权归原作者所有,如损害你的权益请联系客服QQ:597260440给予处理。

本文永久链接:http://code.fangbianle.com/goods/show-5192.html

相关源码

在线客服
热线电话

备案号:豫ICP备14017793号-2编辑热线:0371-56691758

Copyight@2014-2018 All Rights Reserved 页面加载耗时0.642秒