创建多个表时应用程序崩溃

时间:2017-04-18 19:45:58

标签: android sqlite android-sqlite

只在创建第一个表时应用程序正常工作,但在创建多个表时崩溃。

以下是我的DatabaseHelper类的代码

public class DatabaseHelper extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "TrainingSet.db";

//Column Names for selectLocation class table
public static final String TABLE_NAME = "PrimaryLocation";
public static final String ID = "ID";
public static final String LOCATION_NAME = "LOCATION_NAME";
public static final String MAC_1 = "MAC_ADDRESS_1";
public static final String MAC_2 = "MAC_ADDRESS_2";
public static final String MAC_3 = "MAC_ADDRESS_3";

//Column Names for TrainingSet class table
public static final String TABLE_NAME_TRAINING = "TrainingSet";
public static final String TRAINING_ID = "ID";
public static final String X_VALUE = "X";
public static final String Y_VALUE = "Y";
public static final String RSS_M1 = "M1";
public static final String RSS_M2 = "M2";
public static final String RSS_M3 = "M3";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 4);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ LOCATION_NAME + " TEXT, " + MAC_1 +" TEXT, "+ MAC_2 +" TEXT, "+ MAC_3 +" TEXT);");
    db.execSQL("CREATE TABLE " + TABLE_NAME_TRAINING + " (" + TRAINING_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ X_VALUE + " INTEGER, " + Y_VALUE +" INTEGER, "+ RSS_M1 +" INTEGER, "+ RSS_M2 +" INTEGER, "+ RSS_M3 +" INTEGER, "+ ID +" INTEGER);");
    //db.execSQL("create table " + TABLE_NAME_TRAINING + " (" + TRAINING_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ X_VALUE + " INTEGER, " + Y_VALUE +" INTEGER, "+ RSS_M1 +" INTEGER, "+ RSS_M2 +" INTEGER, "+ RSS_M3 +" INTEGER, "+ ID +" INTEGER, " + " FOREIGN KEY ("+ID+") REFERENCES "+TABLE_NAME+"("+ID+"));");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_TRAINING);
    onCreate(db);
}

//METHODS FOR TrainingSet class TABLES
/*public boolean insertDataTraining(int x, int y, int rss_m1, int rss_m2, int rss_m3, int i){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(X_VALUE, x);
    contentValues.put(Y_VALUE, y);
    contentValues.put(RSS_M1, rss_m1);
    contentValues.put(RSS_M2, rss_m2);
    contentValues.put(RSS_M3, rss_m3);
    contentValues.put(ID, i);
    long result = db.insert(TABLE_NAME_TRAINING, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public Cursor getAllDataTraining(int i){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT " + X_VALUE + " , " + Y_VALUE + " FROM " + TABLE_NAME_TRAINING + " WHERE " + ID + " = " + i , null);
    return res;
}*/

//METHODS FOR selectLocation class TABLES

public int deleteData(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    //delete returns the number of rows affected
    int res = db.delete(TABLE_NAME, "ID=?", new String[]{String.valueOf(id)});
    return res;
}

public boolean insertData(String locationname, String mac1,String mac2, String mac3){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(LOCATION_NAME, locationname);
    contentValues.put(MAC_1, mac1);
    contentValues.put(MAC_2, mac2);
    contentValues.put(MAC_3, mac3);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

}

我已经增加了数据库版本号,并且还卸载并重新安装了应用程序,这给了我同样的错误。

此外,我正在尝试创建从“PrimaryLocation”表到“TrainingSet”表的一对多关系。为此,我在创建第二个表(我已经注释掉)时添加了一个外键关系。语法是否正确?

任何帮助将不胜感激。提前谢谢。

编辑:在模拟器上运行应用程序时,我在logcat窗口中获得了以下内容

Process: com.example.indoorpositioning.indoorpositioning, PID: 12863
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.indoorpositioning.indoorpositioning/com.example.indoorpositioning.indoorpositioning.selectLocation}: android.database.sqlite.SQLiteException: duplicate column name: ID (code 1): , while compiling: CREATE TABLE TrainingSet (ID INTEGER PRIMARY KEY AUTOINCREMENT, X INTEGER, Y INTEGER, M1 INTEGER, M2 INTEGER, M3 INTEGER, ID INTEGER);
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: android.database.sqlite.SQLiteException: duplicate column name: ID (code 1): , while compiling: CREATE TABLE TrainingSet (ID INTEGER PRIMARY KEY AUTOINCREMENT, X INTEGER, Y INTEGER, M1 INTEGER, M2 INTEGER, M3 INTEGER, ID INTEGER);
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
  at com.example.indoorpositioning.indoorpositioning.DatabaseHelper.onCreate(DatabaseHelper.java:43)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
  at com.example.indoorpositioning.indoorpositioning.DatabaseHelper.getAllData(DatabaseHelper.java:101)
  at com.example.indoorpositioning.indoorpositioning.selectLocation.displaydata(selectLocation.java:43)
  at com.example.indoorpositioning.indoorpositioning.selectLocation.onCreate(selectLocation.java:157)
  at android.app.Activity.performCreate(Activity.java:6251)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5417) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

您正在重复TABLE_NAME_TRAINING

中的密钥

TRAINING_ID + " INTEGER PRIMARY KEY AUTOINCREMENT

并且

ID +" INTEGER);"

public static final String ID = "ID";

//Column Names for TrainingSet class table
public static final String TABLE_NAME_TRAINING = "TrainingSet";
public static final String TRAINING_ID = "ID";