简体   繁体   English

在Android中使用外部数据库文件

[英]Using external database file in Android

I am using DB browser for SQLite to create an external database, the file name's words_library.db inside that file I have a table called dictionary.db that contains 3 columns : "English_lib", "German_lib", "_id" as shown below (Fig. 1) 我正在使用DB browser for SQLite来创建外部数据库,该文件中文件名为words_library.db ,我有一个名为dictionary.db的表,该表包含3 columns :“ English_lib”,“ German_lib”,“ _ id”,如下所示(图。1)

What I am trying to do is populating a recyclerview with that database but I failed to do so. 我正在尝试用该数据库填充recyclerview,但我没有这样做。

JAVA 爪哇

public void fetchData() //this method is called to populate the RecyclerView
{
    db =new DataBaseHelper(getContext());
    try {

        db.createDataBase();
        db.openDataBase();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    namelist=new LinkedHashMap<>();
    int ii;
    SQLiteDatabase sd = db.getReadableDatabase();
    Cursor cursor = sd.query("dictionary_lib" ,null, null, null, null, null, null);
    ii=cursor.getColumnIndex("_id");
    eng_list=new ArrayList<String>();
    ger_list= new ArrayList<String>();
    id_list= new ArrayList<String>();
    cursor.moveToFirst();
    while (cursor.moveToNext()){
        namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("English_lib")));
    }

    Iterator entries = namelist.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry thisEntry = (Map.Entry) entries.next();
        id_list.add(String.valueOf(thisEntry.getKey()));
        eng_list.add(String.valueOf(thisEntry.getValue()));
        ger_list.add(String.valueOf(thisEntry.getValue()));
    }

    for (int i = 0; i < eng_list.size(); i++) {
        data.add(new DictObjectModel(eng_list.get(i), ger_list.get(i)));
    }
    adapter = new CustomAdapter(data);
    rv.setAdapter(adapter);
}

LOG 日志

Failed to read row 1, column -1 from a CursorWindow which has 3469 rows, 2 columns.
java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  
Make sure the Cursor is initialized correctly before accessing data from it.

fig 1 : 图。1 :

an example of the database I have and the recyclerview should only have the English_lib and the German_lib columns shown 我拥有的数据库的一个示例,recyclerview应该只显示了English_libGerman_lib

在此处输入图片说明

to do so : 这样做:

1- change the PK column name to "rowid" instead of "_id" (this will fix a lot of PK related problems not only this one) 1-将PK列名称更改为“ rowid”而不是“ _id”(这不仅会解决很多与PK相关的问题)

2- update the code by adding new variables and lists like so: 2-通过添加新的变量和列表来更新代码,如下所示:

public void fetchData()
{
    db = new DataBaseHelper(getContext());
    try {

        db.createDataBase();
        db.openDataBase();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    namelist = new LinkedHashMap<>();
    nmlist = new LinkedHashMap<>();
    int ii;
    SQLiteDatabase sd = db.getReadableDatabase();
    Cursor cursor = sd.query("dictionary_lib" ,new String[]{
            "English_lib", "German_lib", "rowid"
    }, null, null, null, null, null);
    ii=cursor.getColumnIndexOrThrow("rowid");
    eng_list = new ArrayList<String>();
    ger_list = new ArrayList<String>();
    id_list = new ArrayList<String>();
    while (cursor.moveToNext()){
        namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndexOrThrow("English_lib")));
        nmlist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndexOrThrow("German_lib")));
    }

    Iterator entries = namelist.entrySet().iterator();
    Iterator entrs = nmlist.entrySet().iterator();
    while (entries.hasNext() && entrs.hasNext()) {
        Map.Entry thisEntry = (Map.Entry) entries.next();
        Map.Entry altEntry = (Map.Entry) entrs.next();
        id_list.add(String.valueOf(thisEntry.getKey()));
        eng_list.add(String.valueOf(thisEntry.getValue()));
        ger_list.add(String.valueOf(altEntry.getValue()));
    }

    for (int i = 0; i < id_list.size(); i++) {
        data.add(new DictObjectModel(eng_list.get(i), ger_list.get(i)));
    }
    adapter = new CustomAdapter(data);
    rv.setAdapter(adapter);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM