一、在终端上建立数据库:sqlite3 mydata.db;create table person(id,name,age);
create table if not exists person(id,name,age);//若是没有这个表,就建立,有不建立
create table if not exists person(id interger primary key autoincrement,name text,age interger);
//标准写法
二、插入
insert into person (id,name,age)values('1','name','19');
update person set name = 'cao';
delete from person where id = '1';
select id ,name from person;
select * from person;
select * from person where id = '1';
3.
select count(*)from info;
4,查看数据库信息,SQLite特有的命令
.tables 查看表
.schema 显示表头
.dump <table name>显示历史命令
5,在一个工程里添加数据库
(1)导入libsqlite3.dylib文件
点击工程,summary->Linked Frameworks and Libraries ->libsqlite3.dylib,add;
#import <sqlite3.h>
sqlite3 * db;//用于操做数据库变量
NSString * db_file = [[NSBundle mainBundle] pathForResource:@"qq"ofType :@"db"];
//打开数据库
int n = sqlite3_open([db_file UTF8String],&db);让DB指针指向数据库
if(n != SQLITE_OK)
{
NSLog(@"open error");//判断是否打开数据库
}
sqlite3_stmt * statement = nil;//指向表头,而且存储查询的数据
NSString * sql = @"select * from chat";
if(sqlite3_prepare(db,[sql UTF8String],-1,&statement,NULL)!=SQLITE_OK)
{
NSLog(@"start error");//开始查询
}
while(sqlite3_step(statement)==SQLITE_OK)
{
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);//关闭数据库sql
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;
if(sqlite3_exec(database,[sql UTF8String],NULL,NULL,&errMsg)!=SQLITE_OK)
{
NSLog(@"error to exec");
}
}
-(void)closeDatabase
{
if(database)
{
sqlite3_close(database);
}
}
-(void)dealloc
{
[self closeDatabase];
}
而且在添加主页的那个函数里面添加代码。
[self openDatabase];数据库
4月12日心得:函数
今天链接了数据库,很受打击,总结了一下今天的东西,发现总结真的是一个好东西,atom
一、建立数据库,在终端,随便找个文件夹sqlite3 databaseName.db.这时候把建立好的数据库导入到项目中,此时,项目与终端已经没什么联系了,表呢,通常在项目中建立,避免别人装个系统还要装个终端?指针
二、能够打印出来项目的沙盒,由于你的databaseName.db就在沙盒里呀,笨蛋,这时候再在终端找到沙盒,就能够在终端观察数据库在程序中的运行了,得到沙盒路径的方法有两种:sqlite
NSString * path = [[NSBuddle mainBuddle] pathForResource:@"databaseName" forType:@"db"];开发
另一种,得到沙盒后还能够在沙盒中加入指定的文件夹rem
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSlog("%@",path);
return [path stringByAppendingPathComponent:@"myDatabaseName"];string
三、接下来就开始打开数据库了,首先要设置一个全局变量sqlite3 * database;it
打开数据库在上面都有,就是把沙盒中的数据库读到 database里面。
sqlite3_open([path UTF8string],&database);