今天正式把SQLlite丟進模擬機試看看
public class DatabaseActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView;
private String mDatabaseName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_database);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
textView = findViewById(R.id.tv_result);
findViewById(R.id.btn_create).setOnClickListener(this);
findViewById(R.id.btn_delete).setOnClickListener(this);
mDatabaseName = getFilesDir()+"/test.db";
}
@Override
public void onClick(View view) {
String desc = null;
if(view.getId()==R.id.btn_create){
SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE, null);
desc = String.format("數據庫%s創建%s",db.getPath(),(db!=null)?"成功":"失敗");
textView.setText(desc);
}else {
boolean result = deleteDatabase(mDatabaseName);
desc = String.format("數據庫%s刪除%s",mDatabaseName,result?"成功":"失敗");
textView.setText(desc);
}
}
}
建立資料庫跟刪除:)