【发布时间】:2015-05-21 22:23:38
【问题描述】:
将 SQLITE3 数据库设置为应用中的永久存储。我需要检查是否存在填充了表的数据库对象,如果不存在表(即在首次启动 APP 时),则执行 SQL DDL 语句,想知道解决此问题的最佳方法是我下面的方法还是完全错误的方法/有没有更好的办法?!我可以在 int ri = sqlite3_open_v2("schoolDatabase.db",&_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL); 之后检查数据库属性是否为 null/nil命令还是即使数据库没有表也会返回一个值?
@interface DBAccess ()
@property (nonatomic,strong) sqlite3 *database;
@implementation DBAccess
@synthesize database = _database
-(id)init
int check =sqlite3_initialise();
if(check==SQLITE_OK){
int ri = sqlite3_open_v2("schoolDatabase.db",&_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL);
if(ri==SQLITE_OK){
NSLOG(@"Database created successfully};
else{ NSLog(@"problem creating database");}
sqlite3_stmt *statement;
const char *databaseExistence = "SELECT name FROM sqlite_master WHERE Type = table";
sqlite3_prepare_v2(self.database,databseExistence,-1,&statement,NULL);
while(sqlite3_step(statement)==SQLITE_ROW)
{
//I am presuming column indexes are zero based in sqlite3 ?
const unsigned char *tableNames = sqlite3_column_text(statement,2);
if(tableNames=nil){
NSLog(@"Tables in database do not exist......creating tables"
//insert SQL statement to prepare and execute CREATE TABLE commands for the Blank database
【问题讨论】:
-
您当前的查询可能是最好的方法。