我正在尝试将字符串存储在iOS 6 iPhone应用程序的SQLite数据库中。这很简单:单击按钮时,文本视图中会显示一个笑话。单击第二个按钮时,我想将该textview保存到SQLite数据库(saveJoke)中。但是,永远不会返回SQLITE_DONE,表明这不起作用。所以,看看我的警报,当下面执行saveJoke时,我总是得到“失败”。
知道为什么这不起作用?我有一种感觉,我可能会遗漏创建和插入SQLite数据库的基本内容。非常感谢您的帮助!
我的代码:
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
答案 0 :(得分:1)
您还必须检查INSERT查询,您的stringFormat是错误的:
变化:
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (jokesaved) VALUES (?)", jokeSaved];
为:
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO CONTACTS (jokesaved) VALUES ('%@')", jokeSaved];
答案 1 :(得分:1)
if (insert_statement == nil)
{
const char * sql = "INSERT INTO CONTACTS (jokesaved) VALUES (?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: Failed to prepare SQL statement: %s.",sqlite3_errmsg(database));
}
}
@try
{
sqlite3_bind_text (insert_statement, 1, [jokesSaved UTF8String], -1, SQLITE_TRANSIENT );
}
@catch (NSException *exception)
{
;
}
int val = sqlite3_step(insert_statement);
if ( val != SQLITE_DONE)
{
NSAssert1(0, @"Failed to prepare SQL property insert statement: %s.", sqlite3_errmsg(database));
}
else
{
}