2016-07-08 14 views
-1

teH SQLiteデータベースを使い始めようとしています。残念ながら、私のonCreateメソッドでは、データベースが作成されることはなく、クラッシュすると誰も私を助けられません。 エラーが発生しました:そのようなテーブルはありません:firstdatabase。SQLIteデータベーステーブルを作成していません

これは私のMySQLiteHelperクラスです:

public class MySQLiteHelper extends SQLiteOpenHelper { 


    //Database instance 
    //private static MySQLiteHelper sInstance; 

    //Database name 

    private static final String DATABASE_NAME = "TestDB"; 

    //Table name 
    private static final String TABLE_NAME = "firstdatabase"; 
    //column names of the table 
    private static final String KEY_ID = "id"; 
    private static final String KEY_ISBN = "isbn"; 
    private static final String KEY_INGREDIENTS = "ingredients"; 

    //Database Version 
    private static final int DATABASE_VERSION = 2; 

    // Log TAG for debugging purpose 
    private static final String TAG = "SQLiteAppLog"; 

    //Method that will ensure only one instance of the database is created. 
    /* public static synchronized MySQLiteHelper getsInstance(Context context){ 
     if(sInstance == null){ 
      sInstance = new MySQLiteHelper(context.getApplicationContext()); 
     } 
     return sInstance; 
    } 
    */ 

    // Constructor 
    public MySQLiteHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     Log.d(TAG, "Inside SQLITEHELPER METHOD()"); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     //SQLite statement to create a table called 'TestDB'. 
     String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ISBN + " TEXT," 
       + KEY_INGREDIENTS + " TEXT" + ")"; 

     Log.d(TAG, "DB created"); 
     db.execSQL(CREATE_PROTOTYPE_TABLE); 
     //Log.d(TAG, "DB created"); 

    } 

    // onUpdate() is invoked when you upgrade the database scheme. 
    // Don’t consider it seriously for the sample app. 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older prototype table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 

     this.onCreate(db); 
    } 

    public void addIngredientsToTable(Product product){ 

     Log.d(TAG, "adding ingredients to table"); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_ISBN, product.getIsbn()); 
     values.put(KEY_ISBN, product.getIngredients()); 

     db.insert(TABLE_NAME,null, values); 
     db.close(); 
    } 

    //Method to get the ingredients list based on the isbn passed in. 
    public ArrayList<String> getIngredients(String isbn){ 
     ArrayList<String> ingredientList = new ArrayList<String>(); 
     String isbnPassedIn = isbn; 

     String query = "SELECT isbn, ingredients FROM " + TABLE_NAME + " WHERE isbn = " + isbnPassedIn; 

     //Getting instance to a readable database 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 

     if(cursor.moveToFirst()){ 
      do{ 
       ingredientList.add(cursor.getString(1)); 
       ingredientList.add(cursor.getString(2)); 
      }while (cursor.moveToFirst()); 

     } 
     Log.d(TAG, "Inside getIngredients()"); 
     return ingredientList; 
    } 


    //Method to get all the list of products in the datbase. 
    public ArrayList<Product> getAllProducts(){ 
     ArrayList<Product> productList = new ArrayList<Product>(); 

     String selectQuery = "SELECT * FROM firstdatabase"; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery,null); 

     if(cursor.moveToFirst()){ 
      do{ 
       Product product = new Product(); 
       product.setId(Integer.parseInt(cursor.getString(0))); 
       product.setIsbn(cursor.getString(1)); 
       product.setIngredients(cursor.getString(2)); 
       productList.add(product); 
      }while (cursor.moveToNext()); 
     } 
     return productList; 

    } 

Javaファイル:

public class BarCodeActivity extends AppCompatActivity { 

    MySQLiteHelper db1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_bar_code); 
     db1 = new MySQLiteHelper(this); 
     Log.d("Insert", "Inserting data..."); 
     db1.addIngredientsToTable(new Product(1, "89470001033", "Lowfat yogurt, mango, milk, water")); 

     Log.d("Reading: ", "Reading all products"); 
     ArrayList<Product> products = db1.getAllProducts(); 

     for(Product product : products){ 
      String log = "Id: " + product.getId() + ", ISBN: " + product.getIsbn() + ", Ingredients: " + product.getIngredients(); 
      Log.d("Product:", log); 
     } 
+2

間違ったファイルをコピーしましたか?同じファイルの2倍です。 'MySQLiteHelper'がありません。 – dipdipdip

+1

はい、正しいファイルとエラー** logcat **を投稿してください。 – Vucko

+0

@stackoverflow.com/questions/6554269/android-sqlite-no-such-table-errorの可能な複製 –

答えて

1

あなたCREATE_PROTOTYPE_TABLE文字列が正しくありません、セミコロンが欠落しています(;):

は、次を使用します。

String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_ISBN + " TEXT," 
       + KEY_INGREDIENTS + " TEXT" + ");"; 
+0

それがあなたのために働いていたら、私の答えを投票してください。 – Saini

関連する問題