0

teH I am trying to get started with SQLite databases. Unfortunately, on my onCreate method, my database is never getting created and crashing can anyone help me out. Error I get is: No such table: firstdatabase.

This is my MySQLiteHelper class:

    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 file:

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);
        }
5
  • 2
    Did you copy the wrong file? It's 2x the same file. MySQLiteHelper is missing. Commented Jul 8, 2016 at 14:44
  • 1
    Yes, please post the right file, as well as the error logcat. Commented Jul 8, 2016 at 14:46
  • Possible duplicate of stackoverflow.com/questions/6554269/… Commented Jul 8, 2016 at 14:50
  • @Saini this is not a joke, just merely a mistake. Commented Jul 8, 2016 at 14:52
  • ok sorry, i am just asking for the code. Commented Jul 8, 2016 at 14:58

1 Answer 1

4

Your CREATE_PROTOTYPE_TABLE String is not correct, the semicolon is missing(;) :

Use the following:

String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_ISBN + " TEXT,"
                + KEY_INGREDIENTS + " TEXT" + ");";
Sign up to request clarification or add additional context in comments.

1 Comment

if it worked for you, then please up vote my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.