2016-01-27 167 views
0

大家好。如何将外部数据库连接到应用程序

我正在尝试制作字典应用程序。 我试图复制我的数据库"dic"在我的"Dic_Openhelper"。 我有一个名为"Dictionary"的执行类。搜索结果将显示在本课程中。

并且什么我的问题: 当我想执行"Dictionary"类需要花费大量的时间来加载。虽然这个错误列表显示在我的月食上。但无论如何,课程将开始。 当然它不是一个大问题,因为这些案件只是第一次执行"Dictionary"课程,而且主要问题在这里。我的数据库有大约140000条记录。所以它是一个大数据库。当我搜索例如“发送”,它花了很多时间,大约2分钟后,它显示并提出了列表视图!当我为一个小型数据库尝试这些鳕鱼时,它运行良好。

我该怎么办?

"Dictionary""Dic_openhelper"类:

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)); 
} 
} 

和我的 “Dic_openhelper” 类:

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 * from words where enWord Like '%"+word+"%'", null); 
    int s=cu.getCount(); 
    return s; 
} 

    public String serach(int row,int col,String word){ 

Cursor cu; 
cu=mydb.rawQuery("select * from words where enWord Like '%"+word+"%'", null);  
cu.moveToPosition(row); 
String s=cu.getString(col); 
return s; 
    } 

    } 

对不起,我浪费你的时间。 我是伊朗人,如果上面的文字有问题,我很抱歉! 谢谢你的耐心和耐心亲爱的朋友。 Ali Bazrafshan。 Iran,Khorasan,Ferdows

+0

你的问题不清楚...你是否正在寻找更快的查询/搜索?也没有寻找外部数据库的例子?... –

+0

我说,当我使用数据库有很多记录,设备将挂断。但是当我使用这些鳕鱼作为基础数据库时,它会运行良好。所以,当你阅读,我有一个搜索和使用我的数据库,它有很多记录(140000记录)的问题。 – Ali

+0

为什么您一次加载所有数据....在查询中使用限制 –

回答

0

首先提高您的查询...请指定column name而不是*。并在搜索查询中使用LIMIT子句。

使用此:

SELECT COLUMN_NAME1,COLUMN_NAME2,COLUMN_NAME3 FROM words where enWord Like '%'+word+'%' LIMIT 10; 

相反的:

select * from words where enWord Like '%"+word+"%' 

然后INDEX搜索栏。

我认为这些技巧会帮助你...

相关问题