如何在Android Studio中查询现有的预先填充的SQLite数据库

时间:2016-03-23 00:14:20

标签: java android database sqlite search

今天早些时候,我在这个网址上提出了一个关于在SQLite中使用预填充数据库的问题:Pre-populated SQLite Database not reading properly in Android Studio

我重新编写了我的整个程序,现在我再次失去了尝试使用正确的代码来允许用户搜索数据库。如果有人可以,请帮我找到查询现有预先填充的SQLite数据库的代码。

数据库助手类:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public class DataBaseHelper extends SQLiteOpenHelper
{
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "chemicals.sqlite";
public final static String DATABASE_PATH ="/data/data/com.example.xxfal_000.chemsafetyapp/databases/";
public static final int DATABASE_VERSION = 1;
//public static final int DATABASE_VERSION_old = 1;

//Constructor
public DataBaseHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

//Create a empty database on the system
public void createDatabase() throws IOException
{
    boolean dbExist = checkDataBase();

    if(dbExist)
    {
        Log.v("DB Exists", "db exists");
        // By calling this method here onUpgrade will be called on a
        // writeable database, but only if the version number has been
        // bumped
        //onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
    }

    boolean dbExist1 = checkDataBase();
    if(!dbExist1)
    {
        this.getReadableDatabase();
        try
        {
            this.close();
            copyDataBase();
        }
        catch (IOException e)
        {
            throw new Error("Error copying database");
        }
    }
}

//Check database already exist or not
private boolean checkDataBase()
{
    boolean checkDB = false;
    try
    {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        File dbfile = new File(myPath);
        checkDB = dbfile.exists();
    }
    catch(SQLiteException e)
    {
    }
    return checkDB;
}

//Copies your database from your local assets-folder to the just created empty database in the system folder
private void copyDataBase() throws IOException
{
    String outFileName = DATABASE_PATH + DATABASE_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0)
    {
        myOutput.write(buffer, 0, length);
    }
    myInput.close();
    myOutput.flush();
    myOutput.close();
}

//delete database
public void db_delete()
{
    File file = new File(DATABASE_PATH + DATABASE_NAME);
    if(file.exists())
    {
        file.delete();
        System.out.println("delete database file.");
    }
}

//Open database
public void openDatabase() throws SQLException
{
    String myPath = DATABASE_PATH + DATABASE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void closeDataBase()throws SQLException
{
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

public void onCreate(SQLiteDatabase db)
{
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    if (newVersion > oldVersion)
    {
        Log.v("Database Upgrade", "Database version higher than old.");
        db_delete();
    }
}
//This is how I tried in my earlier database to search in the .sqlite file
public Compound findCompound(String compoundname) {
    String query = "Select * FROM osha WHERE compound = '" + compoundname + "'";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    Compound compound = new Compound();
    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        compound.setH(cursor.getString(0));
        compound.setF(cursor.getString(1));
        compound.setR(cursor.getString(2));
        compound.setSN(cursor.getString(3));
        // cursor.moveToNext();
        cursor.close();
    } else {
        compound = null;
    }
    db.close();
    return compound;
}
}

搜索OSHA课程(这是我的主要课程)

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class oshaSearch extends AppCompatActivity {
TextView txtH;
TextView txtR;
TextView txtF;
TextView txtSN;
EditText txtCompound;
Button btnSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_osha_search);
    txtF = (TextView) findViewById(R.id.txtF);
    txtR = (TextView) findViewById(R.id.txtR);
    txtH = (TextView) findViewById(R.id.txtH);
    txtSN = (TextView) findViewById(R.id.txtSN);
    txtCompound = (EditText) findViewById(R.id.searchCompound);
    btnSearch = (Button) findViewById(R.id.btnSearch);
}
public void lookupCompound (View view) {
    DataBaseHelper dbHelper = new DataBaseHelper(this);

    Compound compound =
            dbHelper.findCompound(txtCompound.getText().toString());

    if (compound != null) {
        txtH.setText(String.valueOf(compound.getH()));
        txtF.setText(String.valueOf(compound.getF()));
        txtR.setText(String.valueOf(compound.getR()));
        txtSN.setText(String.valueOf(compound.getSN()));
    } else {
        txtH.setText("No Match Found");
    }
}
}

这是我的复合课:

public class Compound {
private String  _compound;
private String _h;
private String _f;
private String _r;
private String _sn;

public Compound() {

}

public Compound(String compound, String h, String f, String r, String sn) {
    this._compound = compound;
    this._h = h;
    this._f = f;
    this._r = r;
    this._sn = sn;
}

public Compound(String h, String f, String r, String sn) {
    this._h = h;
    this._f = f;
    this._r = r;
    this._sn = sn;
}

public void setCompound(String compound) {
    this._compound = compound;
}

public String getCompound() {
    return this._compound;
}

public void setH(String h) {
    this._h = h;
}

public String getH() {
    return this._h;
}

public void setF(String f) {
    this._f = f;
}

public String getF() {
    return this._f;
}
public void setR(String r) {
    this._r = r;
}
public String getR(){
    return this._r;
}
public void setSN(String sn){
    this._sn = sn;
}
public String getSN(){
    return this._sn;
}
}

我知道我提出了很多帮助,但这是因为我对Android Studio相当新鲜。所以我能得到任何帮助,我非常感激。

0 个答案:

没有答案