繁体   English   中英

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

[英]Error in creation of database table in Android using SQL

 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();
        }           
    }
}

外键错误:

db.execSQL(“ create table” + categ +“(categoryId INTEGER PRIMARY KEY自动递增,名称文本,isApproved整数,isDeleted整数,createdDate文本,modifiedDate文本)”);

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

更改为

FOREIGN KEY(categoryId) REFERENCES categ(categoryId))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM