SQLiteDAOImpl.java
3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package libs.cnlive.com.uploadsdk.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/9/22.
*/
public class SQLiteDAOImpl {
private DBOpenHelper helper;
public SQLiteDAOImpl(Context context) {
this.helper = new DBOpenHelper(context, "CNLuploadFile.db", null, 1);
}
public UploadFileEntity find(String filePath) {// 根据文件路径查找纪录
UploadFileEntity uploadFileEntity = null;
SQLiteDatabase db = helper.getReadableDatabase();
// 用游标Cursor接收从数据库检索到的数据
Cursor cursor = null;
cursor = db.rawQuery("select * from cnlive_file where filepath=?", new String[]{filePath});
if (cursor.moveToFirst()) {// 依次取出数据
uploadFileEntity = new UploadFileEntity();
uploadFileEntity.setFilePath(cursor.getString(cursor.getColumnIndex("filepath")));
uploadFileEntity.setUploadId(cursor.getString(cursor.getColumnIndex("uploadid")));
uploadFileEntity.setBucketKey(cursor.getString(cursor.getColumnIndex("bucketkey")));
uploadFileEntity.setPartNumber(cursor.getInt(cursor.getColumnIndex("partnumber")));
uploadFileEntity.setUploadSize(cursor.getLong(cursor.getColumnIndex("uploadsize")));
}
db.close();
return uploadFileEntity;
}
public void save(String filePath, String uploadId, String bucketKey, int partNumber, long uploadSize) {// 插入记录
SQLiteDatabase db = helper.getWritableDatabase();// 取得数据库操作
ContentValues values = new ContentValues();
values.put("filepath", filePath);
values.put("uploadid", uploadId);
values.put("bucketkey", bucketKey);
values.put("partnumber", partNumber);
values.put("uploadsize", uploadSize);
db.insert("cnlive_file", null, values);
db.close();// 记得关闭数据库操作
}
public void update(String uploadId, int partNumber, long uploadSize) {// 修改纪录
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("partnumber", partNumber);
values.put("uploadsize", uploadSize);
db.update("cnlive_file", values, "uploadid=?", new String[]{uploadId});
db.close();
}
public void delete(String filePath) {
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("cnlive_file", "filepath=?", new String[]{filePath});
db.close();
}
public List<UploadFileEntity> findAll() {// 查询所有记录
List<UploadFileEntity> lists = new ArrayList<>();
UploadFileEntity uploadFileEntity;
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from cnlive_file", null);
while (cursor.moveToNext()) {
uploadFileEntity = new UploadFileEntity();
uploadFileEntity.setFilePath(cursor.getString(cursor.getColumnIndex("filepath")));
uploadFileEntity.setUploadId(cursor.getString(cursor.getColumnIndex("uploadid")));
uploadFileEntity.setBucketKey(cursor.getString(cursor.getColumnIndex("bucketkey")));
uploadFileEntity.setPartNumber(cursor.getInt(cursor.getColumnIndex("partnumber")));
lists.add(uploadFileEntity);
}
db.close();
return lists;
}
}