Search code examples
javaandroidsqlitesqliteopenhelper

Android Studio Application crash on database insertion


When I call function 'test' my app crashes. Any ideas why does that happen?

DB class

package com.example.paulcosma.app2;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DB extends SQLiteOpenHelper{
    public static final String
            dbName = "foods.db",
            tableName = "foods",
            colId = "id",colName = "name",colCarbs = "carbs";

    public DB(Context context) {
        super(context, dbName, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + dbName);
        onCreate(db);
    }

    public boolean addFood(String Name, float Carbs){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(colName,Name);
        contentValues.put(colCarbs,Carbs);
        long result = db.insert(tableName,null,contentValues);

        return result != -1; // -1 == not inserted
    }
}

MainActivity

package com.example.paulcosma.app2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    DB db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new DB(this);
    }
    public void test(View v){
        boolean insertedSuccessfully = db.addFood("Milk",4); // crashes when called
        if(insertedSuccessfully)
            Toast.makeText(this,"inserted",Toast.LENGTH_LONG).show();
        else Toast.makeText(this,"not inserted",Toast.LENGTH_LONG).show();
    }

}

logcat

01-20 19:59:47.888 9565-9565/com.example.paulcosma.app2 E/InstantRun: Could not find slices in APK; aborting. 01-20 19:59:47.988 9565-9565/com.example.paulcosma.app2 E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 01-20 19:59:48.005 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed. 01-20 19:59:48.013 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed -- 2. 01-20 19:59:48.045 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed. 01-20 20:00:02.211 9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (10) Failed to do file read, got: 0, amt: 100, last Errno: 2 01-20 20:00:02.244 9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (1) unknown database foods 01-20 20:00:02.263 9565-9565/com.example.paulcosma.app2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paulcosma.app2, PID: 9565
java.lang.IllegalStateException: Could not execute method for android:onClick


Solution

  • You've got a typo in onCreate(). You're using dbName instead of tableName to create your table, therefore when you're running your query which is looking up the correct name, you're looking up a table that doesn't exist. So:

    db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");
    

    needs to become:

    db.execSQL("create table " + tableName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");