Java Android studio SQL找不到表
我在使用Java Android studio SQL找不到表,java,android,sqlite,Java,Android,Sqlite,我在使用SQLiteDatabase时遇到了一个奇怪的问题-当我尝试SELECT*FROM表时,它给了我以下信息: no such table: test_table (code 1): , while compiling: SELECT * FROM test_table 但是,当我插入到同一个表中时,不会发送任何异常。 这是我的代码: 从表中获取所有数据的方法: public List<Test> GetAllTests() { List<Test> tes
SQLiteDatabase
时遇到了一个奇怪的问题-当我尝试SELECT*FROM
表时,它给了我以下信息:
no such table: test_table (code 1): , while compiling: SELECT * FROM test_table
但是,当我插入到同一个表中时,不会发送任何异常
。
这是我的代码:
从表中获取所有数据的方法:
public List<Test> GetAllTests()
{
List<Test> tests =new ArrayList<Test>();
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + TEST_TABLE;
Cursor cursor = db.rawQuery(sql,null);
if(cursor.moveToFirst())
{
do
{
Test test = new Test();
test.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
test.setSubjectID(cursor.getInt(cursor.getColumnIndex(KEY_SUBJECT_ID)));
String dateString = cursor.getString(cursor.getColumnIndex(KEY_TEST_DATE));
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = null;
try {
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
test.setDate(date);
test.setGrade(cursor.getDouble(cursor.getColumnIndex(KEY_TEST_GRADE)));
tests.add(test);
}while(cursor.moveToNext());
}
return tests;
}
插入和获取数据的代码:
Test test = new Test(testSubjectID.getSelectedItemPosition(),date);
AddTestToList(test);
try {
dbManager.InsertTest(test);
List<Test> tests = dbManager.GetAllTests();
for (Test test:tests)
{
AddTestToList(test);
}
}catch (Exception e){Log.d("MSG_ERROR",e.toString());}
Test Test=新测试(testSubjectID.getSelectedItemPosition(),日期);
AddTestToList(测试);
试一试{
dbManager.InsertTest(test);
List tests=dbManager.GetAllTests();
用于(测试:测试)
{
AddTestToList(测试);
}
}catch(异常e){Log.d(“MSG_ERROR”,e.toString());}
您的设备上有一个旧版本的数据库,它没有测试表
表
解决方案:卸载应用程序并重新安装您的设备上有一个旧版本的数据库,它没有
测试表
表
解决方案:卸载应用程序并重新安装供参考
不是一个好方法。如果你的应用程序在PLAYSTORE上,那么应用程序卸载
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
if (oldVersion < 3){ // Here I SET 3. You should use your DB version . When-ever **`adding table`** please increase DB version .
db.execSQL(ALTER_USER_TABLE_1);
db.execSQL(ALTER_USER_TABLE_2);
}
}
FYI
不是一个好方法。如果你的应用程序在PLAYSTORE上,那么应用程序卸载
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
if (oldVersion < 3){ // Here I SET 3. You should use your DB version . When-ever **`adding table`** please increase DB version .
db.execSQL(ALTER_USER_TABLE_1);
db.execSQL(ALTER_USER_TABLE_2);
}
}
卸载你的应用并重新安装修复了它:)卸载并重新安装?是的,哈哈worked@IntelliJAmiya它需要刷新。卸载你的应用并重新安装修复了它:)卸载并重新安装?是的,哈哈worked@IntelliJAmiya它需要刷新。
private static final String ALTER_USER_TABLE_1 = "ALTER TABLE test_table ADD FIELD_NAME TEXT";
private static final String ALTER_USER_TABLE_2 = "ALTER TABLE test_table ADD FIELD_NAME2 TEXT";