Sqlit3 简单操作

1。创建数据库并且打开

- (void)open_db

{

    @synchronized(self){

        NSArray *pathArray =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);

        

        NSString *path = [[pathArrayobjectAtIndex:0]stringByAppendingPathComponent:@"Amt.db"];

        

        if (sqlite3_open([pathUTF8String], &database) != SQLITE_OK) {

            

            sqlite3_close(database);

            

            NSLog(@"创建数据库失败");

        }

    }  

}


2。关闭数据库

sqlite3_close(database);

3。创建表

- (void)create_table:(NSString*)tableName

{

    tableDBName = tableName;

    

    [[NSUserDefaults standardUserDefaults] setObject:tableNameforKey:TABLENAME];

    

    char *errMsg;

    

    NSString *createTableSql = [NSStringstringWithFormat:

                                @"create table if not exists 'Amt_%@'(\

                                row integer primay key ,\

                                msgid,\

                                msgtitle,\

                                msgcontent,\

                                channelid,\

                                channelname,\

                                channeltype)",tableDBName];

    

    if (sqlite3_exec(database, [createTableSqlUTF8String], NULL, NULL, &errMsg)!= SQLITE_OK) {

        

        NSLog(@"创建表失败");

        

        sqlite3_free(errMsg);

        

        [self close];

    }  

}

4。添加表数据

- (void)addMesg:(MsgInfo*)msgInfo

{

    char *errMsg;

    

    tableDBName = [[NSUserDefaultsstandardUserDefaults] objectForKey:TABLENAME];

    

    NSString *addSql = [NSStringstringWithFormat:@"insert or replace into 'Amt_%@'(msgid,msgtitle,msgcontent,channelid,channelname,channeltype) values('%@','%@','%@','%@','%@','%@'); ",tableDBName,msgInfo.msgid,msgInfo.msgtitle,msgInfo.msgcontent,msgInfo.channelid,msgInfo.channelname,msgInfo.channeltype];

    

    if (sqlite3_exec(database, [addSqlUTF8String], NULL, NULL, &errMsg)!= SQLITE_OK) {

        

        NSLog(@"插入数据失败");

        

        sqlite3_free(errMsg);

        

        [self close];

    }   

}

5。筛选数据

- (NSMutableArray *)getChannelIDInfo :(NSString *)channleIDInfo

{

     NSMutableArray *msgArray = [[NSMutableArrayalloc] init];

    

    MsgInfo *msgInfo = [[MsgInfoalloc] init];

    

    tableDBName = [[NSUserDefaultsstandardUserDefaults] objectForKey:TABLENAME];

    

    sqlite3_stmt *statement;

    

    NSString *selSql = [NSStringstringWithFormat:@"select * from 'Amt_%@' where channelid = '%@'",tableDBName,channleIDInfo];

    if (sqlite3_prepare_v2(database, [selSqlUTF8String], -1, &statement, nil) == SQLITE_OK) {

        while (sqlite3_step(statement) ==SQLITE_ROW) {

                //通过循环检索所有结果

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

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

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

            char * channelid  = (char *)sqlite3_column_text(statement,4);

            char * channelname = (char *)sqlite3_column_text(statement,5);

            char * channeltype = (char *)sqlite3_column_text(statement,6);

            

            NSString *msgidStr = [NSStringstringWithCString:msgid encoding:NSUTF8StringEncoding];

            NSString *msgtitleStr = [NSStringstringWithCString:msgtitle encoding:NSUTF8StringEncoding];

            NSString *msgcontentStr = [NSStringstringWithCString:msgcontent encoding:NSUTF8StringEncoding];

            NSString *channelidStr  = [NSStringstringWithCString:channelid encoding:NSUTF8StringEncoding];

            NSString *channelnameStr = [NSStringstringWithCString:channelname encoding:NSUTF8StringEncoding];

            NSString *channeltypeStr    = [NSStringstringWithCString:channeltype encoding:NSUTF8StringEncoding];

            

            msgInfo.msgid  =  msgidStr;

            msgInfo.msgtitle = msgtitleStr;

            msgInfo.msgcontent = msgcontentStr;

            msgInfo.channelid  = channelidStr;

            msgInfo.channelname = channelnameStr;

            msgInfo.channeltype = channeltypeStr;

            [msgArray addObject:msgInfo];

        }    

        

    }

    sqlite3_finalize(statement);

    return [msgArray autorelease];

}

6. 删除数据

- (void)delOneMsg:(NSString *)channleIDInfo

{

    char *errMsg;

    

    tableDBName = [[NSUserDefaults standardUserDefaults] objectForKey:TABLENAME];

    

    NSString *delSql = [NSString stringWithFormat:@"delete from 'Amt_%@' where channelid = '%@'",tableDBName,channleIDInfo];

    

    

    if (sqlite3_exec(database, [delSql UTF8String],NULL , NULL, &errMsg)!= SQLITE_OK) {

        

        NSLog(@"删除失败");

        sqlite3_free(errMsg);

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值