首页 > 解决方案 > 用按钮绑定 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”

标签: javaandroid-studio

解决方案


推荐阅读