參考視頻學習:https://www.bilibili.com/video/BV1wt411n7pz
查看數據庫的軟件 SQLite Expert
從編譯器右下角打開的模擬器中找到對應項目的數據庫文件,然後添加到軟件中去查看
- 創建數據庫
- Dao層去增刪改查
- 一個常量類去管理
public class Constants {
public static final String DATABASE_NAME = "test.db";
public static final int DATABASE_VERSION = 3;
public static final String TABLE_NAME = "employee";
}
Sql語言版
public class Dao {
private DatabaseHelper mHelper;
private static final String TAG = "Dao";
public Dao(Context context) {
//創建數據庫
mHelper = new DatabaseHelper(context);
}
public void insert() {
SQLiteDatabase db = mHelper.getWritableDatabase();
String sql = "insert into "+Constants.TABLE_NAME+"(_id,name,age,salary,phone,average,address) " +
"values(?,?,?,?,?,?,?)";
db.execSQL(sql,new Object[]{1,"Mike",60,111,110,55,"USA"});
db.close();
}
public void delete() {
SQLiteDatabase db = mHelper.getWritableDatabase();
String sql = "delete from "+Constants.TABLE_NAME+" where age = 60";
db.execSQL(sql);
db.close();
}
public void update() {
SQLiteDatabase db = mHelper.getWritableDatabase();
String sql = "update "+Constants.TABLE_NAME+" set salary = 2 where age = 60";
db.execSQL(sql);
db.close();
}
public void query() {
SQLiteDatabase db = mHelper.getWritableDatabase();
String sql = "select * from "+Constants.TABLE_NAME;
Cursor cursor = db.rawQuery(sql, null);
while (cursor.moveToNext()) {
//返回name是第幾列
int index = cursor.getColumnIndex("name");
//返回name那一列的並轉成字符串
String name = cursor.getString(index);
Log.e(TAG, "query: "+name );
}
cursor.close();
db.close();
}
}
使用API版本的
public class Dao {
private DatabaseHelper mHelper;
private static final String TAG = "Dao";
public Dao(Context context) {
//創建數據庫
mHelper = new DatabaseHelper(context);
}
public void insert() {
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("_id",2);
values.put("name","Tom");
values.put("age",20);
values.put("salary",100);
values.put("phone",120);
values.put("average",123);
values.put("address","China");
db.insert(Constants.TABLE_NAME,null,values);
db.close();
}
public void delete() {
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(Constants.TABLE_NAME,"age=?",new String[]{"20"});
db.close();
}
public void update() {
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("age",50);
db.update(Constants.TABLE_NAME,values,"name = ?",new String[]{"Tom"});
db.close();
}
public void query() {
SQLiteDatabase db = mHelper.getWritableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, null, "age = 50", null, null, null, null);
while (cursor.moveToNext()){
Log.e(TAG, "name->"+cursor.getString(cursor.getColumnIndex("name")));
}
cursor.close();
db.close();
}
}
- 測試類測試
@RunWith(AndroidJUnit4.class)
public class TestDatabase {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
@Test
public void testCreate() {
}
@Test
public void testInsert() {
Dao dao = new Dao(context);
dao.insert();
}
@Test
public void testDelete() {
Dao dao = new Dao(context);
dao.delete();
}
@Test
public void testUpdate() {
Dao dao = new Dao(context);
dao.update();
}
@Test
public void testQuery() {
Dao dao = new Dao(context);
dao.query();
}
}
- 數據庫事務
兩個特點- 安全性
比如公司有一百萬,你的工資是一萬,當工資給你發工資時,這時突然停電,公司賬戶少了一萬,但你的賬戶沒有增加錢,這就是安全性不夠.
安全性:當有異常時就會兩個都不變,不發生異常時會正常變化
- 安全性
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String TABLE_NAME = "account";
public DataBaseHelper(@Nullable Context context) {
super(context, "test.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table "+TABLE_NAME+"(_id integer,name varchar,money integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@RunWith(AndroidJUnit4.class)
public class TestDataBase {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
private DataBaseHelper mHelper = new DataBaseHelper(context);
@Test
public void testDataBase() {
mHelper.getWritableDatabase();
}
@Test
public void testInsert() {
SQLiteDatabase db = mHelper.getWritableDatabase();
String sql = "insert into account values (1,'company',1000000)";
String sql1 = "insert into account values (2,'my_count',0)";
db.execSQL(sql);
db.execSQL(sql1);
}
//當有異常時就會兩個都不變,不發生異常時會正常變化
@Test
public void testUpdate() {
SQLiteDatabase db = mHelper.getWritableDatabase();
db.beginTransaction();
try {
String sql = "update account set money = 1000000 - 10000 where name = 'company'";
//int i = 10 / 0;
String sql1 = "update account set money = 10000 where name = 'my_count'";
db.execSQL(sql);
db.execSQL(sql1);
db.setTransactionSuccessful();
}catch (Exception e){
throw new RuntimeException("發生異常!");
}finally {
db.endTransaction();
db.close();
}
}
}
- 高效性
添加大量數據時,使用的時間長短
加上這兩句,效率會提升幾十倍,因爲添加後數據一次性添加進去,而不是一條一條地添加
db.beginTransaction();
db.endTransaction();
@Test
public void testInsert() {
SQLiteDatabase db = mHelper.getWritableDatabase();
Long start = System.currentTimeMillis();
db.beginTransaction();
for(int i=0;i<3000;i++) {
String sql = "insert into account values (1,'company',1000000)";
db.execSQL(sql);
}
db.endTransaction();
Log.e(TAG, "time -> " + (System.currentTimeMillis() - start) );
}