SQLiteデータベースをiPadアプリに接続しました。ここで、データベースには2つのテーブルがあります。1つのテーブルはデータを入力し、他のテーブルはアプリで指定されたデータを保存します。しかし、問題は、データベースが更新されてデータが保存されていないことです。たとえば、テーブルに新しい名前を追加すると、自動的に入力されず、別の名前でdbファイルを再度追加した場合にのみ入力されます。保存したいデータを保存していません。
貴重なご提案をお願いします
参考までに私のコードを見つけてください:
#import <UIKit/UIKit.h>
#import "PersonDatabase.h"
#import "sqlite3.h"
#import <sqlite3.h>
@interface ViewController : UIViewController<UITextViewDelegate>{
PersonDatabase *perDas;
NSString * nameString;
NSString * statusString;
sqlite3 *database;
NSString * empString;
}
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIButton *signIn;
@property (strong, nonatomic) IBOutlet UIButton *signOut;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet NSString * nameString;
@property (strong, nonatomic) IBOutlet NSString * statusString;
-(IBAction)signin:(id)sender;
-(IBAction)signout:(id)sender;
@end
#import "ViewController.h"
#import "PersonInfo.h"
#import "PersonDatabase.h"
@implementation ViewController
@synthesize tableView;
@synthesize signIn;
@synthesize signOut;
@synthesize nameLabel;
@synthesize nameString,statusString;;
- (void)viewDidLoad
{
[super viewDidLoad];
if (perDas == nil) {
perDas = [[PersonDatabase alloc] init];
}
[tableView setDataSource:perDas];
[tableView setDelegate:perDas];
perDas.view = perDas.tableView;
}
-(IBAction)signin:(id)sender{
NSLog(@"%@",perDas.string1);
nameString=[[NSString alloc]initWithString:perDas.string1];
statusString=[[NSString alloc]initWithFormat:@"IN"];
{
NSLog(@"passed");
sqlite3_stmt *statement;
const char *dbpath = [perDas.databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(@"pass");
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO status (names, staus, signin, singnout) VALUES (\"%@\", \"%@\", datetime(), \"%@\")", nameString, statusString,empString];
NSLog(@"%@",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
int results= sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
NSLog(@"%i", results);
if(sqlite3_step(statement) == SQLITE_DONE){
NSLog(@"done");
}else {
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}}
-(IBAction)signout:(id)sender{
NSLog(@"%@",perDas.string1);}
- (void)viewDidUnload
{
[self setTableView:nil];
[self setSignIn:nil];
[self setSignOut:nil];
[self setNameLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
#import <Foundation/Foundation.h>
@interface PersonInfo : NSObject{
int unique;
NSString * nam;
}
@property(nonatomic, assign)int unique;
@property(nonatomic, copy)NSString * nam;
-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name;
@end
#import "PersonInfo.h"
@implementation PersonInfo
@synthesize unique,nam;
-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name{
self=[super init];
if (self) {
self.unique=uniqueId;
self.nam=name;
}
return self;
}
@end
#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import <sqlite3.h>
#import "PersonInfo.h"
@interface PersonDatabase : UITableViewController<UITableViewDataSource>{
NSString *databaseName;
NSString * databasePath;
NSMutableArray *tableOne;
NSString * string1;
NSString * dateString;
NSString * timeString;
sqlite3 *database;
NSString * empString;
}
@property(nonatomic, retain) NSMutableArray *tableOne;
@property(nonatomic, retain) NSString * string1;
@property(nonatomic, retain) NSString * databasePath;
-(void)checkAndCreateDatabase;
-(void)readDataFromDatabase;
@end
#import "PersonDatabase.h"
#import "PersonInfo.h"
#import "ViewController.h"
@implementation PersonDatabase
@synthesize tableOne, string1,databasePath;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
databaseName=@"log.sqlite3";
NSArray *documentPaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDir = [documentPaths objectAtIndex:0];
databasePath=[documentDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readDataFromDatabase];
[super viewDidLoad];
NSLog(@"a");
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - TableView Data Source methods
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tableOne count]; }
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell= nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];}
PersonInfo * temp =(PersonInfo *)[self.tableOne objectAtIndex:indexPath.row];
cell.textLabel.text=temp.nam;
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
string1 = cell.textLabel.text;
NSLog(@"%@",string1);
}
-(void)checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success=[fileManager fileExistsAtPath:databasePath];
if(success)
return;
NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)readDataFromDatabase{
tableOne=[[NSMutableArray alloc]init];
if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
const char *sqlStatement = "SELECT * FROM names";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){
while (sqlite3_step(compiledStatement)==SQLITE_ROW) {
int uniqueId = sqlite3_column_int(compiledStatement, 0);
char *nameChars=(char *)sqlite3_column_text(compiledStatement,1);
NSString *name=[[NSString alloc]initWithUTF8String:nameChars];
PersonInfo *info=[[PersonInfo alloc]initWithUniqueId:uniqueId name:name];
[tableOne addObject:info];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
@end
前もって感謝します!!!