0
我一直在为iOS设计Ionic Phonegap项目。在Appdelegate.m
中实现了一个方法,该方法使得AJAX请求从服务器下载文本文件,该文件包含连接到另一个服务器的URL以使应用程序工作。iOS - 在真实设备上连接到数据库的错误
我已经做了两个班, WebContent
和WebCustomContent
在WebContent.m
我插入从文本文件带到一个SQLite数据库特定的URL,然后使用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,意味着数据库存在于Document中s文件夹。 但插入和更新查询失败,如下所示;
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' )
我已经将数据库文件放在项目文件夹中,如下所示;
我似乎无法找出什么我做错了。请帮忙。