我一直致力于iOS的Ionic Phonegap项目。在Appdelegate.m
中实现了一种方法,该方法发出AJAX请求以从服务器下载文本文件,该服务器包含连接到另一台服务器的URL,以便应用程序正常工作。
我做了两节课,
WebContent
和WebCustomContent
在WebContent.m
中,我将从文本文件中获取的特定URL插入sqlite数据库,然后使用WebCustomContent.m
请参阅以下代码块
-(NSString*)getDataBasePath{
//CHECK
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:@"webcontentdb.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
NSLog(@"%d", fileExists);
//END OF CHECK
//SIMULATOR
NSString *databasePath1 = [[NSBundle mainBundle] pathForResource:@"webcontentdb" ofType:@"sqlite"];
// return databasePath1;
//REAL DEVICE
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"webcontentdb.sqlite"];
return databasePath;
}
-(void)updateUserAgeRange:(NSString*)age{
NSString* databasePath = [self getDataBasePath];
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *query = [NSString stringWithFormat:@"update user_setting set valstr = '%@' where keystr = 'AGE' ", age];
NSLog(@"update %@" , query);
const char * sql = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement); // Here is the added step.
NSLog(@"updateContact SUCCESS - executed command %@",query);
}
else {
NSLog(@"updateContact FAILED - failed to execute command %@",query);
}
sqlite3_finalize(compiledStatement);
}
else {
//NSLog(@"pdateContact FAILED - failed to open database");
}
sqlite3_close(database);
}
- (NSString *)getUserPreferenceValues:(NSString*)keystr {
NSString *retval = [[NSString alloc] init] ;
NSString *query = [NSString stringWithFormat:@"SELECT valstr FROM user_setting where keystr = '%@' " , keystr];
NSLog(@" query %@", query);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 0 );
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSLog(@" valstr %@", name);
retval = name;
}
sqlite3_finalize(statement);
}
return retval;
}
-(void)insertDatabaseCommonValues:(NSString*)urlstr{
NSString* databasePath = [self getDataBasePath];
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *query = [NSString stringWithFormat:@"delete from url_preference"];
const char * sql = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement); // Here is the added step.
// NSLog(@"updateContact SUCCESS - executed command %@",query);
}
else {
//NSLog(@"updateContact FAILED - failed to execute command %@",query);
}
sqlite3_finalize(compiledStatement);
}
else {
//NSLog(@"pdateContact FAILED - failed to open database");
}
//************************************INSERT************************************//
//sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
//NSLog(@"URL STRING %@",urlstr);
NSString *query = [NSString stringWithFormat:@"insert into url_preference (name) values ( '%@' ) ", urlstr];
NSLog(@"inset %@" , query);
const char * sql = [query UTF8String];
sqlite3_stmt *compiledStatement;
NSLog(@" error code.. %d",sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL));
if(sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement); // Here is the added step.
NSLog(@"updateContact SUCCESS - executed command %@",query);
}
else {
NSLog(@"updateContact FAILED - failed to execute command %@",query);
}
sqlite3_finalize(compiledStatement);
}
else {
//NSLog(@"pdateContact FAILED - failed to open database");
}
sqlite3_close(database);
}
这里,当我打印BOOL变量fileExists时,它会输出YES,这意味着数据库存在于Documents文件夹中。 但插入和更新查询失败如下;
2015-06-22 11:18:18.215 App Name[5510:60b] URL http://www.google.lk
2015-06-22 11:18:22.082 App Name[5510:60b] 1
2015-06-22 11:18:24.103 App Name[5510:60b] success to open database!
2015-06-22 11:18:26.197 App Name[5510:60b] 1
2015-06-22 11:18:28.673 App Name[5510:60b] inset insert into url_preference (name) values ( 'http://www.google.lk' )
2015-06-22 11:18:28.676 App Name[5510:60b] error code.. 1
2015-06-22 11:18:28.679 App Name[5510:60b] updateContact FAILED - failed to execute command insert into url_preference (name) values ( 'http://www.google.lk' )
我已将数据库文件放在项目文件夹中,如下所示;
我似乎无法弄清楚我做错了什么。请帮忙。
答案 0 :(得分:0)
这是文件权限问题吗?我建议右键单击Finder中的webcontentdb.sqlite
文件,选择Get Info
,然后在“共享&权限允许所有用户拥有Read & Write
权限。
如果这不起作用,我会使用SQLite浏览器应用程序验证数据库文件是否可以写入并且没有以任何方式损坏。