Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package comsci.boy.applearning.applearning;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- public class DBLearning extends SQLiteOpenHelper{
- public static final String DB_Name = "AppLearning";
- public static final String TB_Name = "Profile";
- public static final String FLD_ID = "id";
- public static final String FLD_USERNAME = "username";
- public static final String FLD_PASSWORD = "passwd";
- public static final String FLD_FULLNAME = "fullname";
- public static final String FLD_EMAIL = "email";
- public static final String TB_Name2 = "Score";
- public static final String FLD_ID2 = "id";
- public static final String FLD_USERNAME2 = "username";
- public static final String FLD_TESTDATE = "testDate";
- public static final String FLD_SCORE = "score";
- public DBLearning(Context context) {
- super(context, DB_Name, null, 1); //super เรียกใช้ constructor ของ คลาสแม่ (SQLiteOpenHelper)
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- //สร้างตารางข้อมูล
- db.execSQL("create table "+TB_Name+" ("+FLD_ID+
- " integer primary key, "+FLD_USERNAME+" text,"+
- FLD_PASSWORD+" text,"+FLD_FULLNAME+" text,"+FLD_EMAIL+"text)"); //.execSQL ไว้ใช้รัน SQLite ไม่จำเป็นต้องประกาศ size
- db.execSQL("create table "+TB_Name2+" ("+FLD_ID2+
- " integer primary key, "+FLD_USERNAME2+" text,"+
- FLD_TESTDATE+" date,"+FLD_SCORE+" text)");
- // Log.d("CREATE Mutiple Table,"Success");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //เขียนดักว่าถ้ามีการเรียกใช้ db ซ้ำ จะ drop ของเก่าแล้วสร้างใหม่
- db.execSQL("drop table if exists "+TB_Name);
- db.execSQL("drop table if exists "+TB_Name2);
- onCreate(db);
- }
- //Data Manipulation
- //Insert new contact
- public boolean insertRegister(String username,String password,String fullname,String email){
- //กำหนดให้สามารถเขียนข้อมูลลงในตารางข้อมูลได้
- //update delete insert ต้องใช้ getWritableDatabase
- SQLiteDatabase db = this.getWritableDatabase();
- //select ใช้ readable
- //กำหนดค่าที่จะเพิ่มลงในตารางข้อมูล ด้วยคลาส ContentValues
- ContentValues contentValues = new ContentValues();
- contentValues.put(FLD_USERNAME,username);
- contentValues.put(FLD_PASSWORD,password);
- contentValues.put(FLD_FULLNAME,fullname);
- contentValues.put(FLD_EMAIL,email);
- db.insert(TB_Name,null,contentValues); //null คือใส่เงื่อนไขในการ insert
- return true;
- }
- public boolean insertScore(String username,String testdate,int score){
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(FLD_USERNAME2,username);
- contentValues.put(FLD_TESTDATE,testdate);
- contentValues.put(FLD_SCORE,score);
- db.insert(TB_Name,null,contentValues);
- return true;
- }
- //Query Data
- //เวลา Select จะใช้ผ่าน cursor
- public Cursor getProfile(int id){
- //กำหนดให้อ่านข้อมูลจากตารางได้อย่างเดียว
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor rs = db.rawQuery("select * from "+TB_Name+" where "+FLD_ID+"="+id,null); //null คือเงื่อนไข where (ในกรณีไม่ได้ hardcode)
- return rs;
- }
- public Cursor getScore(String username){
- //กำหนดให้อ่านข้อมูลจากตารางได้อย่างเดียว
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor rs = db.rawQuery("select * from "+TB_Name2+" where "+FLD_USERNAME2+"="+username,null); //null คือเงื่อนไข where (ในกรณีไม่ได้ hardcode)
- return rs;
- }
- //Uppdate Data
- public boolean updateProfile(int id,String password,String email){ //ไม่มีชื่อ เพราะเราไม่ต้องการให้เปลี่ยน primary key
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(FLD_PASSWORD,password);
- contentValues.put(FLD_EMAIL,email);
- //Syntax update : table name , ค่าที่ต้องการแก้ , where ฟิลไหน , ค่าที่ต้องการ where
- db.update(TB_Name,contentValues,FLD_ID+"=?",new String[] {Integer.toString(id)});
- return true;
- }
- /*
- public int deleteContact (int id){
- SQLiteDatabase db = this.getWritableDatabase();
- int result = db.delete(TB_Name,FLD_ID+"=?", new String[]{Integer.toString(id)});
- return result;
- }*/
- //Get All Record
- public ArrayList<String> getAllContact(){
- //ประกาศ ArrayList สำหรับเก็บข้อมูลทุก record ที่จะคืนกลับ
- ArrayList<String> arrayList = new ArrayList<String>();
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor rs = db.rawQuery("select * from "+TB_Name,null);
- rs.moveToFirst(); //cursor ไป record แรก
- Cursor rs2 = db.rawQuery("select * from "+TB_Name2,null);
- rs2.moveToFirst(); //cursor ไป record แรก
- //อ่านข้อมูลจนกว่าจะครบทุก record
- while(rs.isAfterLast() == false){ //.isAfterLast() cursor เลย end of file แล้วหรือยัง
- //เพิ่มข้อมูลแต่ละ record ลงใน arraylist
- arrayList.add(rs.getString(rs.getColumnIndex(FLD_FULLNAME)));
- rs.moveToNext();
- }
- while(rs2.isAfterLast() == false){ //.isAfterLast() cursor เลย end of file แล้วหรือยัง
- //เพิ่มข้อมูลแต่ละ record ลงใน arraylist
- arrayList.add(rs.getString(rs.getColumnIndex(FLD_FULLNAME)));
- rs.moveToNext();
- }
- return arrayList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement