我该如何解决:SQLiteException:没有这样的表?

时间:2016-12-06 06:19:02

标签: android database sqlite android-sqlite

我收到了这个SQLiteException:没有这样的表 我无法在代码中看到任何错误。我已经尝试过清理,卸载应用程序而不是再次重新安装,但它没有用。

代码:
DatabaseContainerAdapter.java

package com.example.batrad.expenseassist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.Editable;

/**
 * Created by batrad on 11/19/2016.
 */
public class DatabaseContainerAapter {
    DatabaseContainer dc;

    DatabaseContainerAapter(Context context) {
        dc = new DatabaseContainer(context);
    }


    public Cursor showSubData(String[] coloumns, String[] categoryNameandMonth) {
        Cursor cr = null;
        SQLiteDatabase db = dc.getWritableDatabase();
        try {
            Cursor cursor = db.query(DatabaseContainer.TABLE_NAME2, coloumns, DatabaseContainer.CATEGORY + "=? and " + DatabaseContainer.MONTH + "=?", categoryNameandMonth, null, null, null);
            cr = cursor;
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return cr;

    }


    public Cursor showData(String[] coloumns, String[] month) {
        Cursor cr = null;
        SQLiteDatabase db = dc.getWritableDatabase();
        try {
            Cursor cursor = db.query(DatabaseContainer.TABLE_NAME1, coloumns, DatabaseContainer.MONTH + "=?", month, null, null, null);
            cr = cursor;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cr;

    }

    public Cursor showDataForParticularCategory(String[] coloumns, String[] categoryArray) {
        Cursor cr = null;
        SQLiteDatabase db = dc.getWritableDatabase();
        try {
            Cursor cursor = db.query(DatabaseContainer.TABLE_NAME1, coloumns, DatabaseContainer.CATEGORY + "=?", categoryArray, null, null, null);
            cr = cursor;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cr;

    }


    public long insertSubCategory(String category, String subcategory, String date, int amount, String month) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.CATEGORY, category);
        cv.put(DatabaseContainer.DATE, date);
        cv.put(DatabaseContainer.SUBAMOUNT, amount);
        cv.put(DatabaseContainer.SUBNAME, subcategory);
        cv.put(DatabaseContainer.MONTH, month);
//        cv.put(DatabaseContainer.ID, System.currentTimeMillis());
        long id = db.insert(DatabaseContainer.TABLE_NAME2, null, cv);
        return id;
    }


    public long insertCategory(String categoryName, String month, int amount, int icon) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.MONTH, month);
        cv.put(DatabaseContainer.AMOUNT, amount);
        cv.put(DatabaseContainer.ICON, icon);
        cv.put(DatabaseContainer.CATEGORY, categoryName);
//        cv.put(DatabaseContainer.ID, System.currentTimeMillis());
        long id = db.insert(DatabaseContainer.TABLE_NAME1, null, cv);
        return id;
    }

    public long updateCategory(int totalAmount, String[] categoryName) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.AMOUNT, totalAmount);
        long id = db.update(DatabaseContainer.TABLE_NAME1, cv, DatabaseContainer.CATEGORY + "=?", categoryName);
        return id;
    }

    public Cursor showBudget(String[] coloumns, String[] month) {
        Cursor cr = null;
        SQLiteDatabase db = dc.getWritableDatabase();
        try {
            Cursor cursor = db.query(DatabaseContainer.TABLE_NAME3, coloumns, DatabaseContainer.MONTH + "=?", month, null, null, null);
            cr = cursor;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cr;
    }

    public long insertBudget(String budget, int month) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.MONTH, month);
        cv.put(DatabaseContainer.BUDGET, budget);

