Open Chamiko opened 6 years ago
package com.samsung.androidday6sqlite;
/**
public class DBInfor { public static final String DB_NAME = "student.db"; public static final String TABLE_USER = "user"; public static final String CL_ID = "id"; public static final String CL_NAME = "name"; public static final String CL_AGE = "age"; public static final String CL_SCHOOL = "school"; public static final int DB_VERSION = 1;
public static final String CREAT_USER_TABLE =
"CREATE TABLE " + TABLE_USER + "(" + CL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ CL_NAME + " TEXT, " + CL_AGE + " INTEGER, " + CL_SCHOOL + " TEXT)";
}
package com.samsung.androidday6sqlite;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity { private StudentManager studentDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentDatabase = new StudentManager(this);
ArrayList<Student> arrStudents = studentDatabase.getAllStudent();
for(int i = 0; i < arrStudents.size(); i++){
Student student = arrStudents.get(i);
Toast.makeText(this, student.toString(), Toast.LENGTH_LONG).show();
}
//them
// long res = studentDatabase.addStudent("QooBee", 18, "RoyalBee"); // Toast.makeText(this, "So dong: " + res,Toast.LENGTH_SHORT).show();
//xoa
// int res = studentDatabase.deleteStudent(2); // if(res > 0){ // Toast.makeText(this, "xoa ok " + res,Toast.LENGTH_SHORT).show(); // }else { // Toast.makeText(this, "ko xoa dc " + res,Toast.LENGTH_SHORT).show(); // }
// int res = studentDatabase.updateStudent(1, "QooBee cute", 15, "RoyalBee"); // if(res > 0){ // Toast.makeText(this, "update ok " + res,Toast.LENGTH_SHORT).show(); // }else { // Toast.makeText(this, "ko update dc " + res,Toast.LENGTH_SHORT).show(); // } }
}
package com.samsung.androidday6sqlite;
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
/**
public class StudentManager { private DBHelper dbHelper; private SQLiteDatabase database; private Context context;
public StudentManager(Context context) {
this.context = context;
dbHelper = new DBHelper(context, DBInfor.DB_NAME, null, DBInfor.DB_VERSION);
database = dbHelper.getWritableDatabase();
}
public long addStudent(String name, int age, String school) {
ContentValues cv = new ContentValues();
cv.put(DBInfor.CL_NAME, name);
cv.put(DBInfor.CL_AGE, age);
cv.put(DBInfor.CL_SCHOOL, school);
long rows = database.insert(DBInfor.TABLE_USER, null, cv); //nullColumHack cot ko co gia tri trung
return rows;
}
public int deleteStudent(int id) {
int rows = database.delete(DBInfor.TABLE_USER, DBInfor.CL_ID + "=?", new String[]{String.valueOf(id)});
return rows;
}
public int updateStudent(int id, String name, int age, String school) {
ContentValues cv = new ContentValues();
cv.put(DBInfor.CL_NAME, name);
cv.put(DBInfor.CL_AGE, age);
cv.put(DBInfor.CL_SCHOOL, school);
int rows = database.update(DBInfor.TABLE_USER, cv, DBInfor.CL_ID + "=?", new String[]{String.valueOf(id)});
return rows;
}
public ArrayList<Student> getAllStudent() {
ArrayList<Student> arrStudent = new ArrayList<>();
Cursor cursor = database.query(DBInfor.TABLE_USER, null, null, null, null, null, null, null);
// cursor.moveToFirst(); while (cursor.moveToNext()) { int idIndex = cursor.getColumnIndex(DBInfor.CL_ID); int nameIndex = cursor.getColumnIndex(DBInfor.CL_NAME); int ageIndex = cursor.getColumnIndex(DBInfor.CL_AGE); int schoolIndex = cursor.getColumnIndex(DBInfor.CL_SCHOOL);
int id = cursor.getInt(idIndex);
String name = cursor.getString(nameIndex);
int age = cursor.getInt(ageIndex);
String school = cursor.getString(schoolIndex);
Student student = new Student(id, name, age, school);
arrStudent.add(student);
}
return arrStudent;
}
}
package com.samsung.androidday6sqlite;
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
/**
public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); }
}