我试图插入Integer值但它不能接受这些值。取第二个String值并给出数据类型的异常
这是我的插入代码
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
}
这是选择代码
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;
}
这是价值观
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"));
这是Logcat错误:
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)
它无法获取int值。它向上移动到角度&#34;值
这是我的创建表查询
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);
}
这是我的分类:
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;
}
}
答案 0 :(得分:1)
更改代码中的行 -
Object
到 -
Category.setID(Integer.parseInt(cursor.getString(0)));
Category.setimg(Integer.parseInt(cursor.getString(1)));
更改您的创建表查询,如下所示 -
Category.setID(cursor.getInt(0));
Category.setimg(cursor.getInt(1));
答案 1 :(得分:0)
如果修改数据库的表,则必须更改数据库的版本,或者可以卸载并重新安装应用程序。从光标读取时,在遍历游标之前始终使用cursor.movetoFirst
。