PDA

View Full Version : ایجاد کلاس مدل دیتابیس رابطه ای



samiasoft
پنج شنبه 21 تیر 1397, 07:08 صبح
سلام دوستان

فرض کنید ما یک دیتابیس اسکیولایت اماده بصورت زیر ساختیم که 2 جدول دارد و میخواهیم این دیتابیس را داخل پوشه asset قرار بدیم و استفاده کنیم :

http://s9.picofile.com/file/8331558000/2018_07_12_063310.jpg

حال چگونه کلاس دیتابیس را برای جدول tble_file درست کنم؟ ایا اینطوری نوشتن درسته :

public class Table_file {

int id;
int id_students;
String file_name;
String file_comment;


public Table_file(int id, int id_students, String file_name, String file_comment) {
this.id = id;
this.id_students = id_students;
this.file_name = file_name;
this.file_comment = file_comment;
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public int getId_students() {
return id_students;
}


public void setId_students(int id_students) {
this.id_students = id_students;
}


public String getFile_name() {
return file_name;
}


public void setFile_name(String file_name) {
this.file_name = file_name;
}


public String getFile_comment() {
return file_comment;
}


public void setFile_comment(String file_comment) {
this.file_comment = file_comment;
}
}

اما حالا کلید خارجی که وجود داره رو چطوری استفاده کنیم؟!!؟
ناگفته نماند برای ارتباط با دیتابیس اسکیولایت ازکتابخانه sqliteassethelper استفاده میکنم.

ممنون میشوم در مورد استفاده از این کتابخانه برای جداول رابطه ای راهنمایی کنید.

samiasoft
پنج شنبه 21 تیر 1397, 08:40 صبح
درواقع دوستان من در قسمت کوئری سلکت زدن مشکل دارم



select id,id_students,file_name,file_comment from Table_file
که حالا برای دسترسی به ستون های student_name و family_name باید بیام کوئری تودرتو بنویسیم یعنی؟!!!

hesam67_b
پنج شنبه 21 تیر 1397, 10:45 صبح
با سلام.
شما باید از جدول بخونی و کلاس رو تشکیل بدی تا بتونی دوباره از کلاس بخونی!
دستور select رو اجرا کن و با while یکی یکی اطلاعات رو بخون و در کلاس قرار بده.(باید arraylist از این کلاس تشکیل بدی)

samiasoft
جمعه 22 تیر 1397, 02:09 صبح
تشکر از شما
مشکل من در نوشتن کوئری جوین بود که بدین صورت نوشتم و حل شد



SELECT Tble_file.id,id_students,file_name,file_comment,st udent_name,student_family FROM Tble_file JOIN Tble_students ON Tble_file.id_students=Tble_students.id


کلاس :
public class Table_file {


int id;
int id_students;
String file_name;
String file_comment;
String student_name;
String student_family;


public Table_file(int id, int id_students, String file_name, String file_comment, String student_name, String student_family) {
this.id = id;
this.id_students = id_students;
this.file_name = file_name;
this.file_comment = file_comment;
this.student_name = student_name;
this.student_family = student_family;
}


public int getId() {


return id;
}


public void setId(int id) {
this.id = id;
}


public int getId_students() {
return id_students;
}


public void setId_students(int id_students) {
this.id_students = id_students;
}


public String getFile_name() {
return file_name;
}


public void setFile_name(String file_name) {
this.file_name = file_name;
}


public String getFile_comment() {
return file_comment;
}


public void setFile_comment(String file_comment) {
this.file_comment = file_comment;
}


public String getStudent_name() {
return student_name;
}


public void setStudent_name(String student_name) {
this.student_name = student_name;
}


public String getStudent_family() {
return student_family;
}


public void setStudent_family(String student_family) {
this.student_family = student_family;
}
}