IOS_Sqlite

简介: IOS_Sqlite
#import "sqlite3.h"
@interface CSqlite  : NSObject
{
    sqlite3 *database;
}
-(void)openSqlite;
-(sqlite3_stmt*)runSql:(char*)sql;
-(sqlite3_stmt*)NSRunSql:(NSString*)sql;
-(BOOL)NSSendSql:(NSString*)sql;
@end
//
//  CSqlite.m
//  WXS
//
//  Created by zili zhu on 12-7-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "CSqlite.h"
@implementation CSqlite
-(void)openSqlite
{
    NSString *sqlFile = @"qxd.db";
    NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDir = [cachePath objectAtIndex:0];
    NSString *databasePath = [cacheDir stringByAppendingPathComponent:sqlFile];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Copy the database sql file from the resourcepath to the documentpath
    if (![fileManager fileExistsAtPath:databasePath]) {
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:sqlFile];
        NSError *error;
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error];
//        if (error != nil) {
//            NSLog(@"[Database:Error] %@", error);
//        }
    }
    if(sqlite3_open([databasePath cStringUsingEncoding:NSASCIIStringEncoding], &database)==SQLITE_OK)
    {
         NSLog(@"open sqlite db ok.");
    }
//    if (sqlite3_open([[[[NSBundle mainBundle] pathForResource:@"qxd" ofType:@"db"] retain] fileSystemRepresentation], &database)==SQLITE_OK) { 
//        NSLog(@"open sqlite db ok."); 
//    }
}
-(void)closeSqlite
{
    sqlite3_close(database);
}
-(sqlite3_stmt*)runSql:(char*)sql
{
   // char *errorMsg;
    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(database, sql, -1, &statement, nil)==SQLITE_OK) { 
        NSLog(@"select ok"); 
    }
    return statement;
}
-(sqlite3_stmt*)NSRunSql:(NSString*)sql
{
   // char *errorMsg;
    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) { 
        NSLog(@"select ok 2"); 
    }
    else {
        NSLog(@"select error 2");
    }
    return statement;
}
-(BOOL)NSSendSql:(NSString*)sql
{
    char *errorMsg;
    if (sqlite3_exec(database, [sql UTF8String], 0, 0, &errorMsg)==SQLITE_OK)
    { 
        NSLog(@"send ok");
        return YES;    
    }
    else 
    {
        fprintf(stderr,"Error:  %s",  errorMsg);
        return NO;
    }
}
@end
 while (sqlite3_step(stmt)==SQLITE_ROW)
    {
        str_MemberDetail *node = new str_MemberDetail;
        node->Id = sqlite3_column_int(stmt, 0);
        node->Name =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
        node->Phone =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
        node->Photos =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 3) encoding:NSUTF8StringEncoding];
        node->EnName =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 4) encoding:NSUTF8StringEncoding];
        memberList->push_back(*node);
    }
相关文章
|
Swift iOS开发
iOS @available 和 #available 的用法
iOS @available 和 #available 的用法
439 0
|
8月前
|
安全 Linux Shell
IOS安装iSH
IOS安装iSH
304 0
IOS安装iSH
|
编译器 iOS开发
iOS respondsToSelector方法
iOS respondsToSelector方法
179 0
|
数据安全/隐私保护 iOS开发
iOS开发-Xcode8兼容iOS7手记
iOS开发-Xcode8兼容iOS7手记
115 0
iOS开发-Xcode8兼容iOS7手记
|
SQL 存储 数据库
iOS Sqlite数据库的使用
iOS Sqlite数据库的使用
185 0
iOS Sqlite数据库的使用
|
iOS开发
iOS UILabe及UIFont用法总结
iOS UILabe及UIFont用法总结
217 0
|
SQL 数据库 数据库管理
|
iOS开发