Guest User

Untitled

a guest
Oct 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. public class Database extends SQLiteOpenHelper {
  2.  
  3. private static final String DATABASE_NAME = "text.db";
  4. private static final String TABLE_NAME = "STUDENTS";
  5. private static final String COLUMN_NO_1 = "FIRSTNAME"; // (NAME, DATA TYPE, CONSTRAINT)
  6. private static final String COLUMN_NO_2 = "LASTNAME"; // (NAME, DATA TYPE)
  7. // private static final String TABLE_CREATE = "CREATE TABLE"+TABLE_NAME+"" +
  8. // "( ID INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_NO_1+","+COLUMN_NO_2+");";
  9. //"CREATE TABLE STUDENT (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT UNIQUE, LASTNAME TEXT)" look like this
  10.  
  11. public Database(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  12. super(context, DATABASE_NAME, factory, version);
  13. }
  14. @Override
  15. public void onCreate(SQLiteDatabase sqLiteDatabase) { //table will create over here
  16. sqLiteDatabase.execSQL("CREATE TABLE" +TABLE_NAME+"( ID INTEGER PRIMARY KEY AUTOINCREMENT," +COLUMN_NO_1 +"TEXT UNIQUE," +COLUMN_NO_2 +" TEXT);");
  17. }
  18.  
  19. @Override
  20. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
  21. {
  22. sqLiteDatabase.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME+";");
  23. onCreate(sqLiteDatabase);
  24.  
  25. }
  26. public void insert_Student (String firstName, String lastName)
  27. {
  28. ContentValues values = new ContentValues(); //insert the value which column u acces
  29. values.put("FIRSTNAME", firstName);
  30. values.put("LASTNAME", lastName);
  31. this.getWritableDatabase().insertOrThrow(TABLE_NAME,null,values); //write this values into the table
  32. }
  33.  
  34. public void delete_student(String firstName) //table were delete only provide first name
  35. {
  36.  
  37. this.getWritableDatabase().delete(TABLE_NAME,"FIRSTNAME='"+firstName+"'", null );
  38. }
  39.  
  40. public void update_student(String old_Firstname, String new_Firstname)
  41. {
  42. this.getWritableDatabase().execSQL("UPDATE STUDENTS SET FIRSTNAME='"+new_Firstname+"' WHERE FIRSTNAME='"+old_Firstname+"'");
  43. //new name k leye dialog ai ga.
  44. }
  45.  
  46. public void list_all_student(TextView textView)
  47. {
  48. Cursor cursor = this.getReadableDatabase().rawQuery("SELECT * FROM "+TABLE_NAME, null); //read records from table use cusur
  49.  
  50. textView.setText("");
  51. while (cursor.moveToNext()) {
  52.  
  53. textView.append(cursor.getString(1) + " "+cursor.getString(2)+"\n"); //0 id , 1 first name , 2 last name
  54. }
  55. }
  56.  
  57.  
  58. }
Add Comment
Please, Sign In to add comment