Hi I'm have a problem with my database:
1.Files are unable to be saved inside the database itself
Whenever i try saving something inside the app itself, the data does not save inside the db file, i try looking for guides and help but still it does not work
These is my .h file
@interface feedback : UIViewController
@property (nonatomic, strong) NSString *databasePath;
@property (nonatomic) sqlite3 *feedbackDB;
@property (weak, nonatomic) IBOutlet UITextField *nameInput;
@property (weak, nonatomic) IBOutlet UITextField *phonenoInput;
@property (weak, nonatomic) IBOutlet UITextField *commentInput;
@property (weak, nonatomic) IBOutlet UILabel *status;
- (IBAction)saveData:(id)sender;
- (IBAction)searchUser:(id)sender;
my .m file
#import "feedback.h"
@interface feedback ()
@end
@implementation feedback
NSString *feedbackDB;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_feedbackDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS FEEDBACKS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PHONENO TEXT, COMMENT TEXT)";
if (sqlite3_exec(_feedbackDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_status.text = @"Failed to create table";
}
sqlite3_close(_feedbackDB);
}
else
{
_status.text = @"Failed to open/create database";
}
}
}
- (IBAction)saveData:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_feedbackDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO FEEDBACKS (name, phoneno, comment) VALUES (\"%@\",\"%@\", \"%@\")", _nameInput.text, _phonenoInput.text, _commentInput.text];
const char *insert_stmt = [insertSQL UTF8String];
if(sqlite3_prepare_v2(_feedbackDB, insert_stmt, -1, &statement, NULL)==SQLITE_OK);
if (sqlite3_step(statement) == SQLITE_DONE)
{
_status.text = @"Contact added";
_nameInput.text = @"";
_phonenoInput.text = @"";
_commentInput.text = @"";
}
else
{
_status.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(_feedbackDB);
}
}
- (IBAction)searchUser:(id)sender {
const char *dbpath = [_databasePath UTF8String]; sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_feedbackDB) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:
@"SELECT phoneno, comment FROM feedbacks WHERE name=\"%@\"", _nameInput.text];
const char *query_stmt = [querySQL UTF8String]; if (sqlite3_prepare_v2(_feedbackDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW) {
NSString *phonenoField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text( statement, 0)];
_phonenoInput.text = phonenoField;
NSString *commentField = [[NSString alloc] initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1)]; _commentInput.text = commentField;
_status.text = @"Match found"; }
else
{
_status.text = @"Match not found"; _phonenoInput.text = @""; _commentInput.text = @"";
}
sqlite3_finalize(statement); }
sqlite3_close(_feedbackDB); }
}
This is my .h and .m file that displays the information
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface FeedbackVC : UITableViewController
{
NSMutableArray *thefeedbacks;
sqlite3 * db;
}
@property(nonatomic,retain) NSMutableArray *thefeedbacks;
-(NSMutableArray *) feedbackList;
@end
.m file
#import "FeedbackVC.h"
#import "feedback1.h"
#import <sqlite3.h>
@implementation FeedbackVC
@synthesize thefeedbacks;
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[self feedbackList];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.thefeedbacks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FeedbacksCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int rowCount = indexPath.row;
feedback1 *feedback = [self.thefeedbacks objectAtIndex:rowCount];
cell.textLabel.text = feedback.name;
cell.detailTextLabel.text = feedback.phoneno;
return cell;
}
-(NSMutableArray *) feedbackList
{
thefeedbacks = [[NSMutableArray alloc] initWithCapacity:10];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"feedbacks.db"];
//NSLog(@"%@", dbPath);
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM feedbacks";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %@",sqlite3_errmsg(db));
}else
{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
feedback1 * feedback = [[feedback1 alloc] init];
feedback.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
feedback.phoneno = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
feedback.comment = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
[thefeedbacks addObject:feedback];
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
return thefeedbacks;
}
}
Aucun commentaire:
Enregistrer un commentaire