我将一个 SQLite 数据库连接到我的 ipad 应用程序。其中 db 有两个表,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
提前致谢!!!