1

I m trying to insert Integer value but its can't accept the values.its take second String value and give Exception of datatype

this is my inserting code

void addCategory(Category Category) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_IMG, Category.getimg()); // Category Name
    values.put(KEY_NAME, Category.getName()); // Category Name
    values.put(KEY_SELECTION, Category.isSelected()); // Category Phone

    // Inserting Row
    db.insert(TABLE_CategoryS, null, values);
    db.close(); // Closing database connection
}

This is Selection Code

public List<Category> getAllCategorys() {
    List<Category> CategoryList = new ArrayList<Category>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CategoryS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToNext()) {
        do {
            Category Category = new Category();
            Category.setID(Integer.parseInt(cursor.getString(0)));
            Category.setimg(Integer.parseInt(cursor.getString(1)));
            Category.setName(cursor.getString(2));
            Category.setSelected(cursor.getString(3));
            CategoryList.add(Category);
        } while (cursor.moveToNext());
    }

    // return Category list
    return CategoryList;
}

this is Values

db.addCategory(new Category(R.drawable.angle, "Angle", "true"));
db.addCategory(new Category(R.drawable.angle, "Area", "true"));
db.addCategory(new Category(R.drawable.angle,"Currency", "true"));
db.addCategory(new Category(R.drawable.angle,"Current", "true"));
db.addCategory(new Category(R.drawable.angle,"Density", "true"));
db.addCategory(new Category(R.drawable.angle,"Length", "true"));

This is Logcat error:

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
    Caused by: java.lang.NumberFormatException: Invalid int: "Angle"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)

its can't take int value . its move up to "Angle" value

this is my create table query

public void onCreate(SQLiteDatabase db) {
    String CREATE_CategoryS_TABLE = "CREATE TABLE " + TABLE_CategoryS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_IMG + "INTEGER," +
        KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT"  + ")";
    db.execSQL(CREATE_CategoryS_TABLE);
}

This is my Category Class:

public class Category 
{
    int id;
    int img;
    String name = null;
    String selected = "true";
    public Category()
    {

    }

    public Category(int img, String name, String selected) {

        this.name = name;
        this.selected = selected;
        this.img = img;
    }
    public Category(int id,int img, String name, String selected) {

        this.id = id;
        this.name = name;
        this.selected = selected;
        this.img = img;
    }

    public int getID() { return id; }

    public void setID(int id) {
        this.id = id;
    }

    public int getimg() { return img; }

    public void setimg(int img) {
        this.img = img;
    }

    public String getName() { return name; }

    public void setName(String name) {
        this.name = name;
    }

    public String isSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }
}
Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
Aanal Shah
  • 273
  • 1
  • 2
  • 14

2 Answers2

1

change below lines in code -

Category.setID(Integer.parseInt(cursor.getString(0)));
Category.setimg(Integer.parseInt(cursor.getString(1)));

To -

Category.setID(cursor.getInt(0));
Category.setimg(cursor.getInt(1));

change your create table query as below -

String CREATE_CategoryS_TABLE = "CREATE TABLE " + TABLE_CategoryS + "("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG + " INTEGER," +
        KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT"  + ")";
kevz
  • 2,727
  • 14
  • 39
  • its can't insert and give error `Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.` – Aanal Shah Feb 16 '16 at 06:28
  • alright u r not inserting ID value in table so either u enter it manually or just Auto increment will do – kevz Feb 16 '16 at 06:30
  • its auto increment when i remove img column id generate automatically – Aanal Shah Feb 16 '16 at 06:31
  • just change the create table query and see if it works – kevz Feb 16 '16 at 06:34
  • i change the query ` "CREATE TABLE " + TABLE_CategoryS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG + "INTEGER," + KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT" + ")";` but its give same error – Aanal Shah Feb 16 '16 at 06:34
  • I guess the problem is while retrieving and not while insertion. – kevz Feb 16 '16 at 06:41
  • its not insert 3rd row – Aanal Shah Feb 16 '16 at 06:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103568/discussion-between-kevz-and-aanal-shah). – kevz Feb 16 '16 at 06:44
  • der shd be space KEY_IMG + " INTEGER," – kevz Feb 16 '16 at 06:48
0

if you modify the table of your db you have to change the version of the db or you can uninstall and reinstall the app.While reading from cursor,always use cursor.movetoFirst before traversing the cursor.

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34