Reputation: 45
I am following the example shown in this link http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_7_Application I want my database to store a couple of columns that will hold date information such as date of birth as well some text fields. My code looks like this: The DatabaseViewController.h:
#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface DatabaseViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *surname1;
@property (strong, nonatomic) IBOutlet UITextField *surname2;
@property (strong, nonatomic) IBOutlet UITextField *telephone;
@property (strong, nonatomic) IBOutlet UITextField *email;
@property (strong, nonatomic) IBOutlet UITextField *address;
@property (strong, nonatomic) IBOutlet UITextField *bloodType;
@property (strong, nonatomic) IBOutlet UITextField *allergies;
@property (strong, nonatomic) IBOutlet UITextField *fatherName;
@property (strong, nonatomic) IBOutlet UITextField *motherName;
@property (strong, nonatomic) IBOutlet UIDatePicker *dob;
@property (strong, nonatomic) IBOutlet UIDatePicker *wedding;
@property (strong, nonatomic) IBOutlet UIDatePicker *feastDay;
@property (strong, nonatomic) IBOutlet UIDatePicker *otherDates;
@property (strong, nonatomic) IBOutlet UILabel *status;
- (IBAction)saveData:(id)sender;
- (IBAction)findDATA:(id)sender;
@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic) sqlite3 *contactDB;
@end
and the databaseviewcontroller.m look like this:
#import "DatabaseViewController.h"
@interface DatabaseViewController ()
@end
@implementation DatabaseViewController
- (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, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME1 TEXT, SURNAME2 TEXT, TELEPHONE TEXT, EMAIL TEXT, ADDRESS TEXT, BLOODTYPE TEXT, ALLERGIES TEXT, FATHERNAME TEXT, MOTHERNAME TEXT, DOB DATETIME, WEDDING DATETIME, FEASTDAY DATETIME, OTHERDATES DATETIME)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_status.text = @"Failed to create table";
}
sqlite3_close(_contactDB);
} else {
_status.text = @"Failed to open/create database";
}
}
}
- (void) saveData:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS date values (name, surname1, surname2, telephone, email, address, bloodType, allergies, fatherName, motherName, dob, wedding, feastDay, otherDATES ) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\",\"%@\", \"%d\", \"%d\",\"%d\",\"%d\")",
_name.text, _surname1.text, _surname2.text, _telephone.text, _email.text, _address.text, _bloodType.text,_allergies.text, _fatherName.text, _motherName.text, _dob.datePickerMode,_wedding.datePickerMode, _feastDay.datePickerMode, _otherDates.datePickerMode];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
_status.text = @"Contact added";
_name.text = @"";
_surname1.text = @"";
_surname2.text = @"";
_telephone.text = @"";
_email.text = @"";
_address.text = @"";
_bloodType.text = @"";
_allergies.text = @"";
_fatherName.text = @"";
_motherName.text = @"";
_dob.date = @"";
_wedding.date = @"";
_feastDay.date = @"";
_otherDates.date = @"";
} else {
_status.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
The error that appears says "incompatible pointer types assigning to NSDATE* from NSString*. So what I need to do to insert the dates? Thank you in advance and I hope everyone have a nice day!!
Upvotes: 0
Views: 1158
Reputation: 71
Register User with Sqlite Insert
(IBAction)registerButtonClick:(id)sender {
NSString *a,*b; a=_passWordTxtf.text; b=_confirmPassWordTxtf.text;
if(([_userNameTxtf.text isEqualToString:@""])||([_userEmailTxtf.text isEqualToString:@""])||([_passWordTxtf.text isEqualToString:@""]||([_confirmPassWordTxtf.text isEqualToString:@""]))) { UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Please Fill All Details" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[infoAlert show];
} else { if ([a isEqualToString:b]) {
NSArray *dir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath=[NSString stringWithFormat:@"%@/userInfoDb.sqlite",[dir lastObject]];
sqlite3 *db;
if (sqlite3_open([dbPath UTF8String],&db)==SQLITE_OK)
{
NSString *query=[NSString stringWithFormat:@"insert into user values(\"%@\",\"%@\",\"%@\")",_userNameTxtf.text,_userEmailTxtf.text,_passWordTxtf.text];
const char *q=[query UTF8String];
if (sqlite3_exec(db, q, NULL, NULL, NULL)==SQLITE_OK)
{
NSLog(@"User registred successfully");
}
else
{
NSLog(@"User registration failed");
}
}
else
{
NSLog(@"Db Open Falied while saving");
}
sqlite3_close(db);
UIAlertView *sucessAlert = [[UIAlertView alloc] initWithTitle:@"Sucessfull" message:@"User is Registered" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[sucessAlert show];
_userNameTxtf.text=@"";
_userEmailTxtf.text=@"";
_passWordTxtf.text=@"";
_confirmPassWordTxtf.text=@"";
}
else
{
UIAlertView *registerAlert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Password Doesn't Match" delegate:self cancelButtonTitle:@"Re Enter" otherButtonTitles:nil];
[registerAlert show];
_passWordTxtf.text=@"";
_confirmPassWordTxtf.text=@"";
}
}
}
Upvotes: 0
Reputation: 212
I think you could use this method [[NSDate date] timeIntervalSince1970];
and store date as double in database.
Also you are trying to assign an empty NSString @""
to NSDate property:
_dob.date = @"";
_wedding.date = @"";
_feastDay.date = @"";
_otherDates.date = @"";
This is incorrect, if you need to reset these values you should set them to nil
or to specific NSDate
value, for example [NSDate date];
Try to change it to the code below:
_dob.date = nil;
_wedding.date = nil;
_feastDay.date = nil;
_otherDates.date = nil;
Upvotes: 1