2016-03-22 10 views
2

私はStackOverflowを初めて使用しているので、これはどういう仕組みかわかりませんが、Androidスタジオで構築しているこのアプリで本当に助けが必要です。私は、Android Studioで検索しようとしている、事前設定済みのchemicals.sqliteデータベースがあります。基本的には、私が設計しているアプリケーションは、ユーザが化合物を名前で検索できるようになり、その情報が表示されます。何らかの理由で、私のプログラムは、私がアプリケーションのエミュレータで検索しようとしている値を表示できないと言っています。常に「一致しない」と表示されます。ここであらかじめ用意されているSQLiteデータベースがAndroidスタジオで正しく読み込まれない

は私の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"); 
    } 
} 
} 

任意およびすべてのヘルプは非常にあります感謝。私を助けてください。 追加情報:データベースはassetsフォルダにあり、データベースのすべての値はテキストフィールドです。ユーザーは複合名で検索しています。データベースの拡張子は.sqliteです。皆さんありがとうございました。

+0

あなたは、 'DataBaseHelper'は常に電話で新しい' chemicals.sqlite'を作成し、あなたの資産にあるものは無視しますか?私はあなたがあなたのデータベースを埋める場所を見ていないからです。 –

+1

ようこそスタックオーバーフロー!正しい文法を使用してコードが適切にフォーマットされていることを確認していただきありがとうございます。多くの場合、新しいユーザーは投稿時にマナーを忘れてしまいます。 –

+0

@david返信ありがとう!私はそれを知らなかった...私はそれが私の資産の.sqliteフォルダを検索し、そこからデータを引き出すように設定したと思った。少なくとも私の意図は... – KatieJRise

答えて

0

あなたが新たに作成されたデータベースへの資産フォルダにあるデータベースをコピーする必要があります -

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(); 
    } 
+0

コード@shadabのおかげで、私はどのように私が検索することができるのか混乱していますデータベースが接続されています。 findCompound()メソッドで何かを変更する必要がありますか? – KatieJRise

+0

アセットデータベースをローカルデータベースにコピーしたので、すべての値にアクセスできます。他の方法を変更する必要はありません。 –

+0

私はコードを入れようとしましたが、myContextとgetDatabasePathを定義していないというエラーが表示されます。 Context myContextを定義しましたが、getDatabasePathの場合はデータベース名を入れてください。申し訳ありませんが、私は本当にまだ失われています。 – KatieJRise

0

データベースが正しく作成されているかどうかという

public DataBaseHelper(Context context) { 
    super(context, Environment.getExternalStorageDirectory()+"/"+DATABASE_NAME, null, 1); 
} 
のために、最初に確認する必要が

このコンストラクタを使用すると、アプリデータベースを携帯電話の外部ストレージに保存します。その前に がマニフェストファイルの外部ストレージへのアクセス許可を与えます。 と、データベース構造をチェックするGoogle PlayストアのsqliteManagerアプリケーションを使用します。

また、stethoを使用してAndroidのデータベースをデバッグすることもできます。 参照リンク:http://code.tutsplus.com/tutorials/debugging-android-apps-with-facebooks-stetho--cms-24205

関連する問題