Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android studio SQL找不到表_Java_Android_Sqlite - Fatal编程技术网

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";