我怎样才能正确地将SQLite数据库导入到android项目中

时间:2014-10-03 01:24:15

标签: android sqlite

我是Android新手。我已经尝试了几次但仍然无法编译好。所以我想知道为什么我无法正确地将SQLitedatabase导入到项目中。

这是帮助文件

public class Databasehelper {

private static final String DB_NAME = "wordlist.db";
private static final String DATABASE_PATH = "/data/data/edu.neu.madcourse.ruihanwang/databases/";

private Context context;

public Databasehelper(Context context) {
    this.context = context;
}

public SQLiteDatabase openDatabase() {
    File dbFile = context.getDatabasePath(DB_NAME);

    if (!dbFile.exists()) {
        try {
            copyDatabase();
        } catch (IOException e) {
            throw new RuntimeException("Error creating source database", e);
        }
    }

    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

private void copyDatabase() throws IOException {
    InputStream in  = context.getAssets().open("mycontacts");
    Log.e("sample", "Starting copying" );
    String outputFileName = DATABASE_PATH+DB_NAME;
    File databaseFile = new File( "/data/data/edu.neu.madcourse.ruihanwang/databases");
     if (!databaseFile.exists()){
         databaseFile.mkdir();
     }

    OutputStream out = new FileOutputStream(outputFileName);

    byte[] buffer = new byte[1024];
    int length;


    while ((length = in.read(buffer))>0){
           out.write(buffer,0,length);
    }
    Log.e("sample", "Completed" );
    out.flush();
    out.close();
    in.close();

 }

}

这是字典文件      public class Dictionary extends Activity实现OnClickListener {

EditText searcharea;
Button return_button;
Button search;
ListView results;
TextView storage;
SQLiteDatabase wordlist;
ArrayList<String>list;
Databasehelper myDbhelper;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dictionary);
    search = (Button) findViewById(R.id.acknowledge_button);
    search.setOnClickListener(this);
    return_button = (Button) findViewById(R.id.return_button);
    searcharea= (EditText) findViewById(R.id.search_area);
    return_button.setOnClickListener(this);
    myDbhelper.openDatabase();
    results = (ListView) findViewById(R.id.resultslist);
    storage = (TextView) findViewById(R.id.justtext);

    adapter = new ArrayAdapter<String> (Dictionary.this, android.R.layout.simple_expandable_list_item_1,list);
    searcharea.addTextChangedListener((TextWatcher) mylistener);
    }
TextWatcher mylistener = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        String sql = "select * from wordlist where word like'" +s +"'";
        Cursor cursor = wordlist.rawQuery(sql, null);


        if (cursor.getCount() > 0){

        cursor.moveToFirst();
 }
        String ss = cursor.getString(0);
        list.add(ss);
        results.setAdapter(adapter);

    }

    @Override
    public void afterTextChanged(Editable s) {



    }

};

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.return_button) {
        finish();
        } else if(v.getId() == R.id.clearButton) {
            list = null;
            searcharea.setText("");
        }
            else if(v.getId() == R.id.acknowledge_button) {
            finish();
            }
}
}

我将数据库文件存储在资产

0 个答案:

没有答案