samedi 17 octobre 2015

Android, Multiple Tables, SQLite Database

I have antoher problem with SQLite database in Android OS. I'm trying to create database with two tables: departments and students. Departments table contains: dep_id, dep_name and dep_major. Students table contains: stud_id, dep_id, firstname, surname, email, phone.

How can I put data into this two tables?

There is a Databasehandler.java:

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String TAG = "DatabaseHandler";
private static final String DATABASE_NAME = "studentsManager2.db";

// pola tabeli "wydzial"
private static final String TABLE_DEPARTMENTS = "departments",
        KEY_DEP_ID = "id",
        KEY_DEPARTMENT = "wydzial",
        KEY_SPECIALIZATION = "kierunek";
// pola tabeli "students"
private static final String TABLE_STUDENTS = "students",
        KEY_STUD_ID = "id",
        KEY_ID = KEY_DEP_ID,
        KEY_FIRSTNAME = "imie",
        KEY_SURNAME = "nazwisko",
        KEY_INDEKS = "indeks",
        KEY_EMAIL = "email",
        KEY_NUMER = "numer";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_DEPARTMENTS + "(" + KEY_DEP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_DEPARTMENT + " TEXT NOT NULL," + KEY_SPECIALIZATION + " TEXT NOT NULL)");
    db.execSQL("CREATE TABLE " + TABLE_STUDENTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FIRSTNAME + " TEXT NOT NULL," + KEY_SURNAME + " TEXT NOT NULL," + KEY_INDEKS + " INTEGER," + KEY_EMAIL + " TEXT NOT NULL," + KEY_NUMER + " TEXT NOT NULL)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data.");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEPARTMENTS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
    onCreate(db);
}

}

Aucun commentaire:

Enregistrer un commentaire