码迷,mamicode.com
首页 > 数据库 > 详细

SQLite

时间:2015-08-03 22:25:58      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

#import "DataManger.h"
#import <sqlite3.h>
#import "Mdoel.h"

static DataManger *dataManfer;
@implementation DataManger

- (instancetype)init
{
    self = [super init];
    if (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
           
        });
    }
    return self;
}

+(DataManger *)defaultDataManger
{
    if (dataManfer) return dataManfer;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataManfer = [[DataManger alloc]init];
    });
    return dataManfer;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (dataManfer) return dataManfer;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       dataManfer = [super allocWithZone:zone];
    });
    return dataManfer;
}


#pragma mark
static sqlite3 *db;
-(void)openDataBase
{
    if (db) return;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        path = [path stringByAppendingPathComponent:@"data.sqlite"];
        int result = sqlite3_open([path UTF8String], &db);
        if (result != SQLITE_OK) {
            NSLog(@"打开失败");
        }else{
            NSLog(@"打开成功");
        }
    });
}

#pragma mark 关闭数据库
-(void)closeDataBase
{
    int result = sqlite3_close(db);
    if (result == SQLITE_OK) {
        NSLog(@"数据关闭成功");
    }else{
        NSLog(@"关闭失败");
    }
       
}

#pragma mark 创建表格
-(void)createTableWithTableName:(NSString *)tableName
{
     NSString *CTabStr = [NSString stringWithFormat:@"CREATE TABLE %@ (user_id INTEGER PRIMARY KEY, user_name TEXT NOT NULL, user_pwd TEXT, user_info BLOL)",tableName];
   int result = sqlite3_exec(db, [CTabStr UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表格成功");
    }else{
        NSLog(@"创建表格失败");
    }
   
}

#pragma mark 插入一条数据
-(void)insertDataIntoTableWithUseid:(NSInteger)user_id withUserName:(NSString *)name withUserPwd:(NSString *)pwd user_info:(NSData *)data  forTable:(NSString *)table
{
    NSString *sqlString = [NSString stringWithFormat:@"INSERT INTO %@ (user_id, user_name, user_pwd, user_info) VALUES(?, ?, ?, ?)",table];
    sqlite3_stmt *stmt = nil;
    int result =  sqlite3_prepare(db, [sqlString UTF8String], -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        //绑定字段
        sqlite3_bind_int(stmt, 1,(int)user_id);
        sqlite3_bind_text(stmt, 2, [name UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 3, [pwd UTF8String], -1, NULL);
        sqlite3_bind_blob(stmt, 4, [data bytes],(int)[data length], NULL);
       
        sqlite3_step(stmt);
       
        NSLog(@"添加成功");
    }else{
        NSLog(@"添加失败");
    }
    sqlite3_finalize(stmt);
}
#pragma mark 删除表格全部数据
-(void)deleteAllDataWithTableName:(NSString *)tableName
{
    NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM %@",tableName];
    int result = sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}
#pragma mark 删除单条数据根据ID
-(void)deleteDataWithTableName:(NSString *)tableName UserID:(NSInteger)user_id
{
    NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM %@ WHERE user_id = %ld",tableName,user_id];
    int result = sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"userID:%ld删除成功",user_id);
    }else{
        NSLog(@"userID:%ld删除失败",user_id);
    }
}

#pragma mark 修改某条数据
-(void)updateTableForUserID:(NSInteger)user_id name:(NSString *)name WithTableName:(NSString *)tableName
{
    NSString *sqlStr = [NSString stringWithFormat:@"UPDATE %@ SET user_name = ‘%@‘ WHERE user_id = %ld",tableName, name,user_id];
    int result = sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"userID:%ld修改成功",user_id);
    }else{
        NSLog(@"userID:%ld修改失败",user_id);
    }
}

#pragma mark 查询数据
-(void)searchDataFromTable:(NSString *)tableName
{
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT *FROM %@",tableName];
    sqlite3_stmt *stemp = nil;

    /**
     *准备状态
     *
     *  @param db       数据库名称
     *  @param sqlStr   查询语句
     *  @param nByte#>  sql 语句的长度  -1 description#>
     *  @param ppStmt#> 存储值的长度  description#>
     *  @param pzTail#> 结束标志(NULL) description#>
     *
     *  @return int
     */
      int result = sqlite3_prepare(db, [sqlStr UTF8String], -1, &stemp, NULL);
    if (result == SQLITE_OK) {
        //判断是否可以继续玩下读
        while (sqlite3_step(stemp) == SQLITE_ROW) {
            NSInteger userID = sqlite3_column_int(stemp, 0);
            const unsigned char *userName = sqlite3_column_text(stemp, 1);
            NSString  *user_name = [NSString stringWithUTF8String:(const char *)userName];
            const unsigned char *userPwd = sqlite3_column_text(stemp, 2);
            NSString *user_pwd = [NSString stringWithUTF8String:(const char *)userPwd];
           
            //拿出第三列的数据
            const void *bytes = sqlite3_column_blob(stemp, 3);
            int length = sqlite3_column_bytes(stemp, 3);
            NSData *data = [NSData dataWithBytes:bytes length:length];
            Mdoel * model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            NSLog(@"%ld %@ %@, %@,%@",userID, user_name,user_pwd,model.name, model.sex);
            NSLog(@"%@",NSHomeDirectory());
        }
    }
   
   
 
    //结束查询
    sqlite3_finalize(stemp);
}




#pragma mark 删除表格
-(void)dropTableName:(NSString *)tableName
{
   
}
@end

SQLite

标签:

原文地址:http://www.cnblogs.com/booksky/p/4700369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!