Insert not working on SQLite iOS
I am not able to understand the problem.
- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = 开发者_JS百科[dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
}
- (IBAction) saveData{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSLog(@"enter %@",name.text);
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO EMP (name) VALUES (\"%@\")", name.text];
//NSLog(@"enter123 %@",dbpath);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
NSLog(@"enter789 ");
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Done success");
/*status.text = @"Contact added";
name.text = @"";
address.text = @"";
phone.text = @"";*/
}
else
{
NSLog(@"fail");
//status.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
use this it's help you
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){
const char *insert_stmt = "INSERT INTO EMP (name) VALUES (?)";
if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL)==SQLITE_OK){
sqlite3_bind_text( statement,1, [name.text UTF8String], -1, SQLITE_TRANSIENT);
}
if (sqlite3_step(statement) == SQLITE_DONE)
NSLog(@"You have Sucessfully saved!");
else
NSLog(@"Failed to Save!");
sqlite3_finalize(statement);
}
精彩评论