I'm trying to store strings in a SQLite database for an iOS 6 iPhone Application. It's fairly simple: a joke is displayed in a textview when clicking a button. When clicking a second button, I want to save that textview into the SQLite database (saveJoke). However, SQLITE_DONE is never returned, indicating this is not working. So, looking at my alerts, I always get "Fail" when saveJoke is executed below.
Any idea why this is not working? I have a feeling I might be missing something basic in the creation of and inserting into the SQLite database. Very thankful for help!
My code:
JokeFirstViewController.m:
#import "JokeFirstViewController.h"
@interface JokeFirstViewController ()
@end
@implementation JokeFirstViewController
@synthesize joke = _joke;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
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, JOKESAVED TEXT)"; //look into
sqlite3_close(_contactDB);
}
}
}
- (IBAction)saveJoke:(id)sender {
/* get current joke displayed */
self.joke = self.text.text;
NSString *currentJoke = self.joke;
NSString *jokeSaved = [[NSString alloc] initWithFormat:currentJoke];
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (jokesaved) VALUES (?)",
jokeSaved];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
void AlertWithMessage(NSString *message);
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Database" message:@"Success" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
} else {
void AlertWithMessage(NSString *message);
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Database" message:@"Fail" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
}
JokeFirstViewController.h:
#import <UIKit/UIKit.h>
#import <MessageUI/MFMessageComposeViewController.h>
#import <sqlite3.h>
@interface JokeFirstViewController : UIViewController <MFMessageComposeViewControllerDelegate>
- (IBAction)saveJoke:(id)sender;
- (IBAction)shareJoke:(id)sender;
- (IBAction)generateJoke:(id)sender;
@property (strong, nonatomic) IBOutlet UITextView *text;
@property (copy, nonatomic) NSString *joke;
@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic) sqlite3 *contactDB;
@end
sqlite3_prepare_v2and make sure that'sSQLITE_OK. And if not, look atsqlite3_errmsg()to get a nice explanation of why it failed.sqlite3_execthat create table SQL if you want it to create the table. And you really should always check the result codes for allsqlite3function calls, and if they fail, do theNSLog(@"%s: %s", __FUNCTION__, sqlite3_errmsg(contactDB));