如何在android中创建2个SQLite表?

时间:2017-03-16 14:24:23

标签: java android sqlite android-sqlite

我对android很新,一般使用java。我一直在尝试创建一个SQLite数据库并且已经遵循了一些教程,但是我仍然不确定如何在数据库中创建2个表。我能够得到一张桌子而不是第二张桌子。我会附上相关的代码,谢谢!

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "FitnessApp.db";
    public static final String TABLE_NAME = "saved_workout_weights";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "DATE";
    public static final String COL_3 = "EXERCISE";
    public static final String COL_4 = "WEIGHT";
    public static final String COL_5 = "REPS";

    public static final String TABLE2_NAME = "saved_workout_cardio";
    public static final String COL_1a = "ID";
    public static final String COL_2a = "DATE";
    public static final String COL_3a = "TIME";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,DATE TEXT,EXERCISE TEXT,WEIGHT INTEGER,REPS INTEGER)"
                + TABLE2_NAME + "ID INTEGER PRIMARY KEY AUTOINCREMENT,DATE TEXT,TIME INTEGER");
    }

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

2 个答案:

答案 0 :(得分:0)

每个表需要一个CREATE TABLE语句。

CREATE TABLE table1 ...;
CREATE TABLE table2 ...;

答案 1 :(得分:0)

创建查询应该单独运行:两个db.execSQL()调用中的两个SQL语句。