Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. package comsci.boy.applearning.applearning;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import java.util.ArrayList;
  8.  
  9. public class DBLearning extends SQLiteOpenHelper{
  10. public static final String DB_Name = "AppLearning";
  11. public static final String TB_Name = "Profile";
  12. public static final String FLD_ID = "id";
  13. public static final String FLD_USERNAME = "username";
  14. public static final String FLD_PASSWORD = "passwd";
  15. public static final String FLD_FULLNAME = "fullname";
  16. public static final String FLD_EMAIL = "email";
  17.  
  18. public static final String TB_Name2 = "Score";
  19. public static final String FLD_ID2 = "id";
  20. public static final String FLD_USERNAME2 = "username";
  21. public static final String FLD_TESTDATE = "testDate";
  22. public static final String FLD_SCORE = "score";
  23.  
  24.  
  25.  
  26. public DBLearning(Context context) {
  27. super(context, DB_Name, null, 1); //super เรียกใช้ constructor ของ คลาสแม่ (SQLiteOpenHelper)
  28. }
  29.  
  30. @Override
  31. public void onCreate(SQLiteDatabase db) {
  32. //สร้างตารางข้อมูล
  33. db.execSQL("create table "+TB_Name+" ("+FLD_ID+
  34. " integer primary key, "+FLD_USERNAME+" text,"+
  35. FLD_PASSWORD+" text,"+FLD_FULLNAME+" text,"+FLD_EMAIL+"text)"); //.execSQL ไว้ใช้รัน SQLite ไม่จำเป็นต้องประกาศ size
  36. db.execSQL("create table "+TB_Name2+" ("+FLD_ID2+
  37. " integer primary key, "+FLD_USERNAME2+" text,"+
  38. FLD_TESTDATE+" date,"+FLD_SCORE+" text)");
  39. // Log.d("CREATE Mutiple Table,"Success");
  40. }
  41.  
  42. @Override
  43. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  44. //เขียนดักว่าถ้ามีการเรียกใช้ db ซ้ำ จะ drop ของเก่าแล้วสร้างใหม่
  45. db.execSQL("drop table if exists "+TB_Name);
  46. db.execSQL("drop table if exists "+TB_Name2);
  47. onCreate(db);
  48. }
  49.  
  50. //Data Manipulation
  51. //Insert new contact
  52. public boolean insertRegister(String username,String password,String fullname,String email){
  53. //กำหนดให้สามารถเขียนข้อมูลลงในตารางข้อมูลได้
  54. //update delete insert ต้องใช้ getWritableDatabase
  55. SQLiteDatabase db = this.getWritableDatabase();
  56. //select ใช้ readable
  57.  
  58. //กำหนดค่าที่จะเพิ่มลงในตารางข้อมูล ด้วยคลาส ContentValues
  59. ContentValues contentValues = new ContentValues();
  60. contentValues.put(FLD_USERNAME,username);
  61. contentValues.put(FLD_PASSWORD,password);
  62. contentValues.put(FLD_FULLNAME,fullname);
  63. contentValues.put(FLD_EMAIL,email);
  64. db.insert(TB_Name,null,contentValues); //null คือใส่เงื่อนไขในการ insert
  65. return true;
  66. }
  67. public boolean insertScore(String username,String testdate,int score){
  68. SQLiteDatabase db = this.getWritableDatabase();
  69. ContentValues contentValues = new ContentValues();
  70. contentValues.put(FLD_USERNAME2,username);
  71. contentValues.put(FLD_TESTDATE,testdate);
  72. contentValues.put(FLD_SCORE,score);
  73. db.insert(TB_Name,null,contentValues);
  74. return true;
  75. }
  76.  
  77. //Query Data
  78. //เวลา Select จะใช้ผ่าน cursor
  79. public Cursor getProfile(int id){
  80. //กำหนดให้อ่านข้อมูลจากตารางได้อย่างเดียว
  81. SQLiteDatabase db = this.getReadableDatabase();
  82. Cursor rs = db.rawQuery("select * from "+TB_Name+" where "+FLD_ID+"="+id,null); //null คือเงื่อนไข where (ในกรณีไม่ได้ hardcode)
  83. return rs;
  84. }
  85. public Cursor getScore(String username){
  86. //กำหนดให้อ่านข้อมูลจากตารางได้อย่างเดียว
  87. SQLiteDatabase db = this.getReadableDatabase();
  88. Cursor rs = db.rawQuery("select * from "+TB_Name2+" where "+FLD_USERNAME2+"="+username,null); //null คือเงื่อนไข where (ในกรณีไม่ได้ hardcode)
  89. return rs;
  90. }
  91.  
  92. //Uppdate Data
  93. public boolean updateProfile(int id,String password,String email){ //ไม่มีชื่อ เพราะเราไม่ต้องการให้เปลี่ยน primary key
  94. SQLiteDatabase db = this.getWritableDatabase();
  95. ContentValues contentValues = new ContentValues();
  96. contentValues.put(FLD_PASSWORD,password);
  97. contentValues.put(FLD_EMAIL,email);
  98. //Syntax update : table name , ค่าที่ต้องการแก้ , where ฟิลไหน , ค่าที่ต้องการ where
  99. db.update(TB_Name,contentValues,FLD_ID+"=?",new String[] {Integer.toString(id)});
  100. return true;
  101. }
  102. /*
  103. public int deleteContact (int id){
  104. SQLiteDatabase db = this.getWritableDatabase();
  105. int result = db.delete(TB_Name,FLD_ID+"=?", new String[]{Integer.toString(id)});
  106. return result;
  107. }*/
  108.  
  109. //Get All Record
  110. public ArrayList<String> getAllContact(){
  111. //ประกาศ ArrayList สำหรับเก็บข้อมูลทุก record ที่จะคืนกลับ
  112. ArrayList<String> arrayList = new ArrayList<String>();
  113. SQLiteDatabase db = this.getReadableDatabase();
  114. Cursor rs = db.rawQuery("select * from "+TB_Name,null);
  115. rs.moveToFirst(); //cursor ไป record แรก
  116. Cursor rs2 = db.rawQuery("select * from "+TB_Name2,null);
  117. rs2.moveToFirst(); //cursor ไป record แรก
  118. //อ่านข้อมูลจนกว่าจะครบทุก record
  119. while(rs.isAfterLast() == false){ //.isAfterLast() cursor เลย end of file แล้วหรือยัง
  120. //เพิ่มข้อมูลแต่ละ record ลงใน arraylist
  121. arrayList.add(rs.getString(rs.getColumnIndex(FLD_FULLNAME)));
  122. rs.moveToNext();
  123. }
  124. while(rs2.isAfterLast() == false){ //.isAfterLast() cursor เลย end of file แล้วหรือยัง
  125. //เพิ่มข้อมูลแต่ละ record ลงใน arraylist
  126. arrayList.add(rs.getString(rs.getColumnIndex(FLD_FULLNAME)));
  127. rs.moveToNext();
  128. }
  129. return arrayList;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement