fmdb常用操作代碼

b77m 9年前發布 | 806 次閱讀 C# IOS

-(NSString )databaseFilePath
{
    //獲取數據庫路經
    NSString url = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [url stringByAppendingPathComponent:@"sqlTJL.sqlite"];
    return fileName;
}

-(void)fmdbData {

//獲取數據庫
_db = [FMDatabase databaseWithPath:[self databaseFilePath]];
//打開數據庫
if ([_db open]) {
//建表
    BOOL result = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS TJL_student(name text)"];
    if (result) {
        NSLog(@"建表成功");
    }else{
        NSLog(@"建表失敗");
    }
    [_db close];
}

}

//插入數據 -(void)insetsqlto:(NSString )string { [_db open]; if ([_db open]) { BOOL res = [_db executeUpdate:@"insert into TJL_student (name) VALUES(?)", string]; if (!res) { NSLog(@"error"); }else{ NSLog(@"success to insert"); } [_db close]; } } //刪除數據 -(void)deleteopen:(NSString )dataName { if ([_db open]) { NSString *deleteSql = [NSString stringWithFormat:@"delete from TJL_student %@",dataName]; BOOL res = [_db executeUpdate:deleteSql]; if (!res) { NSLog(@"error when delete db table"); }else{ NSLog(@"success to delete db table"); } [_db open]; } }

//修改數據 -(void)updataName:(NSString )string { if ([_db open]) { NSString updatesql = [NSString stringWithFormat:@"UPDATE TJL_student'%@'",string]; BOOL RES = [_db executeUpdate:updatesql]; if (!RES) { NSLog(@"error when update db table"); }else{ NSLog(@"success to insert db able"); } [_db close]; } }

//查詢數據 -(void)seacher:(NSString )seaharName { if ([_db open]) { FMResultSet rs = [_db executeQuery:@"SELECT * FROM TJL_student"]; while ([rs next]) { _Devices = [rs stringForColumn:@"name"]; NSLog(@"is text---->>> %@",[rs stringForColumn:@"name"]); } } }</pre>

 本文由用戶 b77m 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!