为毛要用CoreData,SQLite不是挺好吗?
如果你要用这些代码,请无比仔细测试。

欢迎各种拍砖、探讨!

你可以从这里下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#import <Foundation/Foundation.h>
typedef enum _db_op_result{
     
    SUCCESSED    = 0,
    FAILED       = 1,
    CREATE_TABLE_FAILED = 5,
    TRANSACTION_EXE_FAILED = 7,
    UPDATE_FAILED = 9,
    DELETE_FAILED = 10,
    NOT_ALL_DONE = 20
} SMPDB_OPERATION_RESULT;
typedef enum _db_op_type{
    UPDATE = 1,
    DELETE = 2,
    INSERT = 3,
} SMPDB_OPERTION_TYPE;
@protocol DbOperationDelegate <NSObject>
@optional
- (NSInteger)udpate;
- (NSInteger)insert;
- (NSInteger)delete;
- (NSArray *)select:(NSString *)condition;
- (NSString *)tableName;
- (NSString *)fieldsString;
- (NSString *)queryString;
@end
// SQLite Operation
#import <Foundation/Foundation.h>
#import "sqlite3.h"
#import "DbOperationDelegate.h"
@interface SMPDbUtil : NSObject{
     
    NSString *databasePath;
    sqlite3 *db;
}
@property(readonly)sqlite3 *db;
- (NSInteger)createDatabase:(NSString *)dbName;
- (NSInteger)insert:(id<DbOperationDelegate>)obj;
- (NSInteger)insertSet:(NSString *)tableName tableFields:(NSArray *)fields valueSets:(NSArray *)values;
- (sqlite3_stmt *)select:(id<DbOperationDelegate>)obj conditions:(NSString *)cons;
- (NSInteger)update:(id<DbOperationDelegate>)obj;
@end
//how to use
#import <UIKit/UIKit.h>
#import "DbOperationDelegate.h"
@class ProvinceInfo;
@interface CityInfo : NSObject<DbOperationDelegate>{
     
    NSInteger cityId;
    NSString *cityName;
     
    ProvinceInfo *province;
}
@property(assign)NSInteger cityId;
@property(copy)NSString *cityName;
@property(retain)ProvinceInfo *province;
- (NSInteger)udpate;
- (NSInteger)insert;
- (NSInteger)delete;
- (NSArray *)select;
- (NSString *)fieldsString;
- (NSString *)queryString;
@end
//.m
#import "CityInfo.h"
#import "ProvinceInfo.h"
#import "SMPDbUtil.h"
#define TABLE_NAME @"city"
@implementation CityInfo
@synthesize cityId, cityName, province;
- (NSString *)tableName{
    return @"city";
}
- (NSString *)fieldsString{
     
    return @" id, name, provinceId ";
}
- (NSString *)queryString{
     
    return [NSString stringWithFormat:@"%d, \"%@\", %d", self.cityId, self.cityName, ((self.province == nil? -1 : self.province.provinceId))];
}
- (NSInteger)udpate{
     
    SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init];
     
    NSInteger r = [dbUtil update:self];
     
    [dbUtil release];
     
    return r;
}
- (NSInteger)insert{
     
    SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init];
     
    NSInteger r = [dbUtil insert:self];
     
    [dbUtil release];
     
    return r;
}
//- (NSInteger)delete{
//    return 0;
//}
- (NSArray *)select:(NSString *)condition{
     
    SMPDbUtil *dbUtil = [[SMPDbUtil alloc]init];
     
    sqlite3_stmt *statement = [dbUtil select:self conditions:condition];
     
    if (statement == NULL) {
        sqlite3_close(dbUtil.db);
        return nil;
    }
     
    NSMutableArray *array = [[[NSMutableArray alloc]init]autorelease];
     
    while (sqlite3_step(statement) == SQLITE_ROW) {
        CityInfo *el = [[CityInfo alloc]init];
         
        NSInteger id = sqlite3_column_int(statement, 0);
        NSString *name = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
        NSInteger provinceId = sqlite3_column_int(statement, 2);
        ProvinceInfo *p = [[ProvinceInfo alloc]init];
        NSArray *proList = [p select:[NSString stringWithFormat:@"id = %d", provinceId]];
        if (proList != nil && proList.count > 0) {
            p = [proList objectAtIndex:0];
        }
        el.cityId = id;
        el.cityName = name;
        el.province = p;
         
        [array addObject:el];
         
        [el release];
    }
     
    sqlite3_close(dbUtil.db);
    [dbUtil release];
     
    return array;
}
@end

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2012/03/02/2377647.html,如需转载请自行联系原作者

