SQLite插入方法在我的应用中不起作用

时间:2019-09-27 10:50:36

标签: android sqlite android-sqlite sql-insert

我在项目中使用SQLiteOpenHelper,当我使用add_leader方法时,一切正常,但是当我使用add_Person方法时,它返回false。 这是sqlite的第二个实例。

我使用manjaro 18 linux

请帮忙吗?

public class SqliteHelper extends SQLiteOpenHelper { 


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE " + START+ "("+KIND+" text) ");
    sqLiteDatabase.execSQL("CREATE TABLE " + PERRSON + "("+ID_PER+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
            +FULLNAME_PER+" text,"+BIRTH_DAY_PER+"text," +BIRTH_MONTH_PER+" text,"+BIRTH_YEAR_PER+" text,"
            +LEVEL_EDUCATION+" text,"+MOY_ONE+" text,"+MOY_TWO+" text,"+MOY_THREE+"text) ");
    sqLiteDatabase.execSQL("CREATE TABLE " + LEADER + "("+ID_LEADER+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+FULLNAME_LDR+" text,"+
            BIRTH_DAY_LDR+" text,"+BIRTH_MONTH_LDR+" text,"+BIRTH_YEAR_LDR+" text) ");

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + START);
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + PERRSON);
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + LEADER);

    onCreate(sqLiteDatabase);
}




public boolean add_Person(String name, String date_day, String date_month, String date_year,
                          String level,String moy1,String moy2,String moy3) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(FULLNAME_PER, name);
    cv.put(BIRTH_DAY_PER, date_day);
    cv.put(BIRTH_MONTH_PER, date_month);
    cv.put(BIRTH_YEAR_PER, date_year);
    cv.put(LEVEL_EDUCATION, level);
    cv.put(MOY_ONE, moy1);
    cv.put(MOY_TWO, moy2);
    cv.put(MOY_THREE, moy3);
    long results = db.insert(PERRSON, null, cv);
    db.close();
    if (results == -1) return false;
    else return true;
}


public boolean add_leader(String fullname, String date_day, String date_month, String date_year) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(FULLNAME_LDR, fullname);
    cv.put(BIRTH_DAY_LDR, date_day);
    cv.put(BIRTH_MONTH_LDR, date_month);
    cv.put(BIRTH_YEAR_LDR, date_year);
    long results = db.insert(LEADER, null, cv);
    db.close();
    if (results == -1) return false;
    else return true;

}

2 个答案:

答案 0 :(得分:1)

由于创建表查询中存在间距问题,因此未创建您的PERRSON表。 在这些列BIRTH_DAY_PER,MOY_THREE中发现间距问题。

替换本节代码

 sqLiteDatabase.execSQL("CREATE TABLE " + PERRSON + "("+ID_PER+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
            +FULLNAME_PER+" text,"+BIRTH_DAY_PER+"text," +BIRTH_MONTH_PER+" text,"+BIRTH_YEAR_PER+" text,"
            +LEVEL_EDUCATION+" text,"+MOY_ONE+" text,"+MOY_TWO+" text,"+MOY_THREE+"text) ");

与此

 sqLiteDatabase.execSQL("CREATE TABLE " + PERRSON + "("+ID_PER+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
            +FULLNAME_PER+" text,"+BIRTH_DAY_PER+" text," +BIRTH_MONTH_PER+" text,"+BIRTH_YEAR_PER+" text,"
            +LEVEL_EDUCATION+" text,"+MOY_ONE+" text,"+MOY_TWO+" text,"+MOY_THREE+" text)");

答案 1 :(得分:0)

   sqLiteDatabase.execSQL("CREATE TABLE " + PERRSON + "(" + ID_PER + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
            + FULLNAME_PER + " text," + BIRTH_DAY_PER + " text," + BIRTH_MONTH_PER + " text," + BIRTH_YEAR_PER + " text,"
            + LEVEL_EDUCATION + " text," + MOY_ONE + " text," + MOY_TWO + " text," + MOY_THREE + " text" + ")");