在Sqlite android中插入整数值
我试图插入整数值,但它不能接受这些值。它接受第二个字符串值并给出数据类型的异常 这是我的插入代码 这是选择代码 这是Logcat错误: 它不能接受int值。它将向上移动到“角度”值 这是我的创建表查询 这是我的分类课:在Sqlite android中插入整数值,android,sqlite,int,Android,Sqlite,Int,我试图插入整数值,但它不能接受这些值。它接受第二个字符串值并给出数据类型的异常 这是我的插入代码 这是选择代码 这是Logcat错误: 它不能接受int值。它将向上移动到“角度”值 这是我的创建表查询 这是我的分类课: 在代码行下面更改- Category.setID(Integer.parseInt(cursor.getString(0))); Category.setimg(Integer.parseInt(cursor.getString(1))); 到- 如下所示更改您的创建
在代码行下面更改-
Category.setID(Integer.parseInt(cursor.getString(0)));
Category.setimg(Integer.parseInt(cursor.getString(1)));
到-
如下所示更改您的创建表查询-
String CREATE_CategoryS_TABLE = "CREATE TABLE " + TABLE_CategoryS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG + " INTEGER," +
KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT" + ")";
如果修改数据库的表,则必须更改数据库的版本,或者可以卸载并重新安装应用程序。从游标读取数据时,在遍历游标之前,始终使用
cursor.movetoFirst
。显示创建表SQL QueryCategory.setID(Integer.parseInt(cursor.getString(0));为什么要将“Angle”解析为int呢?您显示的是表SQLite结构和类别classno我不会将“Angle”解析为int,而不是“R.drawable.Angle”它的“Angle”重视并给予error@AanalShah:尝试卸载应用程序,然后运行它,因为您可能已在创建db后更改了创建表查询。由于以下原因导致无法插入并给出错误:java.lang.IllegalStateException:无法从CursorWindow读取第0行第3列。在访问数据之前,请确保光标已正确初始化。
好的,您没有在表中插入ID值,因此您可以手动输入它,或者在删除img列ID时自动递增。自动生成只需更改创建表查询并查看它是否工作我更改查询“创建表”+TABLE_categoris+“(“+KEY_ID+”整型主键自动递增,“+KEY_IMG+”整型,“+KEY_NAME+”文本,“+KEY_SELECTION+”文本“+”)”但它会给出同样的错误
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"));
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)
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;
}
}
Category.setID(Integer.parseInt(cursor.getString(0)));
Category.setimg(Integer.parseInt(cursor.getString(1)));
Category.setID(cursor.getInt(0));
Category.setimg(cursor.getInt(1));
String CREATE_CategoryS_TABLE = "CREATE TABLE " + TABLE_CategoryS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG + " INTEGER," +
KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT" + ")";