網上隨便下分DEMO,把FMDB框架下載下來,導入libsqlite3.tbd動態庫文件。sql
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; NSString *path = [document stringByAppendingPathComponent:@"Student.sqlite"]; NSLog(@"%@",path); self.paths = path; //注意以上三句話是獲取數據庫路徑必不可少的,在viewDidload以前就已經準備好了
建立數據表數據庫
- (void)createTable{ NSFileManager *fileManager=[NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:self.paths]) { NSLog(@"表不存在,建立表"); FMDatabase *db=[FMDatabase databaseWithPath:self.paths]; if ([db open]) { NSString *sql = @"CREATE TABLE 'Student'('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'name' VARCHAR(20),'age' VARCHAR(20),'idcode' VARCHAR(30),'sex' VARCHAR(20)) ";//sql語句 BOOL success = [db executeUpdate:sql]; if (!success) { NSLog(@"error when create table "); }else{ NSLog(@"create table succeed"); } [db close]; } else{ NSLog(@"database open error"); } } }
獲取表數據框架
/** * @author aiqing, 17-11-20 11:11:23 * * //從數據庫中得到全部數據 */ - (void)getAllDatabase { FMDatabase *db = [FMDatabase databaseWithPath:self.paths]; if ([db open]) { NSString *sql = @"SELECT * FROM Student"; FMResultSet *rs = [db executeQuery:sql]; while ([rs next]) { NSString *name = [rs stringForColumn:@"name"]; NSString *age = [rs stringForColumn:@"age"]; NSString *ID = [rs stringForColumn:@"idcode"]; NSString *sex=[rs stringForColumn:@"sex"]; [self.nameArray addObject:name]; [self.ageArray addObject:age]; [self.IDArray addObject:ID]; [self.sexArray addObject:sex]; } [[AQDataFromDataBase shareFromDataBase].nameArrayFromClass arrayByAddingObjectsFromArray:self.nameArray]; NSLog(@"self.nameArray==%@",self.nameArray); [db close]; [self.tableView reloadData]; } }
添加東西到數據庫code
- (void)addNewUserInfo { FMDatabase *db = [[FMDatabase alloc]initWithPath:self.dbPath]; //打開數據庫表 if ([db open]) { if (_nameTextField.text.length == 0||_ageTextField.text.length == 0||_IDTextField.text.length == 0||_sexTextField.text.length==0){ [XFNotices noticesWithTitle:@"請完成填寫信息" Time:1.5 View:self.view Style:(XFNoticesStyleFail)]; }else{ NSLog(@"姓名==%@,年齡==%@,ID==%@,sex==%@",_nameTextField.text,_ageTextField.text,_IDTextField.text, _sexTextField.text); NSString *sql= nil; if (self.operateType == 0){//執行插入操做 sql = @"INSERT INTO Student (name,age,idcode,sex) VALUES (?,?,?,?) "; }else if(_operateType == 1)//執行更新操做 { sql = @"UPDATE Student SET name = ? , age = ?,sex=? where idcode = ?"; } BOOL res = [db executeUpdate:sql,_nameTextField.text,_ageTextField.text,_IDTextField.text,_sexTextField.text]; if (!res) { [XFNotices noticesWithTitle:@"數據插入錯誤" Time:1.5 View:self.view Style:(XFNoticesStyleFail)]; }else{ [XFNotices noticesWithTitle:@"數據插入成功" Time:1.5 View:self.view Style:(XFNoticesStyleFail)]; } } }else{ NSLog(@"數據庫打開失敗") ; } if (operateType1 == 0)//若是是添加就留在該頁,若是是修改就跳回上一頁 { [_nameTextField resignFirstResponder]; [_ageTextField resignFirstResponder]; [_IDTextField resignFirstResponder]; [_sexTextField resignFirstResponder]; _nameTextField.text = @""; _ageTextField.text = @""; _IDTextField.text = @""; _sexTextField.text=@""; [self.navigationController popViewControllerAnimated:YES]; } [db close]; }