我的 android 应用程序在数据库中包含两个表,其中一个表存储动态数据,另一个表应通过解析文本文件将近 7000 个字典单词存储到该表中。此操作应在应用程序第一次安装时执行一次。文本文件是存储在原始文件夹中。
我在这里面临的问题是,当我尝试解析并存储到该表中时,logcat 说数据库中没有这样的表。
这是方法和 DBHelper 代码段:
DBHelper 类片段:
private static final String DICTIONARY_TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE_NAME_DICTIONARY + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"words TEXT NOT NULL);";
private static final String USER_TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE_NAME_USERINFO + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"player_name TEXT NOT NULL, " +
"player_username TEXT NOT NULL, " +
"player_pass TEXT NOT NULL, " +
"player_played VARCHAR NOT NULL, " +
"player_won VARCHAR NOT NULL, " +
"player_loss VARCHAR NOT NULL);";
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(USER_TABLE_CREATE);
db.execSQL(DICTIONARY_TABLE_CREATE);
System.out.println("In onCreate");
}
catch(Exception e)
{
e.printStackTrace();
}
}
这是解析文本文件并将值插入数据库的方法:
public void CopytxtfileTodb()
{
InputStream inputStream = getResources().openRawResource(R.raw.wordslists);
BufferedReader br = new BufferedReader( new InputStreamReader(inputStream));
SQLiteDatabase db = playerDB.getWritableDatabase();
try
{
String line = "";
while((line= br.readLine()) != "")
{
ContentValues values = new ContentValues();
values.put("words", line);
try
{
db.insert(DBHelper.DATABASE_TABLE_NAME_DICTIONARY, null, values);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
catch(Exception e)
{
System.err.println("Parse Error: " + e.getMessage());
}