SQLiteDatabase表未在Android中创建

时间:2013-07-09 10:31:18

标签: android android-emulator android-sqlite sqlite

我一直在尝试使用Android示例中SearchableDictionary之后的搜索工具构建数据库应用程序:

帮助程序类如下:

public class DataBaseTable {

public static String DATABASE_NAME ="DICTIONARY";
public static int DATABASE_VERSION=1;
private final DatabaseOpenHelper datahelper;
public static String TAG="Databse";
public static String COL_DEFINITION="DEFINITION";
public static String COL_WORD="WORD";
public static String FTS_VIRTUAL_TABLE="FTS";

public DataBaseTable(Context context) {
    datahelper=new DatabaseOpenHelper(context);
}
private class DatabaseOpenHelper extends SQLiteOpenHelper{

    private SQLiteDatabase mDatabase;
    private Context mHelperContext;
    private String FTS_TABLE_CREATE="CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +" USING fts3 (" +COL_WORD + ", " +COL_DEFINITION + ");";


    DatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mHelperContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        db.execSQL("CREATE TABLE DICTIONARY ("+ COL_WORD+ " , "+COL_DEFINITION+");");
        db.execSQL(FTS_TABLE_CREATE);

    }




    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
        onCreate(db);


    }
}

MainActivity开始,我在DatabaseTable db=new DatabaseTable(this);方法中调用onCreate(Bundle)。但我在模拟器FileExplorer中看不到任何数据库。

我错过了什么吗?

修改 MainActivity如下

public class MainActivity extends Activity {

    DataBaseTable db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db=new DataBaseTable(this);
        handeIntent(getIntent());

    }


    private void handeIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Cursor c = db.getMatch(query, null);
            int[] to = new int[] {R.id.name,R.id.def};
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, R.layout.info, c, null, to, 0);
        ListView lv=(ListView) findViewById(R.id.listView1);
        lv.setAdapter(adapter);



    }

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handeIntent(intent);
    }    
}

1 个答案:

答案 0 :(得分:1)

从这里获取帮助 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

您需要继承数据库类SQLiteOpenHelper