1。創(chuàng)建SQLiteOpenHelper的子類
public class OldDownloadDatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 2;
private static final String DB_NAME = "download_info.db";
public static final String TABLE_COUSE_DOWNLOAD_NAME = "download_info"; private static final String SQL_CREATE_DOWNLOAD_TABLE = String.format( "CREATE TABLE %s (_id varchar(255) PRIMARY KEY NOT NULL,supportRanges integer NOT NULL,createAt long NOT NULL,uri varchar(255) NOT NULL,path varchar(255) NOT NULL,size long NOT NULL, progress long NOT NULL,status integer NOT NULL);", TABLE_COUSE_DOWNLOAD_NAME);
public OldDownloadDatabaseHelper(@Nullable Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override public void onCreate(SQLiteDatabase db) {
// 創(chuàng)建數(shù)據(jù)庫(kù)1張表 // 通過execSQL()執(zhí)行SQL語句(此處創(chuàng)建了1個(gè)名為person的表)// String sql = "create table person(id integer primary key autoincrement,name varchar(64),address varchar(64))";// db.execSQL(sql); db.execSQL(SQL_CREATE_DOWNLOAD_TABLE);
}
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.獲取可讀或者可寫的數(shù)據(jù)庫(kù)
SQLiteDatabase db =trackHelper.getReadableDatabase();
val helper = OldDownloadDatabaseHelper(AppGlobals.getApplication()).writableDatabase
3.執(zhí)行SQL語句
/ 查詢所有數(shù)據(jù)
cursor = db.query(OldDownloadTrackDatabaseHelper.TABLE_TRACK_DOWNLOAD_NAME, null, null, null, null, null, null);
4.常用的sql語句
1.模糊刪除
helper.execSQL("DELETE FROM download_info where _id like '%" + courseDB.name+"'")
2.刪除一張表
val sql ="DROP TABLE IF EXISTS \"ZYREADER\""
db.execSQL(sql)