首先你在用之前要在项目中加入libsqlite3.dylib

1、定义模型

[cpp] #import <Foundation/Foundation.h>  

#import "sqlite3.h"  

@class NotePad; 

@class NoteDb; 

@interface NoteSqlite : NSObject{ 

    sqlite3 *database; 

    sqlite3_stmt *statement; 

    char *errorMsg; 



//打开数据库  

-(BOOL)open; 

//创建青  

-(BOOL)create; 

 

//增加、删除、修改、查询  

-(BOOL)insert:(NotePad*)aNote; 

-(BOOL)deleteALLNote; 

-(BOOL)deleteaNote:(NotePad*)aNote; 

-(BOOL)update:(NotePad*)aNote; 

 

-(NoteDb*)selecteAll; 

-(NoteDb*)selectNotes:(NotePad*)aNote; 

 

@end 

#import <Foundation/Foundation.h>

#import "sqlite3.h"

@class NotePad;

@class NoteDb;

@interface NoteSqlite : NSObject{

    sqlite3 *database;

    sqlite3_stmt *statement;

    char *errorMsg;

}

//打开数据库

-(BOOL)open;

//创建青

-(BOOL)create;

//增加、删除、修改、查询

-(BOOL)insert:(NotePad*)aNote;

-(BOOL)deleteALLNote;

-(BOOL)deleteaNote:(NotePad*)aNote;

-(BOOL)update:(NotePad*)aNote;

-(NoteDb*)selecteAll;

-(NoteDb*)selectNotes:(NotePad*)aNote;

@end

2、实现方法

[cpp] #import "NoteSqlite.h"  

#import "NotePad.h"  

#import "NoteDb.h"  

@implementation NoteSqlite 

 

-(id)init{ 

    self=[super init]; 

    return self; 



//打开数据库  

-(BOOL)open{ 

    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"noteList.db"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    BOOL find = [fileManager fileExistsAtPath:path]; 

    //判断文件是否存在  

    if (find) { 

        NSLog(@"数据库文件已经存在"); 

        //打开数据库、返回操作是否正确  

        if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

            NSLog(@"打开成功数据库"); 

        } 

        return YES; 

    }else{ 

        if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

            //调用createMusicList创建数据库和表  

            [self create];              

            return YES; 

        } else { 

            sqlite3_close(database); 

            NSLog(@"Error: open database file."); 

            return NO; 

        } 

        return NO; 

    } 

 



//创建表  

-(BOOL)create{ 

    //创建表语句  

    const char *createSql="create table if not exists note (id integer primary key autoincrement,theme text,information text,ndate text,priority integer)";  

    //创建表是否成功  

    if (sqlite3_exec(database, createSql, NULL, NULL, &errorMsg)==SQLITE_OK) {        

        NSLog(@"create ok.");    

        return YES; 

    }else{ 

        //打印出错信息  

        NSLog(@"error: %s",errorMsg);  

        sqlite3_free(errorMsg); 

    } 

    return NO; 

 



 

//增加、删除、修改、查询  

-(BOOL)insert:(NotePad*)aNote{ 

    //向表中插入记录    

    //定义一个sql语句          

    NSString *insertStatementNS = [NSString stringWithFormat: 

                                   @"insert into \"note\"\ 

                                   (theme, information, ndate,priority)\ 

                                   values (\"%@\", \"%@\", \"%@\",%d)", 

                                   aNote.theme,aNote.information,[NSString stringWithFormat:@"%@",aNote.ndate],aNote.priority 

                                    ]; 

    //将定义的NSString的sql语句,转换成UTF8的c风格的字符串  

    const char *insertSql = [insertStatementNS UTF8String]; 

    //执行插入语句   

    if (sqlite3_exec(database, insertSql, NULL, NULL, &errorMsg)==SQLITE_OK) {   

        NSLog(@"insert ok.");  

        return YES; 

    }else{ 

        NSLog(@"error: %s",errorMsg);  

        sqlite3_free(errorMsg); 

    }  

    return NO; 

 



-(BOOL)deleteALLNote{ 

    //删除所有数据,条件为1>0永真  

    const char *deleteAllSql="delete from note where 1>0"; 

    //执行删除语句  

    if(sqlite3_exec(database, deleteAllSql, NULL, NULL, &errorMsg)==SQLITE_OK){ 

        NSLog(@"删除所有数据成功"); 

    } 

    return YES; 



-(BOOL)deleteaNote:(NotePad*)aNote{ 

    //删除某条数据  

    NSString *deleteString=[NSString stringWithFormat:@"delete from note where id=%d",aNote.noteId]; 

    //转成utf-8的c的风格  

    const char *deleteSql=[deleteString UTF8String]; 

    //执行删除语句  

    if(sqlite3_exec(database, deleteSql, NULL, NULL, &errorMsg)==SQLITE_OK){ 

        NSLog(@"删除成功"); 

    } 

    return YES; 



-(BOOL)update:(NotePad*)aNote{ 

    //更新语句  

    NSString *updateString=[NSString stringWithFormat:@"update note set theme='%@', information='%@',
ndate='%@',priority=%d where id=%d",aNote.theme,aNote.information,aNote.ndate,aNote.priority,aNote.noteId]; 

   // NSLog(@"%@",aNote);  

    const char *updateSql=[updateString UTF8String]; 

    //执行更新语句  

    if(sqlite3_exec(database, updateSql, NULL, NULL, &errorMsg)==SQLITE_OK){ 

        NSLog(@"更新成功"); 

    }    

    return YES; 



 

-(NoteDb*)selecteAll{ 

    NoteDb *noteDb=[[[NoteDb alloc]init]autorelease]; 

    //查询所有语句  

    const char *selectAllSql="select * from note"; 

    //执行查询  

    if (sqlite3_prepare_v2(database, selectAllSql, -1, &statement, nil)==SQLITE_OK) { 

        NSLog(@"select ok.");   

        //如果查询有语句就执行step来添加数据  

        while (sqlite3_step(statement)==SQLITE_ROW) { 

            NotePad *note=[[NotePad alloc]init]; 

             

            int noteid=sqlite3_column_int(statement, 0); 

            NSMutableString *theme=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding]; 

            NSMutableString *information=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding]; 

            NSString *ndateString=[NSString stringWithCString:(char*)sqlite3_column_text(statement, 3) encoding:NSUTF8StringEncoding]; 

            NSDateFormatter* formater = [[NSDateFormatter alloc] init]; 

            [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

             

            NSDate *ndate=[formater dateFromString:[ndateString substringToIndex:[ndateString length]-5]]; 

           // NSLog(@"%@",[ndateString substringToIndex:[ndateString length]-5]);  

            [formater release]; 

            int proriory=sqlite3_column_int(statement, 4); 

             

            note.noteId=noteid; 

            note.theme=theme; 

            note.information=information; 

            note.ndate=ndate; 

            note.priority=proriory; 

            [noteDb addNote:note]; 

            [note release]; 

             

        } 

        return noteDb; 

    } 

    return noteDb; 



-(NoteDb*)selectNotes:(NotePad*)aNote{ 

    NoteDb *noteDb=[[[NoteDb alloc]init]autorelease]; 

    NSString *selectNSSql=[NSString stringWithFormat:@"select * from note where id=%i",aNote.noteId]; 

    //查询所有语句  

    const char *selectSql=[selectNSSql UTF8String]; 

    //执行查询  

    if (sqlite3_prepare_v2(database, selectSql, -1, &statement, nil)==SQLITE_OK) { 

        NSLog(@"select ok.");   

        //如果查询有语句就执行step来添加数据  

        while (sqlite3_step(statement)==SQLITE_ROW) { 

            NotePad *note=[[NotePad alloc]init]; 

             

            int noteid=sqlite3_column_int(statement, 0); 

            NSMutableString *theme=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding]; 

            NSMutableString *information=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding]; 

            NSString *ndateString=[NSString stringWithCString:(char*)sqlite3_column_text(statement, 3) encoding:NSUTF8StringEncoding]; 

            NSDateFormatter* formater = [[NSDateFormatter alloc] init]; 

            [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

             

            NSDate *ndate=[formater dateFromString:[ndateString substringToIndex:[ndateString length]-5]]; 

            // NSLog(@"%@",[ndateString substringToIndex:[ndateString length]-5]);  

            [formater release]; 

            int proriory=sqlite3_column_int(statement, 4); 

             

            note.noteId=noteid; 

            note.theme=theme; 

            note.information=information; 

            note.ndate=ndate; 

            note.priority=proriory; 

            [noteDb addNote:note]; 

            [note release]; 

             

        } 

        return noteDb; 

    } 

    return noteDb; 



@end 

#import "NoteSqlite.h"

#import "NotePad.h"

#import "NoteDb.h"

@implementation NoteSqlite

-(id)init{

    self=[super init];

    return self;

}

//打开数据库

-(BOOL)open{

    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"noteList.db"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL find = [fileManager fileExistsAtPath:path];

    //判断文件是否存在 www.2cto.com

    if (find) {

        NSLog(@"数据库文件已经存在");

        //打开数据库、返回操作是否正确

        if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

            NSLog(@"打开成功数据库");

        }

        return YES;

    }else{

        if(sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

            //调用createMusicList创建数据库和表

            [self create];            

            return YES;

        } else {

            sqlite3_close(database);

            NSLog(@"Error: open database file.");

            return NO;

        }

        return NO;

    }

}

//创建表

-(BOOL)create{

    //创建表语句

    const char *createSql="create table if not exists note (id integer primary key autoincrement,theme text,information text,ndate text,priority integer)";

    //创建表是否成功

    if (sqlite3_exec(database, createSql, NULL, NULL, &errorMsg)==SQLITE_OK) {      

        NSLog(@"create ok.");  

        return YES;

    }else{

        //打印出错信息

        NSLog(@"error: %s",errorMsg);

        sqlite3_free(errorMsg);

    }

    return NO;

}

//增加、删除、修改、查询

-(BOOL)insert:(NotePad*)aNote{

    //向表中插入记录 

    //定义一个sql语句       

    NSString *insertStatementNS = [NSString stringWithFormat:

           @"insert into \"note\"\

           (theme, information, ndate,priority)\

           values (\"%@\", \"%@\", \"%@\",%d)",

           aNote.theme,aNote.information,[NSString stringWithFormat:@"%@",aNote.ndate],aNote.priority

                                    ];

    //将定义的NSString的sql语句,转换成UTF8的c风格的字符串

    const char *insertSql = [insertStatementNS UTF8String];

    //执行插入语句

    if (sqlite3_exec(database, insertSql, NULL, NULL, &errorMsg)==SQLITE_OK) { 

        NSLog(@"insert ok.");

        return YES;

    }else{

        NSLog(@"error: %s",errorMsg);

        sqlite3_free(errorMsg);

    }

    return NO;

}

-(BOOL)deleteALLNote{

    //删除所有数据,条件为1>0永真

    const char *deleteAllSql="delete from note where 1>0";

    //执行删除语句

    if(sqlite3_exec(database, deleteAllSql, NULL, NULL, &errorMsg)==SQLITE_OK){

        NSLog(@"删除所有数据成功");

    }

    return YES;

}

-(BOOL)deleteaNote:(NotePad*)aNote{

    //删除某条数据

    NSString *deleteString=[NSString stringWithFormat:@"delete from note where id=%d",aNote.noteId];

    //转成utf-8的c的风格

    const char *deleteSql=[deleteString UTF8String];

    //执行删除语句

    if(sqlite3_exec(database, deleteSql, NULL, NULL, &errorMsg)==SQLITE_OK){

        NSLog(@"删除成功");

    }

    return YES;

}

-(BOOL)update:(NotePad*)aNote{

    //更新语句

    NSString *updateString=[NSString stringWithFormat:@"update note set theme='%@', information='%@',
ndate='%@',priority=%d where id=%d",aNote.theme,aNote.information,aNote.ndate,aNote.priority,aNote.noteId];

   // NSLog(@"%@",aNote);

    const char *updateSql=[updateString UTF8String];

    //执行更新语句

    if(sqlite3_exec(database, updateSql, NULL, NULL, &errorMsg)==SQLITE_OK){

        NSLog(@"更新成功");

    }  

    return YES;

}

-(NoteDb*)selecteAll{

    NoteDb *noteDb=[[[NoteDb alloc]init]autorelease];

    //查询所有语句

    const char *selectAllSql="select * from note";

    //执行查询

    if (sqlite3_prepare_v2(database, selectAllSql, -1, &statement, nil)==SQLITE_OK) {

        NSLog(@"select ok."); 

        //如果查询有语句就执行step来添加数据

        while (sqlite3_step(statement)==SQLITE_ROW) {

            NotePad *note=[[NotePad alloc]init];

           

            int noteid=sqlite3_column_int(statement, 0);

            NSMutableString *theme=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];

            NSMutableString *information=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding];

            NSString *ndateString=[NSString stringWithCString:(char*)sqlite3_column_text(statement, 3) encoding:NSUTF8StringEncoding];

            NSDateFormatter* formater = [[NSDateFormatter alloc] init];

            [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

           

            NSDate *ndate=[formater dateFromString:[ndateString substringToIndex:[ndateString length]-5]];

           // NSLog(@"%@",[ndateString substringToIndex:[ndateString length]-5]);

            [formater release];

            int proriory=sqlite3_column_int(statement, 4);

           

            note.noteId=noteid;

            note.theme=theme;

            note.information=information;

            note.ndate=ndate;

            note.priority=proriory;

            [noteDb addNote:note];

            [note release];

           

        }

        return noteDb;

    }

    return noteDb;

}

-(NoteDb*)selectNotes:(NotePad*)aNote{

    NoteDb *noteDb=[[[NoteDb alloc]init]autorelease];

    NSString *selectNSSql=[NSString stringWithFormat:@"select * from note where id=%i",aNote.noteId];

    //查询所有语句

    const char *selectSql=[selectNSSql UTF8String];

    //执行查询

    if (sqlite3_prepare_v2(database, selectSql, -1, &statement, nil)==SQLITE_OK) {

        NSLog(@"select ok."); 

        //如果查询有语句就执行step来添加数据

        while (sqlite3_step(statement)==SQLITE_ROW) {

            NotePad *note=[[NotePad alloc]init];

           

            int noteid=sqlite3_column_int(statement, 0);

            NSMutableString *theme=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];

            NSMutableString *information=[NSMutableString stringWithCString:(char*)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding];

            NSString *ndateString=[NSString stringWithCString:(char*)sqlite3_column_text(statement, 3) encoding:NSUTF8StringEncoding];

            NSDateFormatter* formater = [[NSDateFormatter alloc] init];

            [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

           

            NSDate *ndate=[formater dateFromString:[ndateString substringToIndex:[ndateString length]-5]];

            // NSLog(@"%@",[ndateString substringToIndex:[ndateString length]-5]);

            [formater release];

            int proriory=sqlite3_column_int(statement, 4);

           

            note.noteId=noteid;

            note.theme=theme;

            note.information=information;

            note.ndate=ndate;

            note.priority=proriory;

            [noteDb addNote:note];

            [note release];

           

        }

        return noteDb;

    }

    return noteDb;

}

@end

iphone数据库(sqlite3)的用法操作oc,xcode的更多相关文章

  1. [python][django学习篇][5]选择数据库版本(默认SQLite3) 与操作数据库

    推荐学习博客:http://zmrenwu.com/post/6/ 选择数据库版本(SQLite3) 如果想选择MySQL等版本数据库,请先安装MySQL并且安装python mysql驱动,这里不做 ...

  2. iphone/iOS 访问本地数据库sqlite3

    Phone也支持访问本地数据库Sqlite 3.这里简单的介绍一下iPhone上Sqlite 3的使用方法. 首先需要在项目中引用Sqlite 3的开发包,下面是在iPhone SDK 3.0下的目录 ...

  3. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  4. 数据库(SQLITE3函数总结): sqlite3_open, sqlite3_exec, slite3_close,sqlite3_prepare_v2,sqlite3_column_text,

    Sqlite3 的确非常好用.小巧.速度快.近期研究它,有一些收获,这里把我对 sqlite3 的研究列出来,以备忘记. 导入SQLLite library并引入头文件. libsqlite3.dyl ...

  5. 我的Android进阶之旅------>温习Sqlite3的常用操作

    前言;今天要写一个应用来调节系统的Brightness值,来改变系统的背光亮度.由于刚开始些的时候没有考虑Brightness的最小值,直接托动SeekBar到最小值(为0).瞬间,屏幕变成全黑,失败 ...

  6. Android中如何使用命令行查看内嵌数据库SQLite3

    转载博客:http://www.linuxidc.com/Linux/2011-06/37135.htm 在上图中,除了最后一个红色的方框,其它方框都是adb shell下的命令. [1]在Andro ...

  7. SQL用法操作合集

    SQL用法操作合集   一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...

  8. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  9. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

随机推荐

  1. B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示

    http://codeforces.com/gym/100633/problem/B B. Dispersed parentheses time limit per test 2 seconds me ...

  2. VPS/云主机CPU占用100%故障排查

    VPS/云主机CPU占用100%故障排查 方法/步骤 通常情况下云主机/VPS的CPU一般不会占用100%,内存资源也不会占完.若您的服务器经常CPU资源100%,可以打开任务管理器,查看是哪个进程引 ...

  3. [转]Creating Mailing Labels in SQL Server Reporting Services (rdlc 数据1页 2竖排 显示)

    本文转自:http://blogs.wrox.com/article/creating-mailing-labels-in-sql-server-reporting-services/ Most wo ...

  4. tomcat在idea中启动乱码

    server,localhost log,catalina log分别乱码 打开tomcat/conf/目录下修改logging.properties 找到"utf-8"行更改为 ...

  5. H5网站加载速度优化总结

    1. 在代码文件结构 尽量优化的同时,能力再强已经到极限了,但你服务器辣鸡,搭配不当,你代码优化上天 也是徒劳啊. 2.你不怎么优化, 服务器 各种技术配置到位的话, now你也看到了,我一个系统首页 ...

  6. [POJ1185][NOI2001]炮兵阵地 状压DP

    题目链接:http://poj.org/problem?id=1185 很裸的状压,考虑对于一行用二进制储存每一种的状态,但是状态太多了做不了. 观察到有很多状态都是不合法的,于是我们预处理出合法的状 ...

  7. Android自定义view之仿微信录制视频按钮

    本文章只写了个类似微信的录制视频的按钮,效果图如下:             一.主要的功能: 1.长按显示进度条,单击事件,录制完成回调 2.最大时间和最小时间控制 3.进度条宽度,颜色设置 二.实 ...

  8. 记AccessibilityService使用(转)

    转自 :http://www.jianshu.com/p/ba298b8d5a6e 一.AccessibilityService的使用 首先先写一个类去继承AccessibilityService p ...

  9. Azure 8 月新公布

    Azure 8 月新发布:Cosmos DB 每分钟请求单位,存储的托管磁盘及促销,高级和标准磁盘存储大尺寸磁盘,高级磁盘存储小尺寸磁盘. Azure Cosmos DB:每分钟请求单位为您降低成本, ...

  10. (十四)maven之启动tomcat

    前言:在网上找了好几种方法启动web项目.比较好用的是:①在Project Facets勾上Dynamic....,但是这个方法会改变项目结构(把WebContent的东西都弄出来了):②使用jett ...