2009-06-22 8 views
0

J'essaie de faire une application de base.
liste Mes fichiers:iPhone - Base de données SQLite Lecture

DatabaseClass.h and .m, 
IDRGameAppDelegate.h and .m, 
scoredb.sqlite, 
ScoreViewController.h and .m, 
MainWindow.xib // Main Window 
ScoreWindow.xib //Show Score Window 

1.) ma base de données est;

CREATE TABLE "game" ("id" INTEGER PRIMARY KEY ,"name" VARCHAR(32),"score" VARCHAR(20)); 
INSERT INTO "game" VALUES(1,'interclock','1.234'); 
INSERT INTO "game" VALUES(2,'dui','345'); 
INSERT INTO "game" VALUES(3,'reflex','987'); 

2-) i créé DatabaseClass.h et DatabaseClass.m

DatabaseClass.h est:

#import <Foundation/Foundation.h> 
@interface DatabaseClass : NSObject { 

    NSString *name; 
    NSString *score; 
} 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *score; 
-(id)initWithName:(NSString *)n score:(NSString *)s; 
@end 

DatabaseClass.m est;

#import "DatabaseClass.h" 
@implementation DatabaseClass 
@synthesize name, score; 
-(id)initWithName:(NSString *)n score:(NSString *)s{ 
    self.name = n; 
    self.score = s; 
    return self; 
} 
@end 

mon IDRGameAppDelegate.h est;

#import <UIKit/UIKit.h> 
#import <sqlite3.h> // Import the SQLite database framework 

@class IDRGameViewController , ScoreViewController ; 


@interface IDRGameAppDelegate : NSObject <UIApplicationDelegate> { 

    IBOutlet UIWindow *window; 
    IBOutlet IDRGameViewController *viewController; 
    IBOutlet ScoreViewController *scoreViewController; 


    // Database variables 
    NSString *databaseName; 
    NSString *databasePath; 

    // Array to store the animal objects 
    NSMutableArray *scores; 
} 

- (void)flipToScore; 
- (void)scoreToMainBack; 

-(void) checkAndCreateDatabase; 
-(void) readScoreFromDatabase; 

@property (nonatomic, retain) UIWindow *window; 
@property (nonatomic, retain) IDRGameViewController *viewController; 
@property (nonatomic, retain) ScoreViewController *scoreViewController; 
@property (nonatomic, retain) NSMutableArray *scores; 

@end 

IDRGameAppDelegate.m est;

#import "IDRGameAppDelegate.h" 
#import "IDRGameViewController.h" 
#import "ScoreViewController.h" 
#import "DatabaseClass.h" 


@implementation IDRGameAppDelegate 

@synthesize window; 
@synthesize viewController; 
@synthesize scoreViewController; 

@synthesize scores; 



- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    // Setup some globals 
    databaseName = @"scoredb.sqlite"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 

    // Execute the "checkAndCreateDatabase" function 
    [self checkAndCreateDatabase]; 

    // Query the database for all animal records and construct the "animals" array 
    [self readScoreFromDatabase]; 

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 

- (void)dealloc { 
    [scores release]; 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 


-(void) checkAndCreateDatabase{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

    [fileManager release]; 
} 


-(void) readScoreFromDatabase { 
    // Setup the database object 
    sqlite3 *database; 

    // Init the animals Array 
    scores = [[NSMutableArray alloc] init]; 

    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = "select * from game"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 



      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 

       NSString *aName =[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSString *aScore =[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 3)]; 

       //NSString *aName = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 2)]; 
       //NSString *aScore = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 3)]; 



       // Create a new animal object with the data from the database 
       DatabaseClass *dbOBJ = [[DatabaseClass alloc] initWithName:aName score:aScore]; 

       // Add the animal object to the animals Array 
       [scores addObject:dbOBJ]; 

       [dbOBJ release]; 
      } 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database); 

} 


- (void)flipToScore { 


    ScoreViewController *aSecondView = [[ScoreViewController alloc] initWithNibName:@"ScoreWindow" bundle:nil]; 
    [self setScoreViewController:aSecondView]; 
    [aSecondView release]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:2.0]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES]; 
    [viewController.view removeFromSuperview]; 
    [self.window addSubview:[scoreViewController view]]; 
    [UIView commitAnimations]; 

} 

- (void)scoreToMainBack { 



    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:2.0]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:NO]; 


    [scoreViewController.view removeFromSuperview]; 
    [self.window addSubview:[viewController view]]; 

    [UIView commitAnimations]; 


    [scoreViewController release]; 
    scoreViewController = nil; 

} 
@end 

ScoreViewController.h est;

#import <UIKit/UIKit.h> 

@interface ScoreViewController : UIViewController { 

    IBOutlet UILabel *gameNameLabel; 
    IBOutlet UILabel *gameScoreLabel; 


} 
@property (nonatomic, retain) IBOutlet UILabel *gameNameLabel; 
@property (nonatomic, retain) IBOutlet UILabel *gameScoreLabel; 

-(IBAction)refreshClick:(id)sender; 

@end 

et enfin ScoreViewController.m est;

#import "ScoreViewController.h" 
#import "DatabaseClass.h" 
#import "IDRGameAppDelegate.h" 

@implementation ScoreViewController 

@synthesize gameNameLabel,gameScoreLabel; 


/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

-(IBAction)refreshClick:(id)sender { 

    // Navigation logic -- create and push a new view controller 
    IDRGameAppDelegate *appDelegate = (IDRGameAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    DatabaseClass *dbOBJ = (DatabaseClass *)[appDelegate.scores objectAtIndex:0]; 



    gameNameLabel.text = [dbOBJ name]; 
    gameScoreLabel.text = [dbOBJ score]; 


} 




- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

Le problème est: lorsque je clique sur refreshButton, le programme se bloque et se casse, mais je ne vois pas d'erreur.

Le programme ne fonctionne pas Je n'ai pas lu et affiché les enregistrements de la base de données. Quel est le problème?

Répondre

1

Pourquoi avez-vous décidé de coller directement avec SQLite sur l'iPhone? Après iPhone OS 3.0 est disponible pour tous les utilisateurs d'iPhone, je recommande d'utiliser cette abstraction de base de données. Il est assez facile à utiliser et il vous empêche de coder un tas de code d'accès à la base de données.

Mike Swans blog est une introduction utile aux données de base, qui peut servir de point de départ pour votre développement.

Dans le cadre de la documentation de référence de l'iPhone, Apple a publié un Core Data Tutorial for the iPhone OS qui couvre les fonctionnalités spécifiques de l'iPhone.

Je l'ai utilisé seul dans une application et je l'ai trouvé vraiment plus rapide que le code de base de données SQLite que j'avais auparavant.

+0

Core Data est livré avec des bagages de performance à l'air. Pour certaines applications, SQLite est le meilleur choix. http://inessential.com/2010/02/26/on_switching_away_from_core_data –

Questions connexes