Search code examples
androiddatabasesqliteandroid-sqlite

error when adding data in my database


I am trying to configure a simple SQLiteOpenHelper, and getting an error while executing. android.database.sqlite.SQLiteException: table people_table has no column named Minutes (code 1): , while compiling: INSERT INTO people_table(Minutes,Hour) VALUES (?,?) I canot figure out what is the problem?

  package radiofm.arabel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DatabaseHelper";

    private static final String TABLE_NAME = "people_table";
    private static final String COL1 = "ID";
    private static final String COL2 = "Hour";
    private static final String COL3 = "Minutes";



    public DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String createTable = "CREATE TABLE " + TABLE_NAME + " (" +

                COL1 + "ID INTEGER PRIMARY KEY AUTOINCREMENT," +

                COL2 + "TEXT," +

                COL3 + "TEXT "  +

                ");";

        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean addData(String item1,String item2) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item1);
        contentValues.put(COL3, item2);

        Log.d(TAG, "addData: Adding " + item1 + " to " + TABLE_NAME);


        long result = db.insert(TABLE_NAME, null, contentValues);

        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }


}

Solution

  • Your onCreate seems to be missing spaces on your SQL column definition. Also, your id columns must to be defined as _id.

    Try this.

     private static final String COL1 = "_id";
    

    And

    @Override
        public void onCreate(SQLiteDatabase db) {
    
            String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
    
                    COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    
                    COL2 + " TEXT, " +
    
                    COL3 + " TEXT "  +
    
                    ");";
    
            db.execSQL(createTable);
        }