我是iOS新手。 目前我正在尝试使用xcode6 obj C和sqlite3构建我自己的应用程序。
我能够打开数据库,创建表,但无法更新。你能帮帮我吗?代码如下
@interface ViewController : UIViewController
{
sqlite3 *db;
}
- (void)viewDidLoad {
[self openDB];
[self createTable:@"custadd" withField1:@"ID" withField2:@"Name" withField3:@"Phone"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//to create table
-(void) createTable:(NSString *) tableName
withField1: (NSString*) field1
withField2: (NSString*) field2
withField3: (NSString*) field3
{
char *err;
NSString *sql = [NSString stringWithFormat:@"Create table if not exists '%@' ('%@' char primary key, '%@' char, '%@' char not null );",tableName,field1,field2,field3];
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Could not create table");
}else{
NSLog(@"Table created");
}
}
- (IBAction)saveButton:(id)sender {
// NSString *sql = [NSString stringWithFormat:@"INSERT INTO custadd('ID','Name','Phone') Values ('%@','%@','%@']",custid,name,phone];
// NSString *sql = @"INSERT INTO custadd(1,ose,012345)";
NSString *sql = @".table";
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK ){
// sqlite3_close(custadd);
// NSAssert(0, @"Could not update table");
NSLog(@"table not updated");
} else{
NSLog(@"table updated");
}
}