【问题标题】:Pre-populated SQLite Database not reading properly in Android Studio预填充的 SQLite 数据库无法在 Android Studio 中正确读取
【发布时间】:2016-07-09 19:11:30
【问题描述】:

我是 StackOverflow 的新手,所以我不确定它是如何工作的,但我确实需要一些关于我在 Android Studio 中构建的应用程序的帮助。我有一个预填充的chemicals.sqlite 数据库,我正在尝试在Android Studio 中进行搜索。基本上,我正在设计的应用程序将允许用户按名称搜索化合物,它会显示有关它的信息。出于某种原因,我的程序说它无法显示我试图在应用程序的模拟器中搜索的任何值。它总是显示“未找到匹配项”。

这是我的 DatabaseHelper 类:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "chemicals.sqlite";
private static final String TABLE_OSHA = "osha";
public static final String COLUMN_COMPOUND = "compound";
public static final String COLUMN_H = "h";
public static final String COLUMN_F = "f";
public static final String COLUMN_R = "r";
public static final String COLUMN_SN = "sn";

public DataBaseHelper(Context context, String name,
                   SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_OSHA_TABLE = "CREATE TABLE " +
            TABLE_OSHA + "("
            + COLUMN_COMPOUND + " TEXT," + COLUMN_H
            + " TEXT," + COLUMN_F + " TEXT," + COLUMN_R + " TEXT," + COLUMN_SN + " TEXT" + ")";
    db.execSQL(CREATE_OSHA_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
                      int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_OSHA);
    onCreate(db);
}
public Compound findCompound(String compoundname) {
    String query = "Select * FROM " + TABLE_OSHA + " WHERE " + COLUMN_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;
}
}

这是我的复合类:

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;
}
}

这是我的主要课程:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 dbHandler = new DataBaseHelper(this, null, null, 1);

    Compound compound =
            dbHandler.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");
    }
}
}

非常感谢任何和所有帮助。请帮帮我。 附加信息:数据库位于资产文件夹中,数据库中的所有值都是文本字段,用户正在按化合物名称搜索,数据库具有 .sqlite 扩展名。提前谢谢大家。

【问题讨论】:

  • 您知道DataBaseHelper 总是会在手机上创建一个新的chemicals.sqlite,而忽略您资产中的任何内容,对吧?因为我看不到您在哪里填充数据库。
  • 欢迎来到 Stack Overflow!感谢您花时间使用正确的语法并确保您的代码格式正确。很多时候,新用户在发帖时似乎忘记了自己的礼貌。
  • @david 感谢您的回复!我不知道……我以为我已经设置好了,所以它会搜索我资产中的 .sqlite 文件夹并从中提取数据。至少我的意图是这样的......
  • 在那个帖子中链接的是SQLiteAssetHelper,它可以从你的assets打开文件

标签: java android database sqlite search


【解决方案1】:

您必须将您所说的位于 assets 文件夹中的数据库复制到新创建的数据库 -

 private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(dbname);
        // Path to the just created empty db
        String outFileName = getDatabasePath(dbname);
        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

【讨论】:

  • 感谢@shadab 的代码,但我对如何搜索附加的数据库感到困惑。我是否必须更改我的 findCompound() 方法中的某些内容?
  • 现在您已将资产数据库复制到本地数据库,您将可以访问所有值。您不必更改其他方法。
  • 所以我尝试输入代码,它给了我错误,说我没有定义 myContext 和 getDatabasePath。我定义了 Context myContext,但是对于 getDatabasePath,我应该在那里输入数据库名称吗?对不起,我真的还是迷路了。
  • getDatabasePath () 应该返回你的应用程序数据库文件路径
  • 所以它就像 return '"data/data/"+BuildConfig.APPLICATION_ID+"/databases"'?知道了。那么我只需在我的 findCompound() 方法中调用 CopyDataBase() 吗?
【解决方案2】:

您首先需要检查数据库是否正确创建,为此

public DataBaseHelper(Context context) {
    super(context, Environment.getExternalStorageDirectory()+"/"+DATABASE_NAME, null, 1);
}

使用此构造函数,这会将您的应用程序数据库保存在手机的外部存储中。 在此之前授予清单文件中的外部存储权限。 并使用 google play store 中的 sqliteManager 应用程序检查数据库结构。

或者你可以使用stetho来调试android的数据库。 参考链接:http://code.tutsplus.com/tutorials/debugging-android-apps-with-facebooks-stetho--cms-24205

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多