2013-12-11 220 views
0

我是新來的sqlite,我已經兩天試圖遵循教程,但它不想添加我的數據庫和表。它給了我如果沒有添加表格的警告。 「TABLE還沒有創建」Sqlite創建數據庫

我沒有得到任何xcode錯誤,所以它可能是別的比我猜這個代碼。有人有什麼建議嗎?

viewcontroller.h

@property (retain, nonatomic) NSString *databaseName, *tableName; 
@property (readwrite, nonatomic) int numberOfRows; 
@property (readwrite, nonatomic) NSMutableArray *dataList; 
@property (readwrite, nonatomic) BOOL table_ok, db_open_status; 
@property (retain, nonatomic) NSArray *my_columns_names; 

viewcontroller.h

[super viewDidLoad]; 
dataList = [[NSMutableArray alloc]init]; 
numberOfRows = 0; 
databaseName = @"mysampledatabase"; 
tableName = @"mypeople"; 
db_open_status = NO; 
table_ok = NO; 
my_columns_names = [[NSArray alloc]initWithObjects:@"hometeam",@"awayteam",@"homeplayers",@"homefouls",@"awayplayers",@"awayfouls",@"period",@"time",@"homescore",@"awayscore", nil]; 
if ([self openDBWithSQLName:databaseName]) { 
    db_open_status = YES; 
    if (![self createTable:tableName WithCoulumns:my_columns_names]) { 
     UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"the TABLE has not been created" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; 
     [av show]; 
    } else { 
     table_ok = YES; 
    } 
} 


-(BOOL)openDBWithSQLName:(NSString *)sqlname{ 
    BOOL is_Opened = NO; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *my_sqlfile = [[paths objectAtIndex:0] stringByAppendingPathComponent:sqlname]; 
    if (sqlite3_open([my_sqlfile UTF8String], &dbname) ==SQLITE_OK) { 
     is_Opened = YES; 
    } 
    return is_Opened; 

} 


-(BOOL)createTable:(NSString *)tablename WithCoulumns:(NSArray *)columnNames{ 
    BOOL has_beencreated = NO; 
    NSString *fieldset = @""; 
    char *err; 
    for (int a=0; a<[columnNames count]; a++){ 

     NSString *columnSet = [NSString stringWithFormat:@"'%@' TEXT", [columnNames objectAtIndex:a]]; 
     fieldset = [fieldset stringByAppendingString:columnSet]; 
     if (a<([columnNames count]-1)) { 
      fieldset = [fieldset stringByAppendingString:@" ,"]; 
     } 

    } 
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (ID INTEGER PRIMARY KEY AUTOINCREMENT,%@", tableName, fieldset]; 
    if (sqlite3_exec(dbname, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) { 
     sqlite3_close(dbname); 
    } else { 
     has_beencreated = YES; 
    } 
    return has_beencreated; 
} 
+0

createTable的方法是什麼:WithCoulumns:和openDBWithSQLName:?展示他們。 –

+0

哦,對不起,我忘了。幹得好! – user3088011

回答

0

我想我找到了與問題「表尚未創建」您收到消息,(我這樣做,以及!)。在CREATETABLE:只是* SQL分配方法之前,如果你添加以下代碼: fieldset=[fieldset stringByAppendingString:@")"];

的fiedlset滿足與它錯過一個右括號。 您可以通過將一個斷點略低於此,在檢查自己:

has_beencreated = YES; 

運行應用程序和檢查變量& ERR

希望它能幫助!