>
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *diretorioPaths;
NSString * dir;
diretorioPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
dir = [diretorioPaths objectAtIndex:0];
_databasePath = [[NSString alloc] initWithString: [dir
stringByAppendingPathComponent:@"carlosaguiar.db"]];
NSFileManager *file = [NSFileManager defaultManager];
if([file fileExistsAtPath: _databasePath] == NO ){
const char *dbPath = [_databasePath UTF8String];
if(sqlite3_open(dbPath, &_db) == SQLITE_OK){
char *error_msg;
const char *sql_stm = "CREATE TABLE IF NOT EXISTS clientes(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, email TEXT)";
if(sqlite3_exec(_db, sql_stm, NULL, NULL, &error_msg) == SQLITE_OK){
NSLog(@"Sucesso!");
}else{
NSLog(@"Erro!");
}
sqlite3_close(_db);
}
}
}
- (void)didReceiveMemoryWarning{
// Dispose of any resources that can be recreated.
}
@end
Ola Carlos,
vc viu o curso de ios basico?
vc fez igual ao curso??
se naum fez faça igual a esse site aqui
http://klebermota.eti.br/2011/11/10/implementando-um-banco-de-dados-no-ios-4-do-iphone-usando-sqlite/
Agora sim. Valeu mesmo.
Agora o buttom Cadastrar naum funciona.
ViewContoller.h
#import
#import
@interface ViewController : UIViewController
@property (weak, nonatomic)
IBOutlet UITextField *textNome;
@property (weak, nonatomic)
IBOutlet UITextField *textEmail;
- (IBAction)save:(id)sender;
@property NSString *databasePath;
@property sqlite3 *db;
@end
View Controller.m
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender {
const char *dbPath = [_databasePath UTF8String];
if(sqlite3_open(dbPath, &_db) == SQLITE_OK){
NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO clientes (nome, email) VALUES(\"%@\", \"%@\")", _textNome.text, _textEmail.text];
const char *sql_insert = [sqlInsert UTF8String];
sqlite3_stmt *sql_statement;
sqlite3_prepare_v2(_db, sql_insert, -1, &sql_statement, NULL);
if (sqlite3_step(sql_statement) == SQLITE_DONE) {
_textNome.text = @"";
_textEmail.text = @"";
}
sqlite3_finalize(sql_statement);
sqlite3_close(_db);
}
}
@end
Carlos
Por favor, poste o erro??