我构建了一个应用程序,在其中我使用sqlite来存储数据。它在模拟器中运行良好,但当它在真实设备上运行时,它会停止响应sqlite。即使代码没有响应sqlite命令..这是我的代码..
- (void)viewDidLoad {
//Create Database
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir=[paths objectAtIndex:0];
NSString *dirString=[dir stringByAppendingPathComponent:@"ABC.sqlite"];
databasepath=[[NSString alloc]initWithString:dirString];
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:databasepath]==NO)
{
const char *dbpath=[databasepath UTF8String];
if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
char *errMsg;
const char *createSQL="CREATE TABLE IF NOT EXISTS QUESTIONS(ID INTEGER PRIMARY KEY AUTOINCREMENT,QUESTIONSLIST TEXT,ANSWER1 TEXT,ANSWER2 TEXT,ANSWER3 TEXT,ANSWER4 TEXT,CORRECTANSWER TEXT)";
if (sqlite3_exec(contactDB, createSQL, NULL, NULL, &errMsg) !=SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(contactDB);
}
}
[super viewDidLoad];
}
-(void)xyz{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir=[paths objectAtIndex:0];
NSString *dirString=[dir stringByAppendingPathComponent:@"ABC.sqlite"];
databasepath=[[NSString alloc]initWithString:dirString];
const char *dbpath=[databasepath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
NSString *querySQL=[NSString stringWithFormat:@"select * from questions order by random() LIMIT 1"];
const char *query=[querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query, -1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement)==SQLITE_ROW)
{
NSString *questionField=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
questionView.text=questionField;
NSString *option1=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
[answer1 setTitle:option1 forState:UIControlStateNormal];
NSString *option2=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
[answer2 setTitle:option2 forState:UIControlStateNormal];
NSString *option3=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
[answer3 setTitle:option3 forState:UIControlStateNormal];
NSString *option4=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 5)];
[answer4 setTitle:option4 forState:UIControlStateNormal];
NSString *option5=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 6)];
correctAnswer.text=option5;
correctAnswer.hidden=NO;
[option1 release];
[option2 release];
[option3 release];
[option4 release];
[option5 release];
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
}
答案 0 :(得分:0)
NoviceDeveloper:
please tell me how to copy sqlite file to device
如果您想这样做,您需要在项目的资源中包含此文件:这样,它将被包含在应用程序包中,然后转移到您的设备。
如果您之前没有这样做,则下一步是将其复制到applicationDocumentsDirectory。水木清华。像:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"NameOfYourDatabase.sqlite"];
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *defaultStoreURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"NameOfYourDatabase" ofType:@"sqlite"]];
if (![fileManager fileExistsAtPath:[storeURL relativePath]]) {
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:&error];
}
}
if (error)
//handle it
编辑:
有功能:
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}