如何使用sqlite自动完成字段?

时间:2012-06-18 13:21:42

标签: java sqlite blackberry

我正在尝试使用此代码。这是一个使用数据库的自动完成字段。

import java.util.Vector;

import net.rim.device.api.collection.util.*;
import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;


public class AutoCompleteFieldDemo extends UiApplication
{ 

  public static void main(String[] args)
  {

    AutoCompleteFieldDemo app = new AutoCompleteFieldDemo();
    app.enterEventDispatcher(); 
  }

  public AutoCompleteFieldDemo()
  {
    pushScreen(new AutoCompleteFieldDemoScreen());
  }

  public static String[] getDataFromDB()
  {
    Vector names = new Vector();

    try
    {     
      Database db = DatabaseFactory.openOrCreate("database1.db"); 
      Statement statement1 = db.createStatement("SELECT name FROM Directory_Items");
      statement1.prepare();
      statement1.execute();
      Cursor c = statement1.getCursor();
      Row r;

      while(c.next()) 
      {
        r = c.getRow();
        names.addElement(r.getString(0));   
      }

      statement1.close(); 
      db.close(); 
    }
    catch( Exception e ) 
    { 
      System.out.println( e.getMessage() );
      e.printStackTrace();
    }

    String [] returnValues = new String[names.size()];
    for (int i = 0; i < names.size(); i++) {
      returnValues[i] = (String) names.elementAt(i);
    }

    return returnValues;
  }

  static final class AutoCompleteFieldDemoScreen extends MainScreen
  {

    AutoCompleteFieldDemoScreen()
    {
      BasicFilteredList filterLst = new BasicFilteredList();

      filterLst.addDataSet(1,getDataFromDB()
         ,"Names",BasicFilteredList.COMPARISON_IGNORE_CASE)​;
      AutoCompleteField autoFld = new AutoCompleteField(filterLst);
      add(autoFld); 
    }

I found it in here

我面临的问题是,我找不到数据库。我已经在SD卡中创建了数据库,并将名称放在createOropen(“myDataBase.db”)中。但是当我调试它时跳转到异常。任何想法为什么我遇到这个问题。

非常感谢你!

0 个答案:

没有答案