首先,纪录一下学习的注意点:获取地址的方式和之前的版本其实没有变化,但是要注意一点
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPath objectAtIndex:0];
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPath objectAtIndex:0];
这两段代码只差了其中一个很相似的参数,一开始的时候一直没搞懂为什么路径从之前的/documents变成了/documentation,这是很大差异的,因为暂时没有从模拟器的沙箱中找到这样一个路径,所以刚开始的时候也许会没整理好导致错误。
另外基本的操作:(增、查)剩下的删、改其实没有其他 区别了就是语句的问题了,注意bug主要会出现在sql语句的错误中,其他么就没什么注意点了。
- (IBAction)save:(id)sender {
sqlite3_stmt *statement;
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &database)==SQLITE_OK) {
if ([stuid.text isEqualToString:@""]) {
UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry! id nil" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
[av show];
}else
{
NSString * insertSQL = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",stuid.text,classname.text,name.text];
const char *insertStatement = [insertSQL UTF8String];
NSLog(@"%@",insertSQL);
sqlite3_prepare_v2(database, insertStatement, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
stuid.text=@"";
classname.text=@"";
name.text=@"";
UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"YES!Saved" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
[av show];
}
else
{
UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry!Saved failed" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil];
[av show];
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}else{NSLog(@"failed in Saving!");}
}
- (IBAction)query:(id)sender {
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
NSLog(@"query");
if(sqlite3_open(dbpath, &database)==SQLITE_OK){
NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",stuid.text];
const char *queryStatement = [querySQL UTF8String];
if (sqlite3_prepare_v2(database, queryStatement, -1, &statement, NULL)==SQLITE_OK) {
if (sqlite3_step(statement)==SQLITE_ROW) {
NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)];
classname.text = classnameField;
NSString *nameField = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
name.text = nameField;
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"we" message:@"query good" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *one =[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:nil];
[controller addAction:one];
[self presentViewController:controller animated:YES completion:nil];
}else{
NSLog(@"NONONONONO");
}
sqlite3_finalize(statement);
}else{NSLog(@"tag1");}
sqlite3_close(database);
}
}
- (IBAction)clear:(id)sender {
stuid.text = @"";
classname.text = @"";
name.text = @"";
}