1

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、名前を選択すると:

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

どうしたの?

4

0 に答える 0