0

I'm trying to keep the Date in String format in a database but it always returns me -1. why is it happening?

public boolean addDate(String item1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL1, item1);

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

Here is the DataBaseHelper class -

package com.exemple.myapplication;

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

/**
 * Created by Mitch on 2016-05-13.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "mylist.db";
    public static final String TABLE_NAME = "mylist_data";
    public static final String COL1 = "Date";
    public static final String COL2 = "Distance";
    public static final String COL3 = "Time";


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

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

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

    public boolean addDate(String item1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL1, item1);

        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;
        }
    }
    public boolean addDis(String item1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item1);

        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;
        }
    }
    public boolean addTime(String item1) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL3, item1);

        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;
        }
    }
    public Cursor getListContents(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }
}
4
  • 1
    I don't think we have enough information here. What are the values of COL1 and item1? Can you show us your CREATE TABLE statement so we know what your table is supposed to look like? Commented Feb 2, 2018 at 16:38
  • please have a look at my update Commented Feb 2, 2018 at 16:41
  • Never use insert(); replace it with insertOrThrow(). Commented Feb 2, 2018 at 18:11
  • what is the difference? Commented Feb 2, 2018 at 18:40

2 Answers 2

3

You don't have a column called Date in your table (nor Distance or Time for that matter). You'll need to change your CREATE TABLE statement to include these columns:

String createTable = "CREATE TABLE " + TABLE_NAME 
        + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " 
        + COL1 + " TEXT, "
        + COL2 + " TEXT, "
        + COL3 + " TEXT, "
        + "ITEM1 TEXT)";

This is assuming that all the columns will be of type TEXT and that you still need the column ITEM1. If not, then just delete that line and close the bracket after COL3.

Sign up to request clarification or add additional context in comments.

Comments

1

it's becuase you are using COL1 as key but equals to ="Date" but table mylist_data contains only one column named "ITEM1" so please replace COL1 = "Date" to "ITEM1".

4 Comments

Be sure to use correct quotes. In Java, '' is only used for single characters.
I couldn't understand so good what do you mean would you like to please show me the changes in the code?
Please replace follow line public static final String COL1 = "Date" to public static final String COL1 = "ITEM1";
Wouldn't it make more sense to do this the opposite way round in this case? I.e. change ITEM1 to Date in the CREATE TABLE statement, to make the table more easily understandable? And with this answer, OP would still be missing the correct columns for the addDis() and addTime() methods.

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.