引起:android.database.sqlite.SQLiteException:没有这样的表:

时间:2016-09-11 15:57:45

标签: android sqlite android-sqlite

我在访问应用的数据库时遇到问题。我必须从数据库加载一些数据,但我不断收到此错误:

    09-11 11:29:36.032 21955-23129/com.stella.erica.cookingtime E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
        Process: com.stella.erica.cookingtime, PID: 21955
        java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:300)
          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
          at java.util.concurrent.FutureTask.run(FutureTask.java:242)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
          at java.lang.Thread.run(Thread.java:841)
        Caused by: android.database.sqlite.SQLiteException: no such table: type (code 1): , while compiling: SELECT type, image FROM type
          at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
          at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
          at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
          at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
          at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
          at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
          at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
          at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
          at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
          at com.stella.erica.cookingtime.TypesDataSource.getListofTypes(TypesDataSource.java:36)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.getListOfChoices(ChoiceActivity.java:179)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.doInBackground(ChoiceActivity.java:108)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.doInBackground(ChoiceActivity.java:103)
          at android.os.AsyncTask$2.call(AsyncTask.java:288)
          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
          at java.lang.Thread.run(Thread.java:841) 

这是我的DBhelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

 public class TypesDBHelper extends SQLiteOpenHelper {

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table "
            + DBContracts.TypeTable.TABLE_NAME + "( "
            + DBContracts.TypeTable.COLUMN_ID + " integer primary key autoincrement, "
            + DBContracts.TypeTable.COLUMN_TYPE + " text not null,"
            + DBContracts.TypeTable.COLUMN_IMAGE + "text not null);";

    private static final String DATABASE_INSERT_VALUES = "insert into "
            + DBContracts.TypeTable.TABLE_NAME + "("
            + DBContracts.TypeTable.COLUMN_TYPE + ","
            + DBContracts.TypeTable.COLUMN_IMAGE +
            ") values ('Main course','main_course_image'), " +
            "('Side dish','side_dish_image'), " +
            "('Dessert','dessert_image'), " +
            "('Appetizer','appetizer_image'), " +
            "('Salad','salad_image'), " +
            "('Bread','bread_image'), " +
            "('Breakfast','breakfast_image')," +
            "('Soup','soup_image')," +
            "('Beverage','beverage_image')," +
            "('Sauce','sauce_image')," +
            "('Drink','drink_image')";

    public TypesDBHelper(Context context) {
        super(context, DBContracts.DATABASE_NAME, null, DBContracts.TypeTable.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
        database.execSQL(DATABASE_INSERT_VALUES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DBContracts.TypeTable.TABLE_NAME);
        onCreate(db);
    }

}

这是我的数据源访问

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class TypesDataSource {

    private SQLiteDatabase database;
    private TypesDBHelper dbHelper;

    public TypesDataSource(Context context){
        this.dbHelper = new TypesDBHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getReadableDatabase();
    }

    public void close(){
        dbHelper.close();
    }

    public ArrayList<String[]> getListofTypes(){
        ArrayList<String[]> types = new ArrayList<>();

        String[] projection = {DBContracts.TypeTable.COLUMN_TYPE, DBContracts.TypeTable.COLUMN_IMAGE};
        Cursor cursor = database.query(DBContracts.TypeTable.TABLE_NAME, projection, null, null, null, null, null);
        cursor.moveToFirst();

        while(!cursor.isAfterLast()){
            Type type = cursorToType(cursor);
            String[] typeToString = {type.getName(), type.getImageName()};
            types.add(typeToString);
            cursor.moveToNext();
        }

        cursor.close();
        return types;
    }

    private Type cursorToType(Cursor cursor){
        Type type = new Type(cursor.getString(0), cursor.getString(1));
        return type;
    }
}

这是我使用我的数据源的方法(它在我使用AsyncTask的子类中):

private ArrayList<String[]> getListOfChoices(Context context){

    TypesDataSource dataSource = new TypesDataSource(context);
    try {
        dataSource.open();
    }catch (SQLException e){
        //todo
        Log.e("DBEX", "ECCEZIONE NEL DB");
    }

    ArrayList<String[]> typeList = dataSource.getListofTypes();
    dataSource.close();

    return typeList;            
}

我无法弄清楚为什么我一直得到这个错误,特别是因为我在同一个数据库中有另一个表(代码几乎完全相同),并且我没有访问它的问题。 在此先感谢您的帮助。

编辑:我按照建议改变了列名,但是新列名称出现了同样的错误。我试图在我发布的链接中寻找答案,但我在那里找不到......

1 个答案:

答案 0 :(得分:2)

您的错误日志似乎在抱怨以下SELECT查询:

SELECT type, image FROM type

type不是SQLite keyword,但可能的一种解释是SQLite无法正确解析type对象,因为它同时显示为列和表。一种解决方法可能是使用表的完全限定名称,包括它所在的数据库,例如

SELECT type, image FROM yourdb.type