簡體   English   中英

無法將字符串保存到SQLite數據庫(SQLITE_DONE不返回true)

[英]Can't save string into SQLite database (SQLITE_DONE doesn't return true)

我正在嘗試將字符串存儲在iOS 6 iPhone應用程序的SQLite數據庫中。 這非常簡單:單擊按鈕時,在文本視圖中會顯示一個笑話。 當單擊第二個按鈕時,我想將該文本視圖保存到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];
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 
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM