我想做的是从我自己的 sqlite 文件中读取数据。
我的应用程序允许我制作一个 sqlite 文件,然后从中读取,但是如果我添加文件“asg.sqlite”,它不会从它读取它,只有当我让我的应用程序首先创建 sqlite 文件时,我才能从中读取它。它绞尽脑汁请帮助这是我的代码
-(void) createTable: (NSString *) tableName
withField1:(NSString *) field1
withField2:(NSString *) field2
withField3:(NSString *) field3
withField4:(NSString *) field4;
{
char *err;
NSString *sql = [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXISTS '%@' ('%@' "" TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER, '%@' TEXT);", tableName, field1, field2, field3, field4 ];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)
!= SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Could not create table");
} else {
NSLog(@"table created");
}
}
//file path to database
-(NSString *) filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"asg.sqlite"
];
}
//open the database
-(void)openDB {
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Database failed to open");
}else{
NSLog(@"database opened");
}
}