IOS_Sqlite 淩亂°似流年 2023-10-18 09:29 139阅读 0赞 #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); }
还没有评论,来说两句吧...