Objective-C SQLiteHelper相关推荐

  1. Objective C内存管理之理解autorelease------面试题

    Objective C内存管理之理解autorelease Autorelease实际上只是把对release的调用延迟了,对于每一个Autorelease,系统只是把该Object放入了当前的Aut ...

  2. C# SQLiteHelper类似SqlHelper类实现存取Sqlite数据库

    这个类不是我实现的,原文在这里,我修改了原文中分析sql语句参数的方法,将方法名修改为AttachParameters,将其修饰符修改为private,并直接传递command到这个方法,直接绑定参数 ...

  3. iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象...

    1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...

  4. 【iOS 开发】Objective-C 运算符

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41624613 参考文章 : 1.[iOS 开发]Object-C 运算符 2.[iO ...

  5. 成功解决coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to inc

    成功解决coordinate_descent.py:491: ConvergenceWarning: Objective did not converge. You might want to inc ...

  6. SQLiteHelper

    本文定义了一个DBHelper类,是SQLite的.需要添加一个System.Data.SQLite的dll,可以到SQLite的官网下载.类的代码如下 1 using System; 2 using ...

  7. 刨根问底Objective-C Runtime(2)- Object Class Meta Class

    刨根问底Objective-C Runtime(2)- Object & Class & Meta Class Chun Tips 专注iOS开发 刨根问底Objective-C Ru ...

  8. Objective C 基础教程

    复合 使用复合可组合多个对象,使之人工协作. 会同时使用继和复合来创建自己的类. 编程中的复合就好像音乐中的作曲一样:将多个组件组合在一起配合使用. 在Objective-C中,复合是通过包含作为实例 ...

  9. 观点:再见Objective C?程序员眼中的Swift

    对于苹果开发者来说,如今已经进入了"Swift时代".虽然编程语言Objective C备受喜爱,不过它作为苹果主流编程语言的日子已经所剩无几.随着WWDC开发者大会的落幕,Swi ...

  10. Objective c类的初始化

    Objective c中实现类的初始化要先重载父类的init方法: 1.首先调用[super init]使用父类的方法进行初始化.将对象赋给self 2.假设self不为nil即父类初始化成功,接着进 ...

最新文章

  1. EffectKeyMap系列1(Ubuntu)
  2. python打开谷歌浏览器 关键字_高效使用Chrome浏览器
  3. rust(71)-for、while循环表达式
  4. 判断sem信号量为零_将信号量递减为零的进程崩溃时,如何恢复信号量?
  5. 前端学习(988):jquery常见的api
  6. dos命令行设置网络优先级_网络安全之木马病毒的防范以及攻击
  7. JAVA设计模式(08):结构化-飞锤(Flyweight)
  8. 向视图中插入的数据能进入到基本表中去吗?_数据库调优,调的是什么及常见手法...
  9. npm ERR! Cannot read property ‘resolve‘ of undefined
  10. [转]垂直搜索基础知识(1)
  11. ibm主机安装服务器系统安装系统安装系统安装方法,IBM系列服务器安装操作系统安装方法.ppt...
  12. Winserver AD管理Powershell——GUI 计算机加入域
  13. 3.单片机 数码管显示
  14. mac 终端 配置代理
  15. 台式计算机联网,台式电脑怎么联网宽带
  16. FreeRtos--队列
  17. 如何发挥公证在知识产权保护中的作用
  18. python如何升级_怎么升级python版本
  19. 小清丽微距花卉拍摄示范
  20. SpaceX火箭发射成功,一文了解所用的软件技术栈

热门文章

  1. poj1161Post Office【经典dp】
  2. 安卓旅途之——开发数独(一)
  3. 网络编程——第一部分
  4. openstack VM可以ping外部网络,但是外部网络ping不通VM
  5. xxx is not mapped 错误 解决方案
  6. GitHub上传文件的过滤规则 -- windows下
  7. C++_类和对象_C++运算符重载_赋值运算符重载_利用深拷贝实现对象深度赋值运算---C++语言工作笔记058
  8. kafka消费者脚本无法启动问题
  9. JAVA中iterator与add的顺序问题
  10. 动态修改路由_tutorial第二部分-路由参数