很简单,不是么?至于插入,更新,删除示例,请参考如下 sqlCmd:
- // insert
- NSString * sqlCmd = [NSString stringWithFormat:@"insert into customer (name,address,age) values ('%@','%@',%d)"sqlCmd = [NSString stringWithFormat:@"update customer set address='%@',age=%d where name='%@'"sqlCmd = [NSString stringWithFormat:@"delete from customer where name='%@'"
-
-
- 查询操作
-
查询操作稍微负责一点,需要创建查询描述(sqlite3_stmt),然后调用如下接口编译成字节程序:
sqlITE_API const void *sqlite3_column_blob(sqlite3_stmt*,int iCol);
sqlITE_API int sqlite3_column_bytes(sqlite3_stmt*,0)"> iCol);
sqlITE_API int sqlite3_column_bytes16(sqlite3_stmt*,0)"> iCol);
sqlITE_API double sqlite3_column_double(sqlite3_stmt*,0)"> iCol);
sqlITE_API int sqlite3_column_int(sqlite3_stmt*,0)"> iCol);
sqlITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*,0)"> iCol);
sqlITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*,0)"> iCol);
sqlITE_API const void *sqlite3_column_text16(sqlite3_stmt*,0)"> iCol);
sqlITE_API int sqlite3_column_type(sqlite3_stmt*,0)"> iCol);
sqlITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*,int iCol);
- (NSArray *)queryAllCustomers
NSMutableArray * array = [[NSMutableArray alloc] init];
const char * sqlCmd = "select name,age from customer";
sqlite3_stmt * statement;
int state = sqlite3_prepare_v2(database,-1,0)">statement,nil);
if (state == sqlITE_OK) {
DLOG(@" >> Succeed to prepare statement. %@"sqlCmd encoding:NSUTF8StringEncoding]);
while (sqlite3_step(statement) == sqlITE_ROW) {
// get raw data from statement
char * cstrName = (char *)sqlite3_column_text(statement,0);
char * cstrAddress = (char *)sqlite3_column_text(statement,1);
int age = sqlite3_column_int(statement,2);
NSString * name = [NSString stringWithCString:cstrName encoding:NSUTF8StringEncoding];
NSString * address = [NSString stringWithCString:cstrAddress encoding:NSUTF8StringEncoding];
KSCustomer * customer = [[KSCustomer alloc]
[array addObject:customer];
DLOG(@" >> Record %d : %@ %@ %d",index++sqlite3_finalize(statement);
DLOG(@" >> Query %d records." array;
三,MAC 下查看 sqlite db 文件的工具
MAC 下有一款不错的开源可视化 sqlite db 浏览器:sqlite Database Browser,你可以从以下链接获取:
在 iOS 中直接使用 sqlite 原生 C 接口还是不那么方便,因此催生了第三方的 iOS 版封装库,其中使用比较广泛又轻量级的就是 FMDB(https://github.com/ccgus/fmdb),目前该库只有六个文件,不超过2000行代码。
#import "FMDatabaseAdditions.h"
NSString * path = [UIHUtilities configPathFor:kDatabaseFile];
FMDatabase db = [[FMDatabase databaseWithPath:path] retain];
DLog(@" >> Error: Failed to open database at %@"
db.traceExecution = TRUE;
[db executeUpdate:@"CREATE TABLE Image (studyUid text,patientId text,seriesUid text,SOPUid text,contentDate text,modality text,patientPosition text,filepath text,thumbnailPath text)"];
BOOL retValue = [db executeUpdate:@"INSERT INTO Image (studyUid,patientId,seriesUid,SOPUid,contentDate,patientPosition,modality,filepath,thumbnailPath) VALUES (?,?,?)"retValue)
DLog(@" >> Error: Database Failed to insert image %@" ([rs next])
FMResultSet *rs = [db executeQuery:@"SELECT COUNT(*) FROM Image WHERE seriesUid = ?" ([rs next]) {
count = [rs intForColumnIndex:0];
retValue = [db executeUpdate:@"DELETE FROM Image WHERE seriesUid = ?"retValue)
DLog(@" >> Error: Database Failed to delete image by seriesUid %@"
-
-
-