【问题标题】:Insert Integer Value in Sqlite android在Sqlite android中插入整数值
【发布时间】:2016-05-27 06:59:57
【问题描述】:

我正在尝试插入整数值,但它不能接受这些值。它采用第二个字符串值并给出数据类型的异常

这是我的插入代码

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 value 。它向上移动到“角度”值

这是我的创建表查询

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

【问题讨论】:

  • 显示创建表 SQL 查询
  • Category.setID(Integer.parseInt(cursor.getString(0)));你为什么将“角度”解析为 int
  • 你展示表SQLite结构和Category类
  • 不,我没有将“角度”解析为 int。而不是“R.drawable.angle”它采用“角度”值并给出错误
  • @AanalShah:尝试卸载应用程序然后运行它,因为您可能在创建数据库后更改了创建表查询

标签: android sqlite int


【解决方案1】:

更改以下代码行 -

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

【讨论】:

  • 无法插入并报错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.
  • 好吧,你没有在表中插入 ID 值,所以你要么手动输入,要么只是自动递增
  • 当我删除 img 列 id 时它的自动增量会自动生成
  • 只需更改创建表查询,看看它是否有效
  • 我更改了查询`“CREATE TABLE”+TABLE_CategoryS+“(”+KEY_ID+“INTEGER PRIMARY KEY AUTOINCREMENT”+KEY_IMG+“INTEGER”+KEY_NAME+“TEXT”+KEY_SELECTION+ " TEXT" + ")";` 但它给出了同样的错误
【解决方案2】:

如果你修改了你的数据库表,你必须更改数据库的版本,或者你可以卸载并重新安装应用程序。从光标读取时,请始终在遍历光标之前使用cursor.movetoFirst

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2012-04-28
    • 2013-06-18
    相关资源
    最近更新 更多