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;
    }
}
10
  • Show create table SQL Query Commented Feb 16, 2016 at 6:17
  • Category.setID(Integer.parseInt(cursor.getString(0))); why are you parsing "Angle" to int Commented Feb 16, 2016 at 6:17
  • You show table SQLite structure and Category class Commented Feb 16, 2016 at 6:20
  • no i m not parse "angle" into int. instead of "R.drawable.angle" its take "Angle" Value and Give error Commented Feb 16, 2016 at 6:20
  • 1
    @AanalShah: try to uninstall app then run it because probably you have changed create table query after db is created Commented Feb 16, 2016 at 6:48

2 Answers 2

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"  + ")";
Sign up to request clarification or add additional context in comments.

9 Comments

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.
alright u r not inserting ID value in table so either u enter it manually or just Auto increment will do
its auto increment when i remove img column id generate automatically
just change the create table query and see if it works
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
|
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.

Comments

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.