iphone - 无法将字符串保存到 SQLite 数据库中(SQLITE_DONE 不返回 true)

标签 iphone ios objective-c cocoa-touch

我正在尝试将字符串存储在 SQLite 数据库中以用于 iOS 6 iPhone 应用程序。这相当简单:当点击一个按钮时,一个笑话会显示在 TextView 中。单击第二个按钮时,我想将该 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

最佳答案

您还必须检查 INSERT 查询,您的 stringFormat 是错误的:

改变:

NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO CONTACTS (jokesaved) VALUES (?)", jokeSaved];

到:

NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO CONTACTS (jokesaved) VALUES ('%@')", jokeSaved];

关于iphone - 无法将字符串保存到 SQLite 数据库中(SQLITE_DONE 不返回 true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16689311/

相关文章:

iphone - 核心数据存储损坏

ios - 在很短的时间后多次播放音频文件?

objective-c - 重新加载 Root View Controller

iphone - 自定义 UITabbarController

ios - 从另一个应用程序打开“设置”应用程序

iphone - 新的 ViewControllers 导致黑屏

android - libgdx 还是 playn?

ios - Linphone iphone G729 在设置界面启用但在拨号界面不启用

iOS 通用应用架构

iphone - 多个 View Controller 中的 iAd 示例