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;
}
createTable的方法是什麼:WithCoulumns:和openDBWithSQLName:?展示他們。 –
哦,對不起,我忘了。幹得好! – user3088011