我无法将我的XCode项目与我的SQLite3数据库相关联

时间:2013-02-18 07:05:56

标签: database xcode sqlite

#import "AddEandM.h"
#import "Questions.h"
#import "sqlite3.h"




@interface AddEandM ()

@end


@implementation AddEandM

@synthesize Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer, status;

-(IBAction) backgroundTouched:(id)sender {
    [Topic resignFirstResponder];
    [Question resignFirstResponder];
    [Answer1 resignFirstResponder];
    [Answer2 resignFirstResponder];
    [Answer3 resignFirstResponder];
    [CorrectAnswer resignFirstResponder];


}

-(IBAction) textfieldReturn:(id)sender {
    [sender resignFirstResponder];
}

- (void) saveData
{
    sqlite3 * Questions;
    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)

    {

        NSString *insertSQL = [NSString stringWithFormat:

                      @"INSERT INTO DATABASE (Topic, Question, Answer1, Answer2, Answer3, CorrectAnswer) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",Topic.text, Question.text, Answer1.text, Answer2.text, Answer3.text, CorrectAnswer.text];


    const char *insert_stmt = [insertSQL UTF8String];


     sqlite3_prepare_v2 (Questions, insert_stmt, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_DONE)

            {
            status.text = @"Question/Answers Added";
                                                    Topic.text = @" ";
                                                    Question.text = @" ";
                                                    Answer1.text = @" ";
                                                    Answer2.text = @" ";
                                                    Answer3.text = @" ";
                                                    CorrectAnswer.text = @" ";

                                        } else {

                        status.text = @"Failed to add Question/Answers";
                                                        }
                                        sqlite3_finalize(statement);
                                        sqlite3_close(Questions);


}
}






-(IBAction)Back:(id)sender {
    Questions *second = [[Questions alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:second animated:YES];

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    NSString *docsDir;
    NSArray *dirPaths;

    sqlite3 * Questions;

    dirPaths = NSSearchPathForDirectoriesInDomains(
            NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex: 0];

    databasePath = [[NSString alloc]
                    initWithString: [docsDir stringByAppendingPathComponent:
                                     @"Questions"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath:databasePath ] == NO)

    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &Questions) == SQLITE_OK)


        {

            char *errMsg;
            const char *sql_stmt = "Create Table If Not Exists Questions (QuestionNo Integer AutoIncrement, Question Text, Answer1 Text, Answer2 Text, Answer 3 Text, Correct Answer Text)";
            if (sqlite3_exec(Questions, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)

            status.text = @"Failed to Create Table";
                }

                sqlite3_close(Questions);
                } else {
    status.text  =@"Failed to open/create database";
                }





    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    @end

我正在为我的计算机科学课创建一个修订应用程序,我对Xcode / Ob C中的编程知识非常有限,任何人都可以帮助我并告诉我这里出了什么问题吗?我的数据库名为“Questions”,包含2个表:“EasyandMedium”以及“Hard”,但是,我无法将程序保存到此数据库中 - 请帮忙!

1 个答案:

答案 0 :(得分:1)

您遇到的问题究竟是什么?你知道你的fileExistsAtPath电话是否成功? sqlite3_open来电是否成功?

在Xcode中使用SQLite3有很多例子,例如I am having trouble linking my XCode Project with my SQLite3 database。我建议检查一下,因为它们是一个很好的起点。