簡體   English   中英

如何在iPhone的sqlite數據庫中保存日期

[英]how to save date in sqlite data base in iphone

我知道如何在表中插入數據,我在不同的應用程序上嘗試了很多次,效果很好,但是當我創建新的數據庫和表時。 當我調用添加按鈕時,我在控制台上收到此保存錯誤的錯誤消息:

沒有這樣的表:位

這是我的sqlite代碼

#import "untitled.h"
#import<sqlite3.h>
#define DATABASE_NAME @"hello.sqlite"

@implementation untitled
@synthesize enter;


- (NSString *) getDBPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSLog(@"*****##:%@",documentsDir);
    return [documentsDir stringByAppendingPathComponent:DATABASE_NAME];
}

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    NSLog(@"*****:%@",dbPath);
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_NAME];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }


}

-(void)Add
{
    NSString *filePath = [self getDBPath];
    NSLog(@"this check:%@",filePath);
    sqlite3 *database;

    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "insert into bit(Title) VALUES (?)";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {


            sqlite3_bind_text( compiledStatement, 1, [enter.text UTF8String], -1, SQLITE_TRANSIENT);



        }
        if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
            NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
        }
        else {
            sqlite3_reset(compiledStatement);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            alert = nil;

        }

        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

-(IBAction)sumit{

    [self Add];


}

我要你聲明

static sqlite3_stmt *addStmt = nil; 

在@implementation className的上方。

+ (void) finalizeStatements 
{ 
if (database) sqlite3_close(database); 
if (addStmt) sqlite3_finalize(addStmt);`
}

然后在ADD()中,您必須在if塊內添加以下語句

sqlite3_bind_text(addStmt, 1, [Title UTF8String], -1, SQLITE_TRANSIENT);

我認為它將為您提供幫助。

查看sql語句“ insert into bit(title)VALUES(?)”和輸出“ No such table:bit”,您可以猜測該表尚未在代碼中定義。 您必須先創建表,然后再嘗試插入數據。

只是一個建議,為什么不使用Core Data? 創建一個使用Core Data模板的新項目,您將看到有關使用Core Data創建應用程序的示例代碼。

暫無
暫無

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

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