我在ios项目的sqlite3数据库中创建了一个LXSGW表:
char *errorMsg;
const char *createLXSGWSql="create table if not exists LXSGW (id integer primary key autoincrement,name text,money_per_month integer,local_flow integer,local_pronunciation integer,local_wifi integer,past_pronunciation integer,past_flow integer,rating integer,others text )";
if (sqlite3_exec(dataBase, createLXSGWSql, NULL, NULL, &errorMsg)==SQLITE_OK) {
NSLog(@"create LXSGW ok.");
}
else {
NSLog(@"error: %s",errorMsg);
sqlite3_free(errorMsg);
}
并将两个名称为“wg001”和“wg002”的记录插入其中:
插入'wg001':
sqlite3_stmt *stmt;
//insert records into LXSGW Table
const char *insertLXSGWSql="insert into LXSGW (name,money_per_month,local_flow,local_pronunciation,local_wifi,past_pronunciation,past_flow,rating,others) values(?,?,?,?,?,?,?,?,?)";
int countForLXSGW=0;
if (sqlite3_prepare_v2(dataBase, insertLXSGWSql, -1, &stmt, nil)==SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [@"wg001" UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 2, 49);
sqlite3_bind_int(stmt, 3,200);
sqlite3_bind_int(stmt, 4,100);
sqlite3_bind_int(stmt, 5, 30);
sqlite3_bind_int(stmt, 6, 2);
sqlite3_bind_int(stmt, 7, 3);
sqlite3_bind_int(stmt, 8, 5);
sqlite3_bind_text(stmt, 9, [@"其他" UTF8String], -1, NULL);
countForLXSGW=countForLXSGW+1; //用于计数,可以观察到数据信息插入的情况
}
if (sqlite3_step(stmt)!=SQLITE_DONE) {
NSLog(@"insert error .....");
}
else
{
NSLog(@"countForLXSGW is %i",countForLXSGW);
}
sqlite3_reset(stmt);
插入'wg002':
if (sqlite3_prepare_v2(dataBase, insertLXSGWSql, -1, &stmt, nil)==SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [@"wg002" UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 2,79);
sqlite3_bind_int(stmt, 3,200);
sqlite3_bind_int(stmt, 4,100);
sqlite3_bind_int(stmt, 5, 25);
sqlite3_bind_int(stmt, 6, 2);
sqlite3_bind_int(stmt, 7, 3);
sqlite3_bind_int(stmt, 8, 5);
sqlite3_bind_text(stmt, 9, [@"其他" UTF8String], -1, NULL);
countForLXSGW=countForLXSGW+1; //用于计数,可以观察到数据信息插入的情况
}
if (sqlite3_step(stmt)!=SQLITE_DONE) {
NSLog(@"insert error .....");
}
else
{
NSLog(@"countForLXSGW is %i",countForLXSGW);
}
sqlite3_reset(stmt);
还有选择代码:
//read names from LW Table For Test
//----------------------------------
const char *selectLWSql="select name from LXSGW";
if (sqlite3_prepare_v2(dataBase, selectLWSql, -1, &stmt, nil)==SQLITE_OK) {
while (sqlite3_step(stmt)==SQLITE_ROW) {
NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(stmt, 0) encoding:NSUTF8StringEncoding];
NSLog(@"%@",name);
[name release];
}
}
else
{
NSLog(@"error: %s",errorMsg);
sqlite3_free(errorMsg);
}
sqlite3_reset(stmt);
当我运行项目时: