在小节12.6.3中,介绍到创建表需要预处理和执行创建两个过程。在预处理上有好几个函数供来选择,在执行创建的时候多选择sqlite3_step来处理。代码清单12-7展示了如何在一个打开的数据库中创建表。代码清单的数据库表有五个字段,它们分别是id、cid、title、imageData和imageLen 。其中 id为表格的主键,cid,和title都是字符串,imageData是二进制数据,imageLen 是该二进制数据的长度。
代码清单12-7 创建表
//在打开的数据库中创建表,其中sqldb为成功打开数据库的sqlite3对象 - (BOOL) createChannelsTable:(sqlite3*)sqlDataBase{ //设置SQL语句 char *sql = "CREATE TABLE channels (id integer primary key, \ cid text, \ title text, \ imageData BLOB, \ imageLen integer)"; sqlite3_stmt *statement; //进行预处理,预处理失败返回NO if(sqlite3_prepare_v2(sqlDataBase, sql, -1, &statement, nil) != SQLITE_OK) { return NO; } //预处理成功,进行执行创建 int success = sqlite3_step(statement); sqlite3_finalize(statement); if ( success != SQLITE_DONE) { return NO; } return YES; }