惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - 威风剑

创建按钮的两种方法 jquery自定义事件 操作div - 威风剑 - 博客园 jQuery基本操作篇 jquery操作select时怎么产生事件 - 威风剑 - 博客园 jquery操作radio/checkbox/select 及其相关 - 威风剑 - 博客园 操作下拉框 - 威风剑 - 博客园 jquery操作select下拉列表框 - 威风剑 - 博客园 jqury操作表格 - 威风剑 - 博客园 .NET SESSION的使用及原理 Ilist转换成dataset (转) DateTime.Now.ToString("yyyyMMddHHmmssfff") WebMethod 特性的 EnableSession Thread.Sleep(0) 【转】DataGridView新特色(vs2005) showModalDialog和showModelessDialog使用心得 Transact SQL 语 句 功 能 SQL语句 SQL语句 - 威风剑
sqllite数据库操作帮助类
威风剑 · 2011-10-06 · via 博客园 - 威风剑

1、基于框架FMDatabase。

2、连接类

#import <Foundation/Foundation.h>
#import "FMDatabase.h"

@interface DbHelper : NSObject {
    FMDatabase *db;
}
- (BOOL)initDatabase;
- (void)closeDatabase;
- (FMDatabase *)getDatabase;

@end

#import "DbHelper.h"
#define DB_NAME @"menu.db"

@implementation DbHelper

- (BOOL)initDatabase
{
    BOOL success;
    NSError *error;
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DB_NAME];
    
    success = [fm fileExistsAtPath:writableDBPath];
    
    if(!success){
        NSString *defaultDBPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:DB_NAME];
        NSLog(@"%@",defaultDBPath);
        success = [fm copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if(!success){
            NSLog(@"error: %@", [error localizedDescription]);
        }
        success = YES;
        NSLog(@"Success to open database.");
    }
    
    if(success){
        db = [[FMDatabase databaseWithPath:writableDBPath] retain];
        if ([db open]) {
            [db setShouldCacheStatements:YES];
        }else{
            NSLog(@"Failed to open database.");
            success = NO;
        }
    }
    
    return success;
}

- (void)closeDatabase
{
    [db close];
}

- (FMDatabase *)getDatabase
{
    if ([self initDatabase]){
        return db;
    }    
    return NULL;
}

- (void)dealloc
{
    [self closeDatabase];
    [db release];
    [super dealloc];
}
@end

3、调用

-(BOOL) UpdataData:(NSArray *)list
{

DbHelper *dbhelper=[[

DbHelper alloc] init];
    FMDatabase *db=[dbhelper getDatabase];
    [db beginTransaction];
    
    [db executeUpdate: @"DELETE FROM DishType"];
    if ([db hadError]) {
       NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
       [db rocback];
    }
    for(id model in list)
    {
        [self insertWithModel:model db:db];
    }
  [db commit];
  [dbhelper release];
} 

-(BOOL) insertWithModel:(DishTypeModel *) model db:(FMDatabase *) db

{

[db executeUpdate: @"INSERT INTO DishType(ID,Name,Img,Sequence) VALUES(?,?,?,?)",

model.ID,model.Name,model.Img,model.Sequence];

if ([db hadError]) {

      NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);

  [db rockback];

  return false;

    }

return true;

}