Adding table data before creating SQLite database

时间:2016-07-11 19:36:49

标签: android android-sqlite

I am creating an SQLite database within an application that scans bar codes. The issue is the application only recognises the first entry and not the rest. I have log statements that I use to debug and I noticed that the tag adding ingredients to table is being called before DB created. I am unsure about why this is happening. Here is my code:

public class MySQLiteHelper  extends SQLiteOpenHelper {

    //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 = 3;

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

    //Method that will ensure only one instance of the database is created.

    // 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 autoincrement," + KEY_ISBN + " TEXT,"
                + KEY_INGREDIENTS + " TEXT" + ");";

        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_ID, product.getId());
        values.put(KEY_ISBN, product.getIsbn());
        values.put(KEY_INGREDIENTS, 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{
                Product product = new Product();
                product.setIsbn(cursor.getString(0));
                product.setIngredients(cursor.getString(1));

                ingredientList.add(product.getIsbn());
                ingredientList.add(product.getIngredients());

                Log.d("MYSQLITEHELPER", product.getIsbn());
                Log.d("MYSQLITEHELPER", product.getIngredients());
            }while (cursor.moveToNext());

        }
        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;

    }

}

Any suggestions on how to fix this problem?

0 个答案:

没有答案