使用SQL在Android中创建数据库表时出错

时间:2015-06-17 13:33:04

标签: android sqlite android-sqlite

 I am trying to make database table using primary key and foreign key. My first table `Categ` contains fields `catgoryId` as primary key and second table `Pers` contains `categoryId` as foreign key.

第一次创建表工作正常,而第二次表示错误无法在Person表中插入数据。

代码是:

  public void onCreate(SQLiteDatabase db) {
       // if any table missing then recreate all tables (can be done             dynamically)
        if(!isTableExist(db, categ)||!isTableExist(db, pers)){

        db.execSQL("DROP TABLE IF EXISTS "+categ);
         db.execSQL("DROP TABLE IF EXISTS "+pers);

        db.execSQL("create table "+categ+"(categoryId INTEGER PRIMARY KEY autoincrement, name text, isApproved integer ,isDeleted integer, createdDate       text, modifiedDate text)");

        db.execSQL("create table "+pers+" (personId INTEGER PRIMARY KEY autoincrement, name text, photo text, biography text,categoryId      integer,isApproved integer, isDeleted integer, createdDate text, modifiedDate     text,FOREIGN KEY(categoryId) REFERENCES categ(categId))");
       }
   }  

}

我通过调用updateAllTables方法创建了Table。 UpdateAllTables以这种方式定义:

public boolean updateAllTables(Context context){
    onCreate(this.getReadableDatabase());
    CDataTransition datatransition = new CDataTransition(context);
    JSONObject jObj = datatransition.getAllTables();
    try {
        JSONArray category = jObj.getJSONArray("categories");
        insertCategories(category);

        JSONArray person = jObj.getJSONArray("persons");
        insertPersons(person);
        return true;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return false;
}

上面使用的方法(insertCategoriers和insertPerson)以这种方式定义:

public void insertCategories(JSONArray jsArr){
     for (int i = 0; i < jsArr.length(); i++) {
        try {
            JSONObject category = jsArr.getJSONObject(i);
            ContentValues cv = new ContentValues();
                  cv.put("categoryId",Integer.parseInt(category.getString("categoryId")));
            cv.put("name",category.getString("name"));
            cv.put("isApproved",Integer.parseInt(category.getString("isApproved")));
            cv.put("isDeleted",Integer.parseInt(category.getString("isDeleted")));
            cv.put("createdDate",category.getString("createdDate"));
            cv.put("modifiedDate",category.getString("modifiedDate"));

            insertData(categ, cv);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }
}

public void insertPersons(JSONArray jsArr){
    for (int i = 0; i < jsArr.length(); i++) {
        try {
            JSONObject person = jsArr.getJSONObject(i);
            ContentValues cv = new ContentValues();
            cv.put("personId",Integer.parseInt(person.getString("personId")));
            cv.put("name",person.getString("name"));
            cv.put("photo",person.getString("photo"));
            cv.put("biography",person.getString("biography"));
            cv.put("categoryId",Integer.parseInt(person.getString("categoryId")));
            cv.put("isApproved",Integer.parseInt(person.getString("isApproved")));
              cv.put("isDeleted",Integer.parseInt(person.getString("isDeleted")));
            cv.put("createdDate",person.getString("createdDate"));
            cv.put("modifiedDate",person.getString("modifiedDate"));

            insertData(pers, cv);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }
}

1 个答案:

答案 0 :(得分:0)

外键错误:

  

db.execSQL(“create table”+ categ +“(categoryId INTEGER PRIMARY KEY autoincrement,name text,isApproved integer,isDeleted integer,createdDate text,modifiedDate text)”);

     

db.execSQL(“create table”+ pers +“(personId INTEGER PRIMARY KEY自动增量,名称文本,照片文本,传记文本,categoryId整数,isApproved整数,isDeleted整数,createdDate文本,modifiedDate文本,FOREIGN KEY(categoryId) REFERENCES categories(categId))“);

将其更改为

FOREIGN KEY(categoryId) REFERENCES categ(categoryId))