IOS_Sqlite
【摘要】
#import "sqlite3.h" @interface CSqlite : NSObject{ sqlite3 *database;} -(void)openSqlite; -(sqlite3_stmt*)runSql:(char*)sql; -(sqlite3_stmt*)NSRunSql:(NSString*)sql;...
#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);
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/8294399
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)