Reputation: 641
I am attempting to set up a button that will save the given fields into a SQLite DB. I was able to create the table, but the insert statement on the button click is giving me a SQLite_MISUSE error instead of giving me SQLite_OK. I am unable to figure out what the issue is in my code, please let me know if you have any clue as to why this is happening.
- (IBAction)SignUp:(id)sender {
sqlite3_stmt *statement;
const char *dbPath = [_databasePath UTF8String];
if(sqlite3_open(dbPath, &_DB) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO users(username, email, password) values(\"%@\", \"%@\", \"%@\")", _Username.text, _Email.text, _Password1.text];
const char *insert_statement = [insertSQL UTF8String];
sqlite3_prepare_v2(_DB, insert_statement, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE) {
//below creates a popup success message upon adding user to the db
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Congrats!"
message:@"You are now a member"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
//returns the text fields back to original empty state
_Username.text = @"";
_Email.text = @"";
_Password1.text = @"";
_Password2.text = @"";
}
else {
//some other error
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Some"
message:@"Other Error"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
sqlite3_close(_DB);
}
}
Upvotes: 0
Views: 2120
Reputation: 318955
You have several issues with the posted code.
sqlite3_prepare_v2
.stringWithFormat:
. Properly bind values into the query using the appropriate sqlite3_bind_xxx
functions.See https://stackoverflow.com/a/39001417/1226963 for an example that fixes these issues.
Upvotes: 1