IOS_Sqlite

举报
清雨小竹 发表于 2022/09/25 00:53:30 2022/09/25
【摘要】 #import "sqlite3.h" @interface CSqlite : NSObject{ sqlite3 *database;} -(void)openSqlite; -(sqlite3_stmt*)runSql:(char*)sql; -(sqlite3_stmt*)NSRunSql:(NSString*)sql;...

  
  1. #import "sqlite3.h"
  2. @interface CSqlite : NSObject
  3. {
  4. sqlite3 *database;
  5. }
  6. -(void)openSqlite;
  7. -(sqlite3_stmt*)runSql:(char*)sql;
  8. -(sqlite3_stmt*)NSRunSql:(NSString*)sql;
  9. -(BOOL)NSSendSql:(NSString*)sql;
  10. @end






  
  1. //
  2. // CSqlite.m
  3. // WXS
  4. //
  5. // Created by zili zhu on 12-7-13.
  6. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "CSqlite.h"
  9. @implementation CSqlite
  10. -(void)openSqlite
  11. {
  12. NSString *sqlFile = @"qxd.db";
  13. NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  14. NSString *cacheDir = [cachePath objectAtIndex:0];
  15. NSString *databasePath = [cacheDir stringByAppendingPathComponent:sqlFile];
  16. NSFileManager *fileManager = [NSFileManager defaultManager];
  17. // Copy the database sql file from the resourcepath to the documentpath
  18. if (![fileManager fileExistsAtPath:databasePath]) {
  19. NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:sqlFile];
  20. NSError *error;
  21. [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error];
  22. // if (error != nil) {
  23. // NSLog(@"[Database:Error] %@", error);
  24. // }
  25. }
  26. if(sqlite3_open([databasePath cStringUsingEncoding:NSASCIIStringEncoding], &database)==SQLITE_OK)
  27. {
  28. NSLog(@"open sqlite db ok.");
  29. }
  30. // if (sqlite3_open([[[[NSBundle mainBundle] pathForResource:@"qxd" ofType:@"db"] retain] fileSystemRepresentation], &database)==SQLITE_OK) {
  31. // NSLog(@"open sqlite db ok.");
  32. // }
  33. }
  34. -(void)closeSqlite
  35. {
  36. sqlite3_close(database);
  37. }
  38. -(sqlite3_stmt*)runSql:(char*)sql
  39. {
  40. // char *errorMsg;
  41. sqlite3_stmt *statement;
  42. if (sqlite3_prepare_v2(database, sql, -1, &statement, nil)==SQLITE_OK) {
  43. NSLog(@"select ok");
  44. }
  45. return statement;
  46. }
  47. -(sqlite3_stmt*)NSRunSql:(NSString*)sql
  48. {
  49. // char *errorMsg;
  50. sqlite3_stmt *statement;
  51. if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
  52. NSLog(@"select ok 2");
  53. }
  54. else {
  55. NSLog(@"select error 2");
  56. }
  57. return statement;
  58. }
  59. -(BOOL)NSSendSql:(NSString*)sql
  60. {
  61. char *errorMsg;
  62. if (sqlite3_exec(database, [sql UTF8String], 0, 0, &errorMsg)==SQLITE_OK)
  63. {
  64. NSLog(@"send ok");
  65. return YES;
  66. }
  67. else
  68. {
  69. fprintf(stderr,"Error: %s", errorMsg);
  70. return NO;
  71. }
  72. }
  73. @end



  
  1. while (sqlite3_step(stmt)==SQLITE_ROW)
  2. {
  3. str_MemberDetail *node = new str_MemberDetail;
  4. node->Id = sqlite3_column_int(stmt, 0);
  5. node->Name =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
  6. node->Phone =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
  7. node->Photos =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 3) encoding:NSUTF8StringEncoding];
  8. node->EnName =[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 4) encoding:NSUTF8StringEncoding];
  9. memberList->push_back(*node);
  10. }


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8294399

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。