2012-01-03 11 views
0

私はテーブルにデータを挿入する方法を知っている 私は別のアプリケーションでこれは多くの時間を試してみてください完璧だが、私は新しいデータベースとテーブルを作成します。私はボタンを追加呼び出すときに、私は、この保存エラーコンソールにエラーが表示されます。どのようにiphoneのsqliteデータベースの日付を保存する

no such table: bit

、これはあなたが

static sqlite3_stmt *addStmt = nil; 

を宣言する必要が

#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]; 


} 
+1

SQLiteに日付を保存することができます。しかし、文字列 –

+0

として保存する方が良いでしょう。どの時点で "ビット"テーブルを作成しますか?あなたは "ビット"テーブルの作成ステートメントを投稿できますか? – CarlJ

答えて

0

私の事私のsqliteのコードです@implementation classNameの上にあります。

を使用すると、内部の次のステートメントを追加する必要があります)(ADDに

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

その後、追加した場合はブロック

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

私はそれはあなたを助けるだろうと思います。

+0

すでに彼はこれをして、あなたが彼を送ってくる答えを... :( – Tirth

0

"insert into bit(title)VALUES(?)"と出力 "No such table:bit"を見て、あなたのコードのどこにでもテーブルが定義されていないと推測できます。データを挿入する前にテーブルを作成する必要があります。

ちょっとした提案、コアデータを使用しないのはなぜですか? Core Dataテンプレートを使用する新しいプロジェクトを作成すると、Core Dataを使用してアプリケーションを作成するサンプルコードが表示されます。

関連する問題