使用数据库个人比较推荐使用FMDB对数据库封装操作,这样简化操作步骤,用起来十分方便。
github:
https://github.com/ccgus/fmdb
创建表
FMDatabase
*db = [
FMDatabase
databaseWithPath
:[
NSHomeDirectory
()
stringByAppendingString
:
@"/tem.db"
]];
打开数据库
打开数据库
if ([db open]) {
创建表语句
NSString
*sqlCreateTable = [
NSString
stringWithFormat
:
@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)"
,
@"user"
,
@"ID"
,
@"NAME"
,
@"AGE"
,
@"ADDRESS"
];
执行SQL语句
BOOL
isOK = [db
executeUpdate
:sqlCreateTable];
if (isOK) {
NSLog ( @"create ok!" );
} else {
NSLog ( @"not ok!" );
if (isOK) {
NSLog ( @"create ok!" );
} else {
NSLog ( @"not ok!" );
}
关闭数据库
[db
close
];
}
插入数据
if
([db
open
]) {
NSString *insertSql1= [ NSString stringWithFormat :
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%d', '%@')" ,
@"user" , @"NAME" , @"AGE" , @"ADDRESS" , @" 李四 " , 13 , @" 济南 " ];
BOOL isinsert = [db executeUpdate :insertSql1];
if (isinsert) {
NSLog ( @" 插入成功! " );
} else {
NSLog ( @" 插入失败! " );
}
[db close ];
NSString *insertSql1= [ NSString stringWithFormat :
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%d', '%@')" ,
@"user" , @"NAME" , @"AGE" , @"ADDRESS" , @" 李四 " , 13 , @" 济南 " ];
BOOL isinsert = [db executeUpdate :insertSql1];
if (isinsert) {
NSLog ( @" 插入成功! " );
} else {
NSLog ( @" 插入失败! " );
}
[db close ];
}
修改数据
if
([db
open
]) {
NSString *updateSql = [ NSString stringWithFormat :
@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'" ,
@"user" , @"AGE" , @130 , @"ID" , @"1" ];
BOOL isupdate = [db executeUpdate :updateSql];
if (isupdate) {
NSLog ( @" 修改成功! " );
} else {
NSLog ( @" 修改失败! " );
}
[db close ];
NSString *updateSql = [ NSString stringWithFormat :
@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'" ,
@"user" , @"AGE" , @130 , @"ID" , @"1" ];
BOOL isupdate = [db executeUpdate :updateSql];
if (isupdate) {
NSLog ( @" 修改成功! " );
} else {
NSLog ( @" 修改失败! " );
}
[db close ];
}
删除数据
if
([db
open
]){
NSString *deleteSql = [ NSString stringWithFormat :
@"delete from %@ where %@ = '%@'" ,
@"user" , @"ID" , @"3" ];
BOOL res = [db executeUpdate :deleteSql];
if (res) {
NSLog ( @" 删除成功! " );
} else {
NSLog ( @" 删除失败! " );
}
[db close ];
NSString *deleteSql = [ NSString stringWithFormat :
@"delete from %@ where %@ = '%@'" ,
@"user" , @"ID" , @"3" ];
BOOL res = [db executeUpdate :deleteSql];
if (res) {
NSLog ( @" 删除成功! " );
} else {
NSLog ( @" 删除失败! " );
}
[db close ];
}
查看数据库
if
([db
open
]) {
NSString * sql = [ NSString stringWithFormat :
NSString * sql = [ NSString stringWithFormat :
@"SELECT * FROM %@",@"user"];
执行查询语句
FMResultSet
* rs = [db
executeQuery
:sql];
判断时候有下一个
while
([rs
next
]) {
取数据按照给定的列的名字
int
Id = [rs
intForColumn
:
@"ID"
];
NSString * name = [rs stringForColumn : @"NAME" ];
NSString * age = [rs stringForColumn : @"AGE" ];
NSString * address = [rs stringForColumn : @"ADDRESS" ];
NSLog ( @"id = %d, name = %@, age = %@ address = %@" , Id, name, age, address);
}
[db close ];
NSString * name = [rs stringForColumn : @"NAME" ];
NSString * age = [rs stringForColumn : @"AGE" ];
NSString * address = [rs stringForColumn : @"ADDRESS" ];
NSLog ( @"id = %d, name = %@, age = %@ address = %@" , Id, name, age, address);
}
[db close ];
}