为什么我的sqlite3数据库中有重复记录?

时间:2012-05-03 06:21:11

标签: ios database

我在ios项目中创建了一个数据库,并创建了一些表格。

sqlite3 *dataBase;

NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                                            , NSUserDomainMask 
                                                            , YES); 
NSString *databaseFilePath=[[documentsPaths objectAtIndex:0]   stringByAppendingPathComponent:@"mydb"];

if (sqlite3_open([databaseFilePath UTF8String], &dataBase)==SQLITE_OK) { 
    NSLog(@"open sqlite db ok."); 
}

//create tables
const char *createPublicSql="create table if not exists Public (id integer primary key    autoincrement,name text,money_per_month integer,local_respond text,city_call   integer,local_long_call integer,rating integer)";
if (sqlite3_exec(dataBase, createPublicSql, NULL, NULL, &errorMsg)==SQLITE_OK) { 
    NSLog(@"create Public ok."); 
}
else { 
    NSLog(@"error: %s",errorMsg); 
    sqlite3_free(errorMsg); 
}

我在每个表中插入了几条记录,例如:

const char *insertPublicOneSql="insert into Public  (name,money_per_month,local_respond,city_call,local_long_call,rating)values('p001',50,'全 免',2,3,8)"; 
if (sqlite3_exec(dataBase, insertPublicOneSql, NULL, NULL, &errorMsg)==SQLITE_OK) { 
    NSLog(@"insert 0401 ok."); 
}
else { 
    NSLog(@"error: %s",errorMsg); 
    sqlite3_free(errorMsg); 
}

然而,当我查找id时,请在Public中输入名称:

const char *selectPublicSql="select id,name from Public"; 
sqlite3_stmt *statement; 
if (sqlite3_prepare_v2(dataBase, selectPublicSql, -1, &statement, nil)==SQLITE_OK) { 
    NSLog(@"select ok."); 
}
while (sqlite3_step(statement)==SQLITE_ROW) { 
    int _id=sqlite3_column_int(statement, 0); 
    NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding]; 
    NSLog(@"row>>id %i, name %@",_id,name); 
}

结果:

2012-05-03 11:04:47.259 China_telecom[2342:f803] row>>id 1, name p001
2012-05-03 11:04:47.260 China_telecom[2342:f803] row>>id 2, name p001
2012-05-03 11:04:47.261 China_telecom[2342:f803] row>>id 3, name p001
2012-05-03 11:04:47.262 China_telecom[2342:f803] row>>id 4, name p001
2012-05-03 11:04:47.263 China_telecom[2342:f803] row>>id 5, name p001
2012-05-03 11:04:47.263 China_telecom[2342:f803] row>>id 6, name p001

问题是什么?

0 个答案:

没有答案