java - 用按钮绑定 sqlite db
问题描述
大家好,我想查询 sqlite db。我已经准备好了 db 文件。当我点击一个按钮时,我想调用我的数据库返回里面的单词及其定义。有一行用于单词,另一行用于定义。
那是我的java代码:
Button Kitten, edge; //more buutons...
static DatabaseHelper myDbHelper;
static boolean databaseOpened= false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_avengers_words );
this.setTitle( "Words(Easy to hard)" );
Kitten = (Button) findViewById( R.id.kitten );
Kitten.setOnClickListener(this);
Toolbar toolbar = (Toolbar) findViewById( R.id.mToolbar );
getSupportActionBar().setDisplayHomeAsUpEnabled( true );
getSupportActionBar().setDisplayShowHomeEnabled( true );
edge=(Button)findViewById( R.id.edge );
//final String[] from = new String[] {"word"};
//final int[] to = new int[] {R.id.};
myDbHelper = new DatabaseHelper(this);
if (myDbHelper.checkDataBase()) {
openDatabase();
} else
{
LoadDatabaseAsync task = new LoadDatabaseAsync((avengers_words.this));
//noinspection deprecation
task.execute();
}
}
protected static void openDatabase()
{
try {
myDbHelper.openDataBase();
databaseOpened=true;
} catch (SQLiteException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.kitten) {
Intent intent = new Intent(avengers_words.this, words_details.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
startActivity(intent);
}
}
}
这是我的数据库加载器;
private String DB_PATH = null;
private static String DB_NAME = "Dictionary.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static int DB_VERSION = 2; // Database version
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath();
Log.e("Path 1", DB_PATH);
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if (!dbExist){
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e){
throw new Error("Error copying database");
}
}
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB= SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e)
{
//
}
if (checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
Log.i("copyDataBase", "Database copied");
}
public void openDataBase() throws SQLiteException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close(){
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVerison, int newVersion) {
try {
this.getReadableDatabase();
myContext.deleteDatabase(DB_NAME);
copyDatabase();
}catch (IOException e){
e.printStackTrace();
}
}
在 db 文件中:我的表名是“words” db 文件中的单词列表“word” 定义是“defn”
解决方案
推荐阅读
- r - 如何通过您已经拥有的一组名称过滤掉行?
- python - Django-TinyMCE 如何在 TinyMCE 中渲染 Django 模板标签?
- android - 自定义视图库 Android 的架构
- json - 按最近的时间戳对数组中的对象数组进行排序,然后使用 jq 按每个数组的第一个对象的时间戳对外部数组进行排序
- vba - 查询 Oracle 时受影响的记录返回 -1
- django-rest-framework - 权限取决于字段 django rest 框架
- flutter - 如何在颤动中添加不太和底部的应用栏
- jquery - 使用jQuery从输入模糊的下拉列表中选择项目
- python - 使用不同长度的 DNA 字符串为 keras 准备数据
- arrays - Excel 数组和小型 VBA 循环与不带数组的大型 VBA 循环的性能