iPhone SQLite

1.工程引用SQLite

iPhone系统自带SQLite

首先工程中,引入头文件#import <sqlite3.h>libsqlite3.dylib LIB库。

2.创建数据库

现在要使用SQLite创建一个数据库文件( 路径:/Users/lvwei/Library/Application Support/iPhone Simulator/4.3.2/Applications/C6B78C3D-8DD5-47B9-B0EE-160B009EA485/Documents/myDbFile/dbFile.db;

注意文件名,不要把dbFile.db中的.db去掉,不然,给你创建个目录).

3.创建数据库表,并执行相关数据库操作

创建关于学生的表,有SVID(学生学号),name(姓名),sex(性别),age(年龄)属性,对数据库有增删查改的操作。特别注意SQL语句的写法.

  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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//  dbTest_sqliteViewController.m
//  dbTest_sqlite
//
//  Created by lv wei on 13-4-23.
//  Copyright (c) 2013年 kAir. All rights reserved.
//

#import "dbTest_sqliteViewController.h"
#import <sqlite3.h>

static NSString* const KDBFileName = @"dbFile.db";

@interface QStudent : NSObject

@property(nonatomic, retain)NSString* svid;
@property(nonatomic, retain)NSString* name;
@property(nonatomic, retain)NSString* sex;
@property(nonatomic, assign)NSUInteger age;

@end

@implementation QStudent
@synthesize svid = _svid;
@synthesize name = _name;
@synthesize sex = _sex;
@synthesize age = _age;

- (void)dealloc
{
    [_svid release];
    [_name release];
    [_sex release];
    [super dealloc];
}

@end

@interface dbTest_sqliteViewController ()
{
      sqlite3* _database;
}

@property(nonatomic, retain)NSString* dbFileFullPath;

@end

@implementation dbTest_sqliteViewController
@synthesize dbFileFullPath = _dbFileFullPath;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self dbFileCreate];
    [self dbFileOpen];
    [self createStudentTable];
    [self insertRecords1];
    [self showCurAllRecord];
    [self insertRecords2];
    [self showCurAllRecord];
    [self deleteRecordBySvid:@"030341311"];
    [self showCurAllRecord];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_dbFileFullPath release];
    if (_database) {
        sqlite3_close(_database);
        _database = NULL;
    }
    [super dealloc];
}

- (void)dbFileCreate
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    if ([paths count] > 0) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myDbFile"];
        
        NSFileManager* fm = [NSFileManager defaultManager];
        
        if (![fm fileExistsAtPath:path]) {
            [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
        self.dbFileFullPath = [path stringByAppendingFormat:@"/%@",KDBFileName];
        
        if (![fm fileExistsAtPath:_dbFileFullPath]) {
            [fm createFileAtPath:_dbFileFullPath contents:nil attributes:nil];
        }
        
        NSLog(@"filePath = %@",_dbFileFullPath);
    }
}

- (void)dbFileOpen
{
    int res = sqlite3_open([_dbFileFullPath UTF8String], &_database);
    
    if (res != SQLITE_OK) {
        sqlite3_close(_database);
        _database = NULL;
    }
}

- (BOOL) createStudentTable{
    char *sql = "CREATE TABLE  student(id integer primary key, \
    svid text,\
    name text, \
    sex text, \
    age integer)";
    char *errorMsg;
    int success = sqlite3_exec(_database, sql, NULL,NULL,&errorMsg);
    
    if (success == SQLITE_OK) {
        NSLog(@"create table student ok!");
        return YES;
    }
    else
    {
        NSLog(@"create table student fail!");
        return NO;
    }
}

//插入学生记录1
- (void)insertRecords1
{
    const int KArrayLen = 4;
    NSArray* svidList = [NSArray arrayWithObjects:@"030341318",@"030341311",@"030341309",@"030341120", nil];
    NSArray* nameList = [NSArray arrayWithObjects:@"ksnow", @"ksnowlv",@"kAir",@"ksnowk",nil];
    NSArray* sexList = [NSArray arrayWithObjects:@"女", @"男",@"男",@"女",nil];
    int ageList[KArrayLen] = {28,25,32,12};
    
    for (int i = 0; i < KArrayLen; ++i) {
        QStudent* student = [[QStudent alloc] init];
        student.svid = [svidList objectAtIndex:i];
        student.name = [nameList objectAtIndex:i];
        student.sex = [sexList objectAtIndex:i];
        student.age = ageList[i];
        
        if (![self findRecordBySVid:student.svid]) {
            [self createStudentRecord:student];  
        }
        else
        {
            [self updateRecord:student];
        }
        
        [student release];
    }
}

