2011-05-11 58 views
0

這是我爲了訪問iPhone上的SQLite數據庫而編寫的代碼。應用程序因某種原因崩潰。任何人都可以幫助我嗎?sqlite iPhone的SDK的幫助?

-(void)openDB{ 
    //create database 
    if (sqlite3_open([[self filPath] UTF8String],&db) != SQLITE_OK) { 
     sqlite3_close(db); 
     NSAssert(0, @"database failed to open"); 
    } 
} 


-(void)insertRecordIntoTableNamed:(NSString *)tableName withField1:(NSString *)field1 field1Value:(NSString*)field1Value andField2:(NSString *)field2 field2Value:(NSString *)field2Value { 

    // [self insertRecordIntoTableNamed:@"Contacts" withFiled1:@"email" field1Value:email andField2:@"name" field2Value:name]; 
    NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@' , '%@') VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value]; 
    char *err; 
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) { 
     sqlite3_close(db); 
     NSAssert(0,@"error updating table."); 
    } 
} 

-(void)createTableNamed:(NSString *) tableName withField1:(NSString *)field1 withField2:(NSString *)field2 { 
    char *err; 
    NSString *sql = [NSString stringWithFormat:@"Creat table if not exist '%@' ('%@' text prmaru key, '%@' TEXT",tableName,field1,field2]; 
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) { 
     sqlite3_close(db); 
     NSAssert (0,@"tabel failed to create"); 
    } 
}  

-(void)getAllRowsFromTableNamed:(NSString *)tableName{ 
    NSString *qsql = [NSString stringWithFormat:@"select * from %@",tableName]; 
    sqlite3_stmt *statment; 

    if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statment, nil) == SQLITE_OK) { 
     while (sqlite3_step(statment) == SQLITE_ROW) { 
      char *field1 = (char *)sqlite3_column_text(statment, 0); 
      NSString *field1Str = [[NSString alloc] initWithUTF8String: field1]; 
      char *field2 = (char *)sqlite3_column_text(statment, 1); 
      NSString *field2Str = [[NSString alloc] initWithUTF8String: field2]; 
      NSString *str = [[NSString alloc] initWithFormat:@"%@ - %@", field1Str, field2Str]; 
      NSLog(@"%@", str); 
      //  [field1Str release]; 
      //  [field2Str release]; 
      //  [str release]; 
     }  
     sqlite3_finalize(statment); 
    } 
} 

- (void)viewDidLoad { 
    [self openDB]; 
    NSLog(@"%@",[self filPath]); 
    [self createTableNamed:@"Contacts" withField1:@"email" withField2:@"name"]; 

    for (int i = 0; i<= 2;i++) { 
     NSString *email = [[NSString alloc]initWithFormat:@"user%[email protected]",i];  
     NSString *name = [[NSString alloc]initWithFormat:@"user %i",i]; 
     [self insertRecordIntoTableNamed:@"Contacts" withField1:@"email" field1Value:email andField2:@"name" field2Value:name]; 
     [email release]; 
     [name release]; 
    } 

    [self getAllRowsFromTableNamed:@"Contacts"]; 
    sqlite3_close(db); 
    [super viewDidLoad]; 
} 
+2

只有受虐者直接在Objective-C中使用SQLite C API。 [使用FMDB](http://github.com/ccgus/fmdb)(一個SQLite包裝器)或[CoreData](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/ cdProgrammingGuide.html)(一個對象圖管理器)。 – 2011-05-11 22:20:20

+0

我不明白的核心數據我想sql的兼容性問題,因爲3.0以下的ios不支持核心數據請看看問題謝謝 – 2011-05-11 22:22:53

回答

3

的問題,我看到:

  • [NSString stringWithFormat:@"Creat table if not exist '%@' ('%@' text prmaru key, '%@' TEXT",tableName,field1,field2];

    應該

    [NSString stringWithFormat:@"Create table if not exists %@ ('%@' text primary key, '%@' TEXT)",tableName,field1,field2];

    一樣,你真正閱讀the documentation on SQLite syntax?拼寫很重要。

  • NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@' , '%@') VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value];

    應該

    NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@ , %@) VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value];

    你不把單引號括起來的東西,除非你打算它被解釋爲一個字符串。

  • 你註釋掉這些行:

    //  [field1Str release]; 
    //  [field2Str release]; 
    //  [str release]; 
    

    他們不應該。

我不知道修復這些東西是否能夠真正解決您的崩潰問題,但他們肯定是錯誤的。


除此之外,直接在Objective-C中使用SQLite API是非常愚蠢的。沒有理由,因爲有很多方法可以做到這一點。

  1. CoreData。這是Apple創建的一個對象圖框架,使您可以輕鬆創建和保存對象。這不是每個人都說的數據庫,但它通常表現得像一個。如果你可以使用這個,我強烈推薦它。這絕對不是一件容易理解的事情,但它使得一些事情變得更加困難,並且讓事情變得更容易。

  2. 如果由於某種原因您必須與現有的SQLite數據庫進行交互,您應該通過FMDB來完成。它是SQLite C API的封裝。換句話說,Gus已經習慣了學習SQLite API的所有麻煩,所以你不必。你會做這樣的事情:

    FMDatabase *db = [[FMDatabase alloc] initWithPath:[self filePath]]; 
    if (![db open]) { 
        [db release]; 
        db = nil; 
    } 
    
    //create a table: 
    [db executeUpdate:@"create table if not exists Customers (email text primary key, name text)"]; 
    
    //insert into a table: 
    [db executeUpdate:@"insert into Customers (email, name) values (?, ?)", @"[email protected]", @"user1"]; 
    
    //select from a table: 
    FMResultSet *results = [db executeQuery:@"select * from Customers"]; 
    while ([results next]) { 
        NSLog(@"%@ - %@", [results stringForColumn:@"email"], [results stringForColumn:@"name"]); 
    } 
    
    //close the database: 
    [db close]; 
    [db release]; 
    

是不是約bajillion倍,你在做什麼更容易?