SQLite Android插入返回-1

时间:2015-02-06 11:36:31

标签: android database sqlite

我正在Android App上使用SQLite创建数据库。 我已经成功创建了一个名为“alerts”的表,我可以从中插入和读取数据。问题是当我创建第二个名为“中断”的表时。

当我尝试插入时,没有给出异常。但结果是

valuesID = db.insert(TABLE_NAME_INTERRUPTS, null, values); 

是-1。

然后,当我尝试从该特定表中读取时,cursor.moveToFirst()返回false。

我使用的代码与之前用过的代码相同。

在以下代码之后调试我的应用时,

SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

我可以看到表格的列。我相信表是创建的,但插入时不知何故有问题。我不知道它是否与我正在使用的数据类型有关。

我有2个表:“警报”和“中断”。 对于每个表,我都有一个“插入”和“获取”数据的方法。一切都适用于“警报”表。另一个没有。

这是我的SQLiteOpenHelper:

public class DataBaseManagement extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "partum.db";
    public static final String TABLE_NAME_ALERTS = "alerts";
    public static final String TABLE_NAME_INTERRUPTS = "interruptions";
    private static final int DATABASE_VERSION = 1;

    public DataBaseManagement(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       db.execSQL(CREATE_TABLE_NAME_ALERTS);
       db.execSQL(CREATE_TABLE_NAME_INTERRUPTS);
    }



    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.execSQL("PRAGMA foreign_keys=ON");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_NAME_ALERTS);
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_NAME_INTERRUPTS);

        onCreate(db);
    }

    private static final String CREATE_TABLE_NAME_ALERTS = "create table "
            + TABLE_NAME_ALERTS + " ( "
            + "id_alert" + " integer primary key, "
            + "msg" + " TEXT, "
            + "id_animal" + " TEXT, "
            + "status" + " TEXT, "
            + "id_box" + " TEXT, "
            + "date_inserted" + " TEXT, "
            + "type_alert" + " TEXT);";


    private static final String CREATE_TABLE_NAME_INTERRUPTS = "create table "
            + TABLE_NAME_INTERRUPTS + " ( "
            + "id_interrupt" + " integer primary key, "
            + "status" + " TEXT, "
            + "duration" + " TEXT, "
            + "frequency" + " TEXT, "
            + "id_box" + " TEXT, "
            + "id_mother" + " TEXT, "
            + "id_lot" + " TEXT, "
            + "date_birth" + " TEXT, "
            + "hour_birth" + " TEXT, "
            + "id_birth" + " TEXT, "
            + "id_row" + " TEXT, "
            + "id_wean" + " TEXT, "
            + "mummified" + " TEXT, "
            + "number_children_alive" + " TEXT, "
            + "number_children_dead" + " TEXT, "
            + "id_interruption_event" + " TEXT, "
            + "date_time_event" + " TEXT);";



    public int insertAlerts(String[] dataAlerts){

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor;
        long valuesID;


        ContentValues values = new ContentValues(); // for readings
        values.put("id_alert", dataAlerts[4]);
        values.put("msg", dataAlerts[0]);
        values.put("id_animal", dataAlerts[1]);
        values.put("status", dataAlerts[2]);
        values.put("id_box", dataAlerts[3]);
        values.put("date_inserted", dataAlerts[5]);
        values.put("type_alert", dataAlerts[6]);


        //check if DB has more than 50 entries
        cursor = db.rawQuery("SELECT COUNT(id_alert) AS count FROM alerts", null);
        if(cursor.moveToFirst())
        {
            if(cursor.getInt(0) > 49){
//            Log.d("database", "database limit exceeded");

                cursor = db.query(TABLE_NAME_ALERTS, null, null, null, null, null, null);

                //delete first row
                if(cursor.moveToFirst()) {
                    String rowId = cursor.getString(cursor.getColumnIndex("id_alert"));
                    db.delete(TABLE_NAME_ALERTS, "id_alert" + "=?",  new String[]{rowId});
                }
                cursor.close();
            }
        }
        // Inserting Row in Data Values
        valuesID = db.insert(TABLE_NAME_ALERTS, null, values);
        return (int) valuesID;
    }

    public ArrayList<ArrayList<String>> getDataAlerts(){

        ArrayList<ArrayList<String>> tableRows = new ArrayList<ArrayList<String>>();

        tableRows.clear();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_NAME_ALERTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ArrayList<String> rowValues = new ArrayList<String>();
                rowValues.clear();
                rowValues.add(cursor.getString(0));
                rowValues.add(cursor.getString(1));
                rowValues.add(cursor.getString(2));
                rowValues.add(cursor.getString(3));
                rowValues.add(cursor.getString(4));
                rowValues.add(cursor.getString(5));
                rowValues.add(cursor.getString(6));

                System.out.println("Row Values: " + rowValues);

                // Adding row to list
                tableRows.add(rowValues);

                System.out.println("Table Rows:" + tableRows);

            } while (cursor.moveToNext());
        }

        return tableRows;
    }

    public int insertInterrupts(String[] dataInterrupts){

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor;
        long valuesID;


        ContentValues values = new ContentValues(); // for readings
        values.put("id_interrupt", dataInterrupts[15]);
        values.put("status", dataInterrupts[0]);
        values.put("duration", dataInterrupts[1]);
        values.put("frequency", dataInterrupts[2]);
        values.put("id_box", dataInterrupts[3]);
        values.put("id_mother", dataInterrupts[4]);
        values.put("id_lot", dataInterrupts[5]);
        values.put("date_birth", dataInterrupts[6]);
        values.put("hour_birth", dataInterrupts[7]);
        values.put("id_birth", dataInterrupts[8]);
        values.put("id_row", dataInterrupts[9]);
        values.put("date_wean", dataInterrupts[10]);
        values.put("mummified", dataInterrupts[11]);
        values.put("number_children_alive", dataInterrupts[12]);
        values.put("number_children_dead", dataInterrupts[13]);
        values.put("id_interruption_event", dataInterrupts[14]);
        values.put("date_time_event", dataInterrupts[16]);



        //check if DB has more than 50 entries
        cursor = db.rawQuery("SELECT COUNT(id_interrupt) AS count FROM interruptions", null);
        if(cursor.moveToFirst())
        {
            if(cursor.getInt(0) > 49){
//            Log.d("database", "database limit exceeded");

                cursor = db.query(TABLE_NAME_INTERRUPTS, null, null, null, null, null, null);

                //delete first row
                if(cursor.moveToFirst()) {
                    String rowId = cursor.getString(cursor.getColumnIndex("id_interrupt"));
                    db.delete(TABLE_NAME_INTERRUPTS, "id_interrupt" + "=?",  new String[]{rowId});
                }
                cursor.close();
            }
        }
        // Inserting Row in Data Values
        valuesID = db.insert(TABLE_NAME_INTERRUPTS, null, values);
        return (int) valuesID;
    }

        public ArrayList<ArrayList<String>> getDataInterrupts(){

        ArrayList<ArrayList<String>> tableRows = new ArrayList<ArrayList<String>>();

            tableRows.clear();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_NAME_INTERRUPTS;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ArrayList<String> rowValues = new ArrayList<String>();
                rowValues.clear();
                rowValues.add(cursor.getString(0));
                rowValues.add(cursor.getString(1));
                rowValues.add(cursor.getString(2));
                rowValues.add(cursor.getString(3));
                rowValues.add(cursor.getString(4));
                rowValues.add(cursor.getString(5));
                rowValues.add(cursor.getString(6));
                rowValues.add(cursor.getString(7));
                rowValues.add(cursor.getString(8));
                rowValues.add(cursor.getString(9));
                rowValues.add(cursor.getString(10));
                rowValues.add(cursor.getString(11));
                rowValues.add(cursor.getString(12));
                rowValues.add(cursor.getString(13));
                rowValues.add(cursor.getString(14));
                rowValues.add(cursor.getString(15));
                rowValues.add(cursor.getString(16));

                System.out.println("Row Values: " + rowValues);

                // Adding row to list
                tableRows.add(rowValues);

                System.out.println("Table Rows:" + tableRows);

            } while (cursor.moveToNext());
        }

        return tableRows;
    }

}

如果有人可以帮助我,我将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

看,你的代码中有这样一行:

values.put("date_wean", dataInterrupts[10]);

没有这样名称的列

 这就是为什么使用CONSTANTS而不是硬编码字符串来访问字段等非常重要