Aanal Shah
Aanal Shah

Reputation: 283

Insert Integer Value in Sqlite android

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

Upvotes: 1

Views: 11261

Answers (2)

kevz
kevz

Reputation: 2737

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"  + ")";

Upvotes: 1

Ravi Theja
Ravi Theja

Reputation: 3401

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.

Upvotes: 0

Related Questions