android - how to connect a external database to application -
hello everybody.
i'm trying make dictionary application. tried copy database named "dic"
in "dic_openhelper"
. have executive class named "dictionary"
. searched results show in class.
and whats problem: when want perform "dictionary"
class takes lot of time load. while list of errors shown on eclipse. anyway class start. off course not big problem because cases first time of performing "dictionary"
class , main problem here. database has 140000 records. big database. when searched example "send", took lot of time , after 2 minutes, showed , made list view!!! , when tried cods small database, worked well.
what should do??
my "dictionary"
, "dic_openhelper"
class:
package com.example.masaf; import java.util.locale; import android.app.activity; import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.graphics.typeface; import android.os.bundle; import android.speech.tts.texttospeech; import android.text.editable; import android.text.textwatcher; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class dictionary extends activity{ public listview list; public imageview search; public edittext et; private textview tv; private dic_openhelper db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.dic); et=(edittext) findviewbyid(r.id.dic_et); tv=(textview) findviewbyid(r.id.dic_tv); list=(listview) findviewbyid(r.id.dic_list); search=(imageview) findviewbyid(r.id.dic_img_search); db=new dic_openhelper(this); db.database(); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); search.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { showlist(et.gettext().tostring()); } }); } public void showlist(string w){ db.open(); int count=db.count_serach(w); string word[]=new string[count]; for(int i=0;i<count;i++){ word[i]=db.serach(i, 1, w); } db.close(); list.setadapter(new arrayadapter(this, android.r.layout.simple_list_item_1,word)); } @override public void onbackpressed() { finish(); startactivity(new intent(dictionary.this, other.class)); } }
and "dic_openhelper" class:
package com.example.masaf; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.widget.toast; public class dic_openhelper extends sqliteopenhelper { public final string path="data/data/com.example.masaf/databases/"; public final string name="dic"; public sqlitedatabase mydb; private final context mycontext; public dic_openhelper(context context) { super(context, "dic", null, 1); mycontext=context; } @override public void oncreate(sqlitedatabase arg0) { // todo auto-generated method stub } @override public void onupgrade(sqlitedatabase arg0, int arg1, int arg2) { // todo auto-generated method stub } public void database(){ boolean checkdb=checkdb(); if(checkdb){ }else{ this.getreadabledatabase(); try{ copydatabase(); }catch(ioexception e){ } } } public void open(){ mydb=sqlitedatabase.opendatabase(path+name, null, sqlitedatabase.open_readwrite); } public void close(){ mydb.close(); } public boolean checkdb(){ sqlitedatabase db=null; try{ db=sqlitedatabase.opendatabase(path+name, null, sqlitedatabase.open_readonly); } catch(sqlexception e) { } return db !=null ? true:false ; } public void copydatabase() throws ioexception{ outputstream myoutput = new fileoutputstream(path+name); byte[] buffer = new byte[1024]; int length; inputstream myinput = mycontext.getassets().open("dic"); while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } myinput.close(); myoutput.flush(); myoutput.close(); } public string display(int row, int position){ cursor d=mydb.query("words", null, null, null, null, null, null); d.movetoposition(position); string name=d.getstring(row); return name; } public integer count(){ cursor cu=mydb.query("words", null, null, null, null, null, null); int s=cu.getcount(); return s; } public integer count_serach(string word){ cursor cu; cu=mydb.rawquery("select * words enword '%"+word+"%'", null); int s=cu.getcount(); return s; } public string serach(int row,int col,string word){ cursor cu; cu=mydb.rawquery("select * words enword '%"+word+"%'", null); cu.movetoposition(row); string s=cu.getstring(col); return s; } }
excuse me wasting time. i'm iranian, , if there problems in above texts, apologize! thank patience , tolerance dear friend. ali bazrafshan. iran, khorasan, ferdows
first of improve query... please specify column name
instead of *
. , use limit clause in search query.
use this:
select column_name1,column_name2,column_name3 words enword '%'+word+'%' limit 10;
instead of:
select * words enword '%"+word+"%'
then index search column.
i think these tricks you...
Comments
Post a Comment