gpt4 book ai didi

android - SQLiteDatabase 表不是在 Android 中创建的

转载 作者:行者123 更新时间:2023-11-30 03:30:26 24 4
gpt4 key购买 nike

我一直在尝试构建一个带有搜索功能的数据库应用程序,该应用程序以 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 中,我在 onCreate(Bundle) 方法中调用 DatabaseTable db=new DatabaseTable(this);。但是我在模拟器 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);
}
}

最佳答案

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

需要继承数据库类SQLiteOpenHelper

关于android - SQLiteDatabase 表不是在 Android 中创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com