在android中填充DB的更有效方法

时间:2013-11-14 14:09:54

标签: java android database sqlite

我想在我的数据库中填充一个Android应用程序,但我不知道我是否做得很好。所以我想在这里寻求一点帮助

这是我的DB代码

public long createData() {
    // TODO Auto-generated method stub
    String [] arg = new String [] {"Abdullah", "Abdulatif","Abdulaziz","Abdulbaki","Zafir"};
    String [] mng = new String [] { "Adhurues; Rob i Zotit", "Adhurues i Zotit Ledhatues","Adhurues i Zotit Fuqiplotë","Adhurues i Zotit të përhershëm","Fitimtar"};
    String [] gnr = new String [] {"Mashkull", "Mashkull","Mashkull", "Mashkull", "Mashkull"};
    ContentValues cv = new ContentValues();
    for (int i = 0; i < arg.length; i++){
        cv.put(KEY_EMRI, arg[i]);

        cv.put(KEY_KUPTIMI, mng[i]);


        cv.put(KEY_GJINIA, gnr[i]);
        ourDatabase.insert(DATABASE_TABLE, null, cv);
    }
    return  ourDatabase.insert(DATABASE_TABLE, null, cv);

} 

这是我在java文件中创建的方法onCreate,我想显示来自DB的结果

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.meshkuj);

    search = (Button) findViewById(R.id.bKerko);
    etSearch = (EditText) findViewById(R.id.etKerko);
    tvName = (TextView) findViewById(R.id.tvEmr);
    tvGenre = (TextView) findViewById(R.id.tvGjinia);
    tvMeaning = (TextView) findViewById(R.id.tvKuptimi);
    emri = (TextView) findViewById(R.id.tvShEmri);


    Emertimet info = new Emertimet(this);
    info.open();
    info.createData();
    info.close();           
}

所以我再次想知道填充DB或(createData)的任何其他方法,或者这个方法是好的。提前谢谢你

1 个答案:

答案 0 :(得分:0)

你做错了,因为

  1. 你是在主线程上做的(片段在这里:Correct use of Sqlite on Android (SQLiteOpenHelper, connections, main thread, etc...)
  2. 您没有在交易(Android SQLite database: slow insertion
  3. 中对其进行批处理