从Android中的SQLite数据库中获取数据?

时间:2019-03-04 05:22:09

标签: android sqlite

在这里,我正在从android中的SQLite中获取数据,但问题是数据库中总共有20列,其正确获取了19列,但是当我获取最后20列时,整个array []为空

例如 当我写查询

SELECT NEW_ANIMAL_ID,TAG_NO,FARMER_ID,ANIMAL_EAR_POSITION,ANIMAL_SPECIES,ANIMAL_BREED,ANIMAL_BODY_COLOR,ANIMAL_SHAPE_RIGHT,ANIMAL_SHAPE_LEFT,ANIMAL_SWITCH_OF_TAIL,AGE_YEARS,ANIMAL_OTHER_MARKS,PRAG_STATUS,NUMBER_OF_LACTATION,CURRENT_MILK,SUM_INSURED,TAG_IMAGE,HEAD_IMAGE,LEFT_SIDE_IMAGE,RIGHT_SIDE_IMAGE  FROM  ANIMAL_SYNC_TABLE    ORDER BY  NEW_ANIMAL_ID   DESC

它完全获取19列

但是当我添加最后一个列名并尝试获取它时,显示一个空数组[]

Query = SELECT NEW_ANIMAL_ID,TAG_NO,FARMER_ID,ANIMAL_EAR_POSITION,ANIMAL_SPECIES,ANIMAL_BREED,ANIMAL_BODY_COLOR,ANIMAL_SHAPE_RIGHT,ANIMAL_SHAPE_LEFT,ANIMAL_SWITCH_OF_TAIL,AGE_YEARS,ANIMAL_OTHER_MARKS,PRAG_STATUS,NUMBER_OF_LACTATION,CURRENT_MILK,SUM_INSURED,TAG_IMAGE,HEAD_IMAGE,LEFT_SIDE_IMAGE,RIGHT_SIDE_IMAGE,TAIL_IMAGE_1  FROM  ANIMAL_SYNC_TABLE    ORDER BY  NEW_ANIMAL_ID   DESC

这是创建表代码

db.execSQL("create table " + TABLE_NAME_ANIMAL_SYNC + "(NEW_ANIMAL_ID INTEGER PRIMARY KEY AUTOINCREMENT,FARMER_ID INT,TAG_NO TEXT,ANIMAL_EAR_POSITION TEXT,ANIMAL_SPECIES TEXT,ANIMAL_BREED TEXT,ANIMAL_BODY_COLOR TEXT,ANIMAL_SHAPE_RIGHT TEXT,ANIMAL_SHAPE_LEFT TEXT,ANIMAL_SWITCH_OF_TAIL TEXT,AGE_YEARS NUMBER,ANIMAL_OTHER_MARKS NUMBER,PRAG_STATUS NUMBER,NUMBER_OF_LACTATION NUMBER,CURRENT_MILK NUMBER,SUM_INSURED NUMBER,TAG_IMAGE BLOB NOT NULL,HEAD_IMAGE BLOB NOT NULL,LEFT_SIDE_IMAGE BLOB NOT NULL,RIGHT_SIDE_IMAGE BLOB NOT NULL,TAIL_IMAGE_1 BLOB NOT NULL)");