//        cv.put(DatabaseContainer.ID, System.currentTimeMillis());
        long id = db.insert(DatabaseContainer.TABLE_NAME3, null, cv);
        return id;
    }

    public long updateBudget(String budget, String[] month) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.BUDGET, budget);
        long id = db.update(DatabaseContainer.TABLE_NAME3, cv, DatabaseContainer.MONTH + "=?", month);
        return id;
    }


    class DatabaseContainer extends SQLiteOpenHelper {

        public static final String DATABASE_NAME = "expenseDatabase";
        public static final int DATABASE_VERSION = 7;
        public static final String TABLE_NAME1 = "categorytable";
        public static final String CATEGORY = "category";
        public static final String MONTH = "month";
        public static final String AMOUNT = "amount";
        public static final String ICON = "icon";
        public static final String TABLE_NAME2 = "subcategorytable";
        public static final String SUBNAME = "subname";
        public static final String SUBAMOUNT = "subamount";
        public static final String DATE = "date";

        public static final String TABLE_NAME3 = "budgetTable";
        public static final String YEAR = "year";
        public static final String BUDGET = "budget";

        public static final String CREATE_TABLE1 = "CREATE TABLE " + TABLE_NAME1 + " (" + CATEGORY + " varchar (20) ," + MONTH + " varchar(150)," + AMOUNT + " integer (10)," + ICON + " varchar(50) );";
        public static final String DROP_TABLE1 = "DROP TABLE IF EXISTS " + TABLE_NAME1;
        public static final String CREATE_TABLE2 = "CREATE TABLE " + TABLE_NAME2 + " (" + MONTH + " varchar (20) ," + CATEGORY + " varchar (20) ," + SUBNAME + " varchar (20) ," + SUBAMOUNT + " integer(10)," + DATE + " varchar(20));";
        public static final String DROP_TABLE2 = "DROP TABLE IF EXISTS " + TABLE_NAME2;
        public static final String CREATE_TABLE3 = "CREATE TABLE " + TABLE_NAME3 + " (" + MONTH + " varchar (20) ," + YEAR + " varchar (20) ," +  BUDGET+" integer(10) ;";
        public static final String DROP_TABLE3 = "DROP TABLE IF EXISTS " + TABLE_NAME3;

        public DatabaseContainer(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);


        }


        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            try {
                sqLiteDatabase.execSQL(CREATE_TABLE1);
                sqLiteDatabase.execSQL(CREATE_TABLE2);
                sqLiteDatabase.execSQL(CREATE_TABLE3);
            } catch (SQLException e) {
e.printStackTrace();
            }


        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
            try {
                sqLiteDatabase.execSQL(DROP_TABLE1);
                sqLiteDatabase.execSQL(DROP_TABLE2);
                sqLiteDatabase.execSQL(DROP_TABLE3);
            } catch (SQLException e) {
            }
            onCreate(sqLiteDatabase);


        }
    }

}

异常仅适用于TABLE_NAME3,其他表格正常 所以我完全感到困惑,因为控件进入onCreate方法(我在调试器中检查过)和onCreate()方法内部我正在为所有表执行Create Table查询。正在创建其他表,仅在TABLE_NAME3上获取异常。

我在上面提到的代码中使用的这三种方法(DatabaseContainerAdapter.java)。

    public Cursor showBudget(String[] coloumns, String[] month) {
        Cursor cr = null;
        SQLiteDatabase db = dc.getWritableDatabase();
        try {
            Cursor cursor = db.query(DatabaseContainer.TABLE_NAME3, coloumns, DatabaseContainer.MONTH + "=?", month, null, null, null);
            cr = cursor;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cr;
    }

    public long insertBudget(String budget, int month) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.MONTH, month);
        cv.put(DatabaseContainer.BUDGET, budget);

//        cv.put(DatabaseContainer.ID, System.currentTimeMillis());
        long id = db.insert(DatabaseContainer.TABLE_NAME3, null, cv);
        return id;
    }

    public long updateBudget(String budget, String[] month) {
        SQLiteDatabase db = dc.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DatabaseContainer.BUDGET, budget);
        long id = db.update(DatabaseContainer.TABLE_NAME3, cv, DatabaseContainer.MONTH + "=?", month);
        return id;
    }

我尝试过这些东西:

  • 清理和重建项目。
  • 更改数据库版本。
  • 取消应用程序并再次重新安装。
  • 清除缓存和应用的数据。

没有什么对我有用。

Exception 
 13658-13658/com.example.batrad.expenseassist E/SQLiteLog: (1) no such table: budgetTable
12-06 11:26:24.849 13658-13658/com.example.batrad.expenseassist W/System.err: android.database.sqlite.SQLiteException: no such table: budgetTable (code 1): , while compiling: SELECT budget FROM budgetTable WHERE month=?
12-06 11:26:24.849 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1355)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1202)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1073)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1241)
12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at 

    com.example.batrad.expenseassist.DatabaseContainerAapter.showBudget(DatabaseContainerAapter.java:103)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at com.example.batrad.expenseassist.MainActivity.setBudget(MainActivity.java:82)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at com.example.batrad.expenseassist.MainActivity.onCreate(MainActivity.java:76)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.Activity.performCreate(Activity.java:6304)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.ActivityThread.access$900(ActivityThread.java:159)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    12-06 11:26:24.850 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.os.Looper.loop(Looper.java:152)
    12-06 11:26:24.851 13658-13658/com.example.batrad.expenseassist W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5507)
    12-06 11:26:24.851 13658-13658/com.example.batrad.expenseassist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    12-06 11:26:24.851 13658-13658/com.example.batrad.expenseassist W/System.err:     at 

在执行updateBudget方法时遇到此错误:

12-06 11:46:03.470 20868-20868/com.example.batrad.expenseassist E/SQLiteDatabase: Error inserting month=11 budget=537
                                                                                  android.database.sqlite.SQLiteException: no such table: budgetTable (code 1): , while compiling: INSERT INTO budgetTable(month,budget) VALUES (?,?)
                                                                                      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.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1508)
                                                                                      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1380)
                                                                                      at com.example.batrad.expenseassist.DatabaseContainerAapter.insertBudget(DatabaseContainerAapter.java:118)
                                                                                      at com.example.batrad.expenseassist.BudgetDialogFragment$2$1.onClick(BudgetDialogFragment.java:55)
                                                                                      at android.view.View.performClick(View.java:5233)
                                                                                      at android.view.View$PerformClick.run(View.java:21209)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:152)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5507)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    12-06 11:26:24.851 13658-13658/com.example.batrad.expenseassist W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:2)

我认为您忘记在CREATE_TABLE3查询中添加结束括号。它应该是这样的:

public static final String CREATE_TABLE3 = "CREATE TABLE " + TABLE_NAME3 
                                           + " (" + MONTH + " varchar (20) ,"
                                           + YEAR + " varchar (20) ," +  BUDGET
                                           + " integer(10)) ;";

答案 1 :(得分:0)

关闭CREATE_TABLE3的大括号。 &#39;)&#39;在声明结尾处遗漏

答案 2 :(得分:0)

首先,您扩展SQLiteOpenHelper类并在onCreate()方法中创建表。

请检查以下代码 -

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import java.util.ArrayList;

/**
 *
 */
public class DatabaseController extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "meeting_finder_db";


    // table name
    private static final String TABLE_APP_INFO = "tbl_app_info";



    // TABLE_APP_INFO Columns names
    private static final String KEY_APP_ID = "app_id";
    private static final String KEY_APP_NAME = "app_name";
    private static final String KEY_APP_IMAGE = "app_image";
    private static final String KEY_APP_STATUS = "app_status";
    private static final String KEY_APP_ORDER = "app_order";
    private static final String KEY_APP_SOCIAL_URL = "app_social_url";
    private static final String KEY_CREATED_AT = "createdAt";
    private static final String KEY_UPDATED_AT = "updatedAt";





  public DatabaseController(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      //  Create APP table 
        String CREATE_APP_INFO = "CREATE TABLE " + TABLE_APP_INFO + "("
                + KEY_APP_ID + " INTEGER PRIMARY KEY," + KEY_APP_NAME + " TEXT,"
                + KEY_APP_IMAGE + " TEXT," + KEY_APP_STATUS + " TEXT," + KEY_APP_ORDER + " TEXT,"
                + KEY_APP_SOCIAL_URL + " TEXT," + KEY_CREATED_AT + " TEXT," + KEY_UPDATED_AT + " TEXT" + ");";



        db.execSQL(CREATE_APP_INFO);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }