1 |
+(void)insertPeople:(People *)aPeople |
2 |
{ |
3 |
if (!db) { |
4 |
[selfcreatDatabase]; |
5 |
} |
6 |
|
7 |
if (![db open]) { |
8 |
NSLog(@"数据库打开失败"); |
9 |
return; |
10 |
} |
11 |
|
12 |
[dbsetShouldCacheStatements:YES]; |
13 |
|
14 |
if(![dbtableExists:@"people"]) |
15 |
{ |
16 |
[selfcreatTable]; |
17 |
} |
18 |
//以上操作与创建表是做的判断逻辑相同 |
19 |
//现在表中查询有没有相同的元素,如果有,做修改操作 |
20 |
FMResultSet *rs = [dbexecuteQuery:@"select * from people where people_id = ?",[NSStringstringWithFormat:@"%d",aPeople.peopleID]]; |
21 |
if([rs next]) |
22 |
{ |
23 |
NSLog(@"dddddslsdkien"); |
24 |
[dbexecuteUpdate:@"update people set name = ?, age = ? where people_id = 1",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; |
25 |
} |
26 |
//向数据库中插入一条数据 |
27 |
else{ |
28 |
[dbexecuteUpdate:@"INSERT INTO people (name, age) VALUES (?,?)",aPeople.name,[NSStringstringWithFormat:@"%d",aPeople.age]]; |
29 |
} |
30 |
|
31 |
} |