Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. package com.example.work.dbtest;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. /**
  10. * Created by Work on 25/03/2017.
  11. */
  12.  
  13. public class JobsDbHelper extends SQLiteOpenHelper {
  14.  
  15. public static final String DATABASE_NAME = "Jobs1.db";
  16. public static final String TABLE_NAME = "job_table";
  17. public static final String ID = "ID";
  18. public static final String TITLE = "TITLE";
  19. public static final String DESC = "DESCRIPTION";
  20. public static final String TIME_PERIOD = "TIMEPERIOD";
  21. public static final String SALARY = "SALARY";
  22. public static final String SKILLS_REQUIRED = "SKILLSREQUIRED";
  23.  
  24. public JobsDbHelper(Context context) {
  25. super(context, DATABASE_NAME, null, 1);
  26. }
  27.  
  28. @Override
  29. public void onCreate(SQLiteDatabase db) {
  30. String CREATE_JOBS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
  31. + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE + " TEXT,"
  32. + DESC + " TEXT," + TIME_PERIOD + " TEXT," + SALARY + " TEXT," + SKILLS_REQUIRED + " TEXT" + ")";
  33. db.execSQL(CREATE_JOBS_TABLE);
  34. }
  35.  
  36. @Override
  37. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  38. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  39. onCreate(db);
  40. }
  41.  
  42. public boolean insertData(String title, String description, String timePeriod, String salary, String skills){
  43. SQLiteDatabase db = this.getWritableDatabase();
  44. ContentValues contentValues = new ContentValues();
  45. contentValues.put(TITLE, title);
  46. contentValues.put(DESC, description);
  47. contentValues.put(TIME_PERIOD, timePeriod);
  48. contentValues.put(SALARY, salary);
  49. contentValues.put(SKILLS_REQUIRED, skills);
  50. long result = db.insert(TABLE_NAME, null, contentValues);
  51. if(result == -1){
  52. return false;
  53. }
  54. else {
  55. return true;
  56. }
  57. }
  58.  
  59. public Cursor getAllData(){
  60. SQLiteDatabase db = this.getWritableDatabase();
  61. Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
  62. return res;
  63. }
  64. //
  65. // public boolean updateData(String id, String title, String desc, String timePeriod, String salary, String skills){
  66. // SQLiteDatabase db = this.getWritableDatabase();
  67. // ContentValues contentValues = new ContentValues();
  68. // contentValues.put(ID, id);
  69. // contentValues.put(TITLE, title);
  70. // contentValues.put(DESC, desc);
  71. // contentValues.put(TIME_PERIOD, timePeriod);
  72. // contentValues.put(SALARY, salary);
  73. // contentValues.put(SKILLS_REQUIRED, skills);
  74. // db.update(TABLE_NAME, contentValues, "");
  75. // }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement