2016-12-06 67 views
-1

我得到這個SQLiteException:沒有這樣的表。
我無法在代碼中看到任何錯誤。我已嘗試清理,卸載應用程序並重新安裝,但它沒有奏效。我該如何解決: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()方法內執行了爲所有表創建表查詢時,我完全感到困惑。其他表正在創建,只有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; 
    } 

我嘗試了這些東西:

  • 清洗和重建項目。
  • 更改數據庫版本。
  • Unistalling the application and again reinstalling。
  • 清除緩存和應用程序的數據。

什麼都沒有爲我工作。

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 

而excecuting的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) 

回答

2

我想你忘記在CREATE_TABLE3查詢中加上右括號。它應該是這樣的:

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

感謝一噸,你救了我的一天。 –

+0

:)。不用謝 – Reena

0

爲CREATE_TABLE3關閉括號。 ')'在語句結尾缺失

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) { 

    } 
相關問題