background image

NSString * time = [NSString stringWithUTF8String
(char *)sqlite3_column_text(statement,0)];
//0 代表列号,从 0-最后一列-1;
}
sqlite3_finalize(statement);//finish 查询
//插入数据
NSString * sql1 = @”insert into chat values(’2011-9-19′,’xiaoji’,'hoew’,’1′)”;
char * errmsg;
if(sqlite3_exec(db, [sql1 UTF8String], NULL, NULL, &errmsg)!=SQLITE_OK)

{
NSLog(@”message:%s”,errmsg);
}

sqllite3_close(db);//关闭数据库

    6,由于在 iPhone 项目开发中,一个项目对应唯一的 UIApplication,一个
UIApplication 又对应唯一的 ApplicationDelegate,即常见的 AppDelegate 文件
因此,可以在这里面增加一些全局都要使用的,比如数据库的打开关闭。
AppDelegate.h 中添加:
#import <sqlite3.h>
sqlite3* database;//属性。
@property (nonatomic,retain)sqlite3 * database;
AppDelegate.h 中添加。
@synthesize database;
声明函数:
-(NSString *)dataPath
{
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSlog(“%@”,path);
return [path stringByAppendingPathComponent:@"myDatabaseName"];
}
-(void)openDatabase
{
NSString * name = [self dataPath];
if(sqlite3_open([name UTF8String],&database)!=SQLITE_OK)

NSLog(@”open error”);
database = NULL;

NSString * sql = @”create table if not exists”
“personInfo(ID integer primary key autoincrement,name text,age int)”;
char * errMsg = NULL;