//插入学生记录2
- (void)insertRecords2
{
    const int KArrayLen = 4;
    NSArray* svidList = [NSArray arrayWithObjects:@"030341318",@"030341300",@"030341301",@"030341302", nil];
    NSArray* nameList = [NSArray arrayWithObjects:@"ksnowlv", @"学生1",@"学生2",@"学生3",nil];
    NSArray* sexList = [NSArray arrayWithObjects:@"男", @"男",@"男",@"女",nil];
    int ageList[KArrayLen] = {29,25,32,12};
    
    for (int i = 0; i < KArrayLen; ++i) {
        QStudent* student = [[QStudent alloc] init];
        student.svid = [svidList objectAtIndex:i];
        student.name = [nameList objectAtIndex:i];
        student.sex = [sexList objectAtIndex:i];
        student.age = ageList[i];
        
        if (![self findRecordBySVid:student.svid]) {
            [self createStudentRecord:student];
        }
        else
        {
            [self updateRecord:student];
        }
        
        [student release];
    }
}

//创建学生的记录
- (BOOL)createStudentRecord:(QStudent*)student{
   
    sqlite3_stmt *statement;
    static char *sql = "INSERT INTO student (svid,name,sex,age)\
    VALUES(?,?,?,?)";
    
    //问号的个数要和(svid,name,sex,age)里面字段的个数匹配,代表未知的值,将在下面将值和字段关联。
    int success = sqlite3_prepare_v2(_database, sql, -1, &statement, NULL);
    if (success != SQLITE_OK) {
        NSLog(@"Error: failed to insert:channels");
        return NO;
    }
    
    //这里的数字1,2,3,4代表第几个问号
    sqlite3_bind_text(statement, 1,student.svid.UTF8String, -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(statement, 2, student.name.UTF8String, -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(statement, 3, student.sex.UTF8String, -1, SQLITE_TRANSIENT);
    sqlite3_bind_int(statement, 4, student.age);
    
    success = sqlite3_step(statement);
    sqlite3_finalize(statement);
    
    if (success == SQLITE_ERROR) {
        NSLog(@"Error: failed to insert into the database with message.");
        return NO;
    }
    
    NSLog(@"insert student svid = %@, name = %@,sex = %@,age=%d",
          student.svid,student.name,student.sex,student.age);
    return YES;
}

//显示所有学生的记录
- (void)showCurAllRecord
{
    NSLog(@"start showAllRecord");
    
    NSString* sqlString = [NSString stringWithFormat:@"SELECT * FROM student"];
    
    sqlite3_stmt *statement = nil;
    
    int res = sqlite3_prepare_v2(_database, [sqlString UTF8String], -1, &statement, NULL);

    if ( res != SQLITE_OK) {
        NSLog(@"Error: failed to prepare statement with message:get student.");
    }
    
    //查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。
    while (sqlite3_step(statement) == SQLITE_ROW) {
        char* svid       = (char*)sqlite3_column_text(statement, 1);
        char* name     = (char*)sqlite3_column_text(statement, 2);
        char* sex     = (char*)sqlite3_column_text(statement, 3);
        int age    = sqlite3_column_int(statement, 4);
        NSString* studentSvid = [[NSString alloc] initWithCString:svid encoding:NSUTF8StringEncoding];
        NSString* studentName = [[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];
        NSString* studentSex = [[NSString alloc] initWithCString:sex encoding:NSUTF8StringEncoding];
        NSLog(@"student svid = %@, name = %@,sex = %@,age=%d",
              studentSvid,studentName,studentSex,age);
        [studentSvid release];
        [studentName release];
        [studentSex release];
    }
    sqlite3_finalize(statement);
    
     NSLog(@"complete showAllRecord");
}

//查询是否存在该SVID的记录?
- (BOOL) findRecordBySVid:(NSString*)svid{
        
    NSString* sqlString = [NSString stringWithFormat:@"SELECT * FROM student where svid == \"%@\";",svid];
    
    sqlite3_stmt *statement = nil;
 
    int res = sqlite3_prepare_v2(_database, [sqlString UTF8String], -1, &statement, NULL);
    if ( res != SQLITE_OK) {
        NSLog(@"Error: failed to prepare statement with message:get student.");
    }
    
    BOOL isExistRecord = NO;
    //查询结果集中一条一条的遍历所有的记录,这里的数字对应的是列值。
    while (YES) {
        res = sqlite3_step(statement);
        if (res == SQLITE_ROW) {
            char* svid       = (char*)sqlite3_column_text(statement, 1);
            char* name     = (char*)sqlite3_column_text(statement, 2);
            char* sex     = (char*)sqlite3_column_text(statement, 3);
            int age    = sqlite3_column_int(statement, 4);
            NSString* studentSvid = [[NSString alloc] initWithCString:svid encoding:NSUTF8StringEncoding];
            NSString* studentName = [[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];
            NSString* studentSex = [[NSString alloc] initWithCString:sex encoding:NSUTF8StringEncoding];
            NSLog(@"query student svid = %@, name = %@,sex = %@,age=%d",
                  studentSvid,studentName,studentSex,age);
            [studentSvid release];
            [studentName release];
            [studentSex release];
            
            isExistRecord = YES;
            break;
        }
        else
        {
            break;
        }
    }
    sqlite3_finalize(statement);
    
    return isExistRecord;
}

//在查询存在该SVID的记录时,更新记录
- (BOOL)updateRecord:(QStudent*)student
{
    NSString*   updateSQLString =[NSString stringWithFormat:@"update student set name= \"%@\", sex=\"%@\",age=%d where svid=\"%@\"",student.name,student.sex,student.age,student.svid];
    
    char *errMsg = NULL;
    int res = sqlite3_exec(_database, [updateSQLString UTF8String], NULL, NULL, &errMsg);
    
    if(res ==SQLITE_OK){
        
        NSLog(@"更新成功!");
        return YES;
    }
    else
    {
        NSLog(@"更新失败!");
        return NO;
    }
}

//在查询存在该SVID的记录时,更新记录
- (BOOL)deleteRecordBySvid:(NSString*)svid
{
    NSString*   updateSQLString =[NSString stringWithFormat:@"delete from student where svid=\"%@\"",svid];
    
    char *errMsg = NULL;
    int res = sqlite3_exec(_database, [updateSQLString UTF8String], NULL, NULL, &errMsg);
    
    if(res ==SQLITE_OK){
        
        NSLog(@"删除成功!");
        return YES;
    }
    else
    {
        NSLog(@"删除失败!");
        return NO;
    }
}

@end

4.执行情况

 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
	2013-04-24 17:53:49.170 dbTest_sqlite[26700:c303] filePath = /Users/lvwei/Library/Application Support/iPhone Simulator/4.3.2/Applications/C6B78C3D-8DD5-47B9-B0EE-160B009EA485/Documents/myDbFile/dbFile.db
	2013-04-24 17:53:49.222 dbTest_sqlite[26700:c303] create table student ok!
	2013-04-24 17:53:49.228 dbTest_sqlite[26700:c303] insert student svid = 030341318, name = ksnow,sex = 女,age=28
	2013-04-24 17:53:49.251 dbTest_sqlite[26700:c303] insert student svid = 030341311, name = ksnowlv,sex = 男,age=25
	2013-04-24 17:53:49.253 dbTest_sqlite[26700:c303] insert student svid = 030341309, name = kAir,sex = 男,age=32
	2013-04-24 17:53:49.258 dbTest_sqlite[26700:c303] insert student svid = 030341120, name = ksnowk,sex = 女,age=12
	2013-04-24 17:53:49.258 dbTest_sqlite[26700:c303] start showAllRecord
	2013-04-24 17:53:49.259 dbTest_sqlite[26700:c303] student svid = 030341318, name = ksnow,sex = 女,age=28
	2013-04-24 17:53:49.259 dbTest_sqlite[26700:c303] student svid = 030341311, name = ksnowlv,sex = 男,age=25
	2013-04-24 17:53:49.259 dbTest_sqlite[26700:c303] student svid = 030341309, name = kAir,sex = 男,age=32
	2013-04-24 17:53:49.260 dbTest_sqlite[26700:c303] student svid = 030341120, name = ksnowk,sex = 女,age=12
	2013-04-24 17:53:49.260 dbTest_sqlite[26700:c303] complete showAllRecord
	2013-04-24 17:53:49.261 dbTest_sqlite[26700:c303] query student svid = 030341318, name = ksnow,sex = 女,age=28
	2013-04-24 17:53:49.333 dbTest_sqlite[26700:c303] 更新成功!
	2013-04-24 17:53:49.336 dbTest_sqlite[26700:c303] insert student svid = 030341300, name = 学生1,sex = 男,age=25
	2013-04-24 17:53:49.338 dbTest_sqlite[26700:c303] insert student svid = 030341301, name = 学生2,sex = 男,age=32
	2013-04-24 17:53:49.341 dbTest_sqlite[26700:c303] insert student svid = 030341302, name = 学生3,sex = 女,age=12
	2013-04-24 17:53:49.342 dbTest_sqlite[26700:c303] start showAllRecord
	2013-04-24 17:53:49.342 dbTest_sqlite[26700:c303] student svid = 030341318, name = ksnowlv,sex = 男,age=29
	2013-04-24 17:53:49.342 dbTest_sqlite[26700:c303] student svid = 030341311, name = ksnowlv,sex = 男,age=25
	2013-04-24 17:53:49.343 dbTest_sqlite[26700:c303] student svid = 030341309, name = kAir,sex = 男,age=32
	2013-04-24 17:53:49.343 dbTest_sqlite[26700:c303] student svid = 030341120, name = ksnowk,sex = 女,age=12
	2013-04-24 17:53:49.344 dbTest_sqlite[26700:c303] student svid = 030341300, name = 学生1,sex = 男,age=25
	2013-04-24 17:53:49.344 dbTest_sqlite[26700:c303] student svid = 030341301, name = 学生2,sex = 男,age=32
	2013-04-24 17:53:49.366 dbTest_sqlite[26700:c303] student svid = 030341302, name = 学生3,sex = 女,age=12