SQLite Android插入返回-1

SQLite Android插入返回-1,android,database,sqlite,Android,Database,Sqlite,我正在Android应用程序上使用SQLite创建一个数据库。 我已经成功地创建了一个名为“警报”的表,并且能够从中插入和读取数据。问题是当我创建第二个名为“interruptions”的表时 当我尝试插入时,没有给出任何例外。但结果是 valuesID = db.insert(TABLE_NAME_INTERRUPTS, null, values); 是-1 然后,当我尝试从该特定表读取游标时,moveToFirst()返回false 我使用的代码与以前创建上一个表时使用的代码相同 当我在

我正在Android应用程序上使用SQLite创建一个数据库。 我已经成功地创建了一个名为“警报”的表,并且能够从中插入和读取数据。问题是当我创建第二个名为“interruptions”的表时

当我尝试插入时,没有给出任何例外。但结果是

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

然后,当我尝试从该特定表读取游标时,moveToFirst()返回false

我使用的代码与以前创建上一个表时使用的代码相同

当我在以下代码之后调试我的应用程序时

SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
我可以看到表中的列。我相信这个表已经创建好了,但是在插入时出现了一个问题。我不知道它是否与我使用的数据类型有关

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

这是我的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;
    }

}
公共类数据库管理扩展了SQLiteOpenHelper{
私有静态最终字符串数据库\u NAME=“partum.db”;
公共静态最终字符串表\u NAME\u ALERTS=“ALERTS”;
公共静态最终字符串表\u NAME\u INTERRUPTS=“interruptions”;
私有静态最终int数据库_VERSION=1;
公共数据库管理(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
db.execSQL(创建表名警报);
execSQL(创建表名中断);
}
@凌驾
public void onOpen(SQLiteDatabase db){
super.onOpen(db);
db.execSQL(“PRAGMA foreign_keys=ON”);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“如果存在删除表”+创建表名\u警报);
db.execSQL(“如果存在删除表”+创建表名\u中断);
onCreate(db);
}
私有静态最终字符串CREATE\u TABLE\u NAME\u ALERTS=“CREATE TABLE”
+表_NAME_ALERTS+“(”
+id_警报“+”整数主键
+“消息”+“文本,”
+“id_animal”+“文本”
+“状态”+“文本,”
+“id_框”+“文本,”
+插入日期“+”文本
+“类型_警报”+“文本);”;
私有静态最终字符串CREATE\u TABLE\u NAME\u INTERRUPTS=“CREATE TABLE”
+表_NAME_中断+”(“
+id_中断“+”整数主键
+“状态”+“文本,”
+“持续时间”+“文本,”
+“频率”+“文本,”
+“id_框”+“文本,”
+“id_母亲”+“文本,”
+“id_lot”+“文本”
+“出生日期”+“文本”
+“出生时间”+“文本”
+“id_出生”+“文本”
+“id_行”+“文本,”
+“id_脱机”+“文本”
+“木乃伊化”+“文本”
+“数字\u子项\u活动”+“文本”
+“数字\u子项\u死亡”+“文本”
+id\u中断\u事件“+”文本
+“日期\时间\事件”+“文本);”;
public int insertAlerts(字符串[]数据警报){
SQLiteDatabase db=this.getWritableDatabase();
光标;
长值sid;
ContentValues=新ContentValues();//用于读取
value.put(“id_警报”,dataAlerts[4]);
value.put(“msg”,dataAlerts[0]);
value.put(“id_动物”,dataAlerts[1]);
值。put(“状态”,数据警报[2]);
value.put(“id_框”,dataAlerts[3]);
值。put(“插入日期”,dataAlerts[5]);
值。put(“类型_警报”,数据警报[6]);
//检查数据库是否有超过50个条目
cursor=db.rawQuery(“选择COUNT(id\u警报)作为警报计数”,null);
if(cursor.moveToFirst())
{
if(cursor.getInt(0)>49){
//Log.d(“数据库”,“超出数据库限制”);
cursor=db.query(表名警报,null,null,null,null,null,null);
//删除第一行
if(cursor.moveToFirst()){
String rowId=cursor.getString(cursor.getColumnIndex(“id_警报”));
db.delete(表名称警报,“id警报”+“=?”,新字符串[]{rowId});
}
cursor.close();
}
}
//在数据值中插入行
valuesID=db.insert(表名称警报,空,值);
返回(int)valuesID;
}
公共阵列列表getDataAlerts(){
ArrayList tableRows=新的ArrayList();
tableRows.clear();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格名称\u警报;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
ArrayList rowValues=新的ArrayList();
rowValues.clear();
添加(cursor.getString(0));
添加(cursor.getString(1));
添加(cursor.getString(2));
添加(cursor.getString(3));
添加(cursor.getString(4));
添加(cursor.getString(5));
添加(cursor.getString(6));
System.out.println(“行值:”+rowValues);
//向列表中添加行
tableRows.add(rowValues);
System.out.println(“表行:“+tableRows”);
}while(cursor.moveToNext());
}
返回表行;
}
公共int插入中断(字符串[]数据中断){
SQLiteDatabase db=this.getWritableDatabase();
光标;
长值sid;
ContentValues=新ContentValues();//用于读取
价值观
values.put("date_wean", dataInterrupts[10]);