这是数据库代码

 public List<DataModel> getAllData() {
        List<DataModel> data = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT NEW_ANIMAL_ID,TAG_NO,FARMER_ID,ANIMAL_EAR_POSITION,ANIMAL_SPECIES,ANIMAL_BREED,ANIMAL_BODY_COLOR,ANIMAL_SHAPE_RIGHT,ANIMAL_SHAPE_LEFT,ANIMAL_SWITCH_OF_TAIL,AGE_YEARS,ANIMAL_OTHER_MARKS,PRAG_STATUS,NUMBER_OF_LACTATION,CURRENT_MILK,SUM_INSURED,TAG_IMAGE,HEAD_IMAGE,LEFT_SIDE_IMAGE,RIGHT_SIDE_IMAGE,TAIL_IMAGE_1  FROM  ANIMAL_SYNC_TABLE    ORDER BY  NEW_ANIMAL_ID   DESC", null);
        StringBuffer stringBuffer = new StringBuffer();
        DataModel dataModel = null;

        if (cursor.moveToFirst()) {
            do {
                dataModel = new DataModel();


                int NEW_ANIMAL_ID = cursor.getInt(cursor.getColumnIndexOrThrow("NEW_ANIMAL_ID"));
                String TAG_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("TAG_NO"));
                String FARMER_ID_1 = cursor.getString(cursor.getColumnIndexOrThrow("FARMER_ID"));
                String ANIMAL_EAR_POSITION_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_EAR_POSITION"));
                String ANIMAL_SPECIES_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_SPECIES"));
                String ANIMAL_BREED_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_BREED"));
                String ANIMAL_BODY_COLOR_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_BODY_COLOR"));
                String ANIMAL_SHAPE_RIGHT_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_SHAPE_RIGHT"));
                String ANIMAL_SHAPE_LEFT_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_SHAPE_LEFT"));
                String ANIMAL_SWITCH_OF_TAIL_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_SWITCH_OF_TAIL"));
                String AGE_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("AGE_YEARS"));
                String ANIMAL_OTHER_MARKS_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("ANIMAL_OTHER_MARKS"));
                String PRAG_STATUS_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("PRAG_STATUS"));
                String NUMBER_OF_LACTATION_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("NUMBER_OF_LACTATION"));
                String CURRENT_MILK_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("CURRENT_MILK"));
                String SUM_INSURED_COLUMN_1 = cursor.getString(cursor.getColumnIndexOrThrow("SUM_INSURED"));


                dataModel.setAnimal_id(NEW_ANIMAL_ID);
                dataModel.setFarmer_id(FARMER_ID_1);
                dataModel.setTag_no(TAG_COLUMN_1);
                dataModel.setEar_position(ANIMAL_EAR_POSITION_COLUMN_1);
                dataModel.setAnimal_species(ANIMAL_SPECIES_COLUMN_1);
                dataModel.setAnimal_breed(ANIMAL_BREED_COLUMN_1);
                dataModel.setBody_color(ANIMAL_BODY_COLOR_COLUMN_1);
                dataModel.setShape_right(ANIMAL_SHAPE_RIGHT_COLUMN_1);
                dataModel.setShape_left(ANIMAL_SHAPE_LEFT_COLUMN_1);
                dataModel.setTail_switch(ANIMAL_SWITCH_OF_TAIL_COLUMN_1);
                dataModel.setAge(AGE_COLUMN_1);
                dataModel.setOther_marks(ANIMAL_OTHER_MARKS_COLUMN_1);
                dataModel.setPrag_status(PRAG_STATUS_COLUMN_1);
                dataModel.setLactations(NUMBER_OF_LACTATION_COLUMN_1);
                dataModel.setMilk_qty(CURRENT_MILK_COLUMN_1);
                dataModel.setSum_insured(SUM_INSURED_COLUMN_1);
                dataModel.setTag_photo(Utils.getBitmapFromByte(cursor.getBlob(cursor.getColumnIndex("TAG_IMAGE"))));
                dataModel.setHead_photo(Utils.getBitmapFromByte(cursor.getBlob(cursor.getColumnIndex("HEAD_IMAGE"))));
                dataModel.setLeft_photo(Utils.getBitmapFromByte(cursor.getBlob(cursor.getColumnIndex("LEFT_SIDE_IMAGE"))));
                dataModel.setRight_photo(Utils.getBitmapFromByte(cursor.getBlob(cursor.getColumnIndex("RIGHT_SIDE_IMAGE"))));
                dataModel.setSwitch_Tail_Photo(Utils.getBitmapFromByte(cursor.getBlob(cursor.getColumnIndex("TAIL_IMAGE_1"))));


                stringBuffer.append(dataModel);
                data.add(dataModel);

            } while (cursor.moveToNext());

        }

        return data;
    }

预先感谢您!

3 个答案:

答案 0 :(得分:1)

您将从以下位置更改代码

 if (cursor.moveToFirst()) {
            do {
                dataModel = new DataModel();
               ....
        } while (cursor.moveToNext());
  }

对此:

while(cursor.moveToNext())
{
  dataModel = new DataModel();
  ...

  data.add(dataModel);
}

答案 1 :(得分:1)

将查询更改为此

SELECT * FROM ANIMAL_SYNC_TABLE ORDER BY NEW_ANIMAL_ID DESC

比从此链接下载的应用程序检查问题是否仍然存在,并打开数据库  在您电脑中的这个应用程序中  他们会在执行sql选项卡中触发您的查询,并检查查询是否正确

[https://sqlitebrowser.org/]

答案 2 :(得分:0)

SQLITE数据库对大型二进制对象(BLOBS)的支持有限。每行数据限制为1 MB,并且数据库使用NUL字节来分隔存储中的列。 ...包含对单独的Blob表的引用。