2015-10-17 113 views
0

我在Android OS中有SQLite數據庫的另一個問題。 我試圖創建兩個表的數據庫:部門和學生。 Departments表包含:dep_id,dep_name和dep_major。 學生表包含:stud_id,dep_id,名字,姓氏,電子郵件,電話。Android,多表,SQLite數據庫

如何將數據放入這兩個表中?

有一個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); 
    } 
} 
+0

我不知道我理解你的問題,但'INSERT'是將數據插入表的SQL命令。 –

回答

0

下面的方法將插入在SQLite數據庫中的記錄使用Datbase輔助類:

public void insertDataInBothTables() { 

    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues valuesForDepartmentTable = new ContentValues(); 
    valuesForDepartmentTable.put(KEY_DEPARTMENT , "name"); 
    valuesForDepartmentTable.put(KEY_SPECIALIZATION , "special"); 

    // insert row 
    long idOfDepart = db.insert(TABLE_DEPARTMENTS, null, valuesForDepartmentTable); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_FIRSTNAME, "first-name"); 
    values.put(KEY_SURNAME, "sur-name"); 
    values.put(KEY_INDEKS , "inserting-data"); 
    values.put(KEY_EMAIL , "inserting-data"); 
    values.put(KEY_NUMER , "inserting-data"); 
    //Parameter to add department id 
    values.put(KEY_WYDZIAL, idOfDepart + ""); 

    // insert row 
    db.insert(TABLE_STUDENTS, null, values); 

} 
+0

這兩張表會相互依存嗎? 當我將學生和其數據一起添加時,教師和學習領域是應用程序應該在數據庫中正確讀寫數據? –

+0

「相互依賴」是什麼意思?是的,這些方法在數據庫中正確寫入數據......! –

+0

我的意思是:如果我點擊saveButton來保存數據庫中的數據,我需要調用兩個方法,正確嗎?並且databasehandler.class可以嗎? –