2017-01-01 113 views
0

對於我的應用程序,我正在使用SQLite數據庫。SQLite無法在OnCreate方法中創建數據庫。 E/SQLiteLog:(1)

我遵循教程實現數據庫,它的工作。但後來我發現了一些東西......我錯過了一張額外的桌子。

當我試圖添加另一個表時,它給了我一個錯誤,所以我嘗試了很多不同的解決方案,但沒有在這裏工作。我嘗試了以下內容是:

  • 增加版本號onUpgrade方法
  • DROP DATABASE槽降聲明
  • 創建新的數據庫文件
  • 創建新的數據庫表
  • 刪除的應用程序和文件
  • 訪問設備顯示器以找到.DB文件

所有這些解決方案都不適合我。

現在有趣的部分是當我刪除我的應用程序,並嘗試上述所有其他解決方案。我什至不能創建我的第一個工作數據庫表了。這就好像我的整個數據庫已經壞了。

我不知道該怎麼做才知道。我不重視我的數據庫中的數據。我只想從一個乾淨而新鮮的數據庫開始。

,我使用來實現數據庫的故障排除工作之前的代碼,是:

public class Database extends SQLiteOpenHelper{ 

     // Fields 
     private final static int DATABASE_VERSION = 1; 
     // Table products 
     private final static String DATABASE_NAME = "product.db"; 
     public static final String TABLE_PRODUCTS = "products"; 
     public static final String ID = "_id"; 
     public static final String NAME = "name"; 
     public static final String CALORIES = "calories"; 
     public static final String PRODUCT_TYPE = "producttype"; 
     public static final String CATEGORY_TYPE = "categorytype"; 
     public static final String IMAGE = "image"; 

     // Table Calories 
     /* Second table 
     public static final String TABLE_CALORIES = "calories"; 
     public static final String CALID = "_id"; 
     public static final String AMOUNT_OF_CALORIES = "amountOfcalories"; 
     public static final String DAY_OF_THE_WEEK = "dayoftheweek"; 
     */ 
     // Table product fields 
     private String prodID = null; 
     private String prodname = null; 
     private String prodcalories = null; 
     private String prodproducttype = null; 
     private String prodcategorytype = null; 
     private String prodimage = null; 
     private String totalCalories = null; 
     private ProductType tempProductType = null; 
     private ProductType newProductType = null; 
     private CategoryType tempCatType = null; 
     private CategoryType newCategoryType = null; 
     private SQLiteDatabase database; 

     // Table Calories fields 
     private String calID = null; 
     private String calTotalCalories = null; 

     public Database(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
      super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
      System.out.println("DATABASE PATH: " + context.getDatabasePath("product.db")); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      System.out.println(db.isOpen()); 

      String create_table_products = "create table " + TABLE_PRODUCTS + "(" + 
        ID + " integer primary key auto increment " + 
        NAME + " text " + 
        CALORIES + " text " + 
        PRODUCT_TYPE + " text " + 
        CATEGORY_TYPE + " text " + 
        IMAGE+ " text " + 
        ");"; 
      db.execSQL(create_table_products); 

      /* Second table 
      String create_table_calories = "create table " + TABLE_CALORIES + "(" + 
        CALID + " integer primary key auto increment " + 
        AMOUNT_OF_CALORIES + " text " + 
        DAY_OF_THE_WEEK + " text " + 
        ");"; 
      db.execSQL(create_table_calories); 
      */ 
     } 

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

    } 

暫時代碼在OnCreate()停止創建的第一個表。它給我一個模糊的E/SQLite: (1) error。所以基本上如果我不能創建表格,我想開始新鮮的,但我不知道如何嘗試所有的東西來擺脫舊的。

在此先感謝

+3

「這給了我一個含糊的E/SQLite的:(1)錯誤」 它可能有足夠多的信息來解決它。請發佈完整的堆棧跟蹤 – e4c5

+0

愚蠢的問題。但我在哪裏可以找到E/SQLite錯誤的完整堆棧跟蹤? Android監視器 - > Logcat中的大文本轉儲? –

回答

2

嘗試更換

String create_table_products = "create table " + TABLE_PRODUCTS + "(" + 
       ID + " integer primary key auto increment " + 
       NAME + " text " + 
       CALORIES + " text " + 
       PRODUCT_TYPE + " text " + 
       CATEGORY_TYPE + " text " + 
       IMAGE+ " text " + 
       ");"; 

String create_table_products = "create table " + TABLE_PRODUCTS + "(" + 
       ID + " integer primary key autoincrement, " + 
       NAME + " text, " + 
       CALORIES + " text, " + 
       PRODUCT_TYPE + " text, " + 
       CATEGORY_TYPE + " text, " + 
       IMAGE+ " text " + 
       ");"; 
+0

感謝它的工作原理。 爲了使它工作,我不得不用我的database.db文件替換一個新的。不知道爲什麼。對此有任何想法? 我將你的awnser標記爲正確的一個 –

+0

兩點:你錯過了逗號並且有拼寫錯誤 - 'autoincrement'應該是一個單詞。 –

2

您已在創建表格查詢中忘記了逗號','。

語法應爲

CREATE TABLE mytable (
col1 int, 
col2 text); 

編輯:本來評論,但沒有足夠的代表。

+1

我用逗號試過,但沒有成功。感謝您的幫助。 –

+0

CursorFactory不是必需的。這是我的代碼片段。有用。 HTTPS://gist.github。com/stacks13/65b76b739cc7ee33e8490e847665c935 – Stacks13

+1

感謝您的幫助。在你給我你的鏈接之前,我已經修好了。儘管如此,謝謝你的努力,我給了你一點 –

相關問題