Hombre_muerto

dupa

Oct 13th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. import android.content.ContentValues;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.util.Log;
  7.  
  8. //import static android.icu.text.MessagePattern.ArgType.SELECT;
  9.  
  10. public class DBAdapter {
  11.  
  12. private static final String TAG = "DBAdapter"; //used for logging database version changes
  13.  
  14. // Field Names:
  15. public static final String KEY_ROWID = "_id";
  16. public static final String KEY_TASK = "task";
  17. public static final String KEY_DATE = "date";
  18. *** public static final String KEY_ISDONE = "is_done"; *** //<----------------
  19.  
  20.  
  21. public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_TASK, KEY_DATE *** , KEY_ISDONE ***}; //<----------------
  22.  
  23. // Column Numbers for each Field Name:
  24. public static final int COL_ROWID = 0;
  25. public static final int COL_TASK = 1;
  26. public static final int COL_DATE = 2;
  27. *** public static final int COL_ISDONE = 3; *** //<----------------
  28.  
  29. // DataBase info:
  30. public static final String DATABASE_NAME = "dbToDo";
  31. public static final String DATABASE_TABLE = "mainToDo";
  32. public static final int DATABASE_VERSION = 6; // The version number must be incremented each time a change to DB structure occurs.
  33.  
  34. //SQL statement to create database
  35. private static final String DATABASE_CREATE_SQL =
  36. "CREATE TABLE " + DATABASE_TABLE
  37. + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  38. + KEY_TASK + " TEXT NOT NULL, "
  39. + KEY_DATE + " TEXT, "
  40. *** + KEY_ISDONE + " TEXT" *** //<----------------
  41. + ");";
  42.  
  43. private final Context context;
  44. private DatabaseHelper myDBHelper;
  45. private SQLiteDatabase db;
  46.  
  47.  
  48. public DBAdapter(Context ctx) {
  49. this.context = ctx;
  50. myDBHelper = new DatabaseHelper(context);
  51. }
  52.  
  53. // Open the database connection.
  54. public DBAdapter open() {
  55. db = myDBHelper.getWritableDatabase();
  56. return this;
  57. }
  58.  
  59. // Close the database connection.
  60. public void close() {
  61. myDBHelper.close();
  62. }
  63.  
  64. // Add a new set of values to be inserted into the database.
  65. public long insertRow(String task, String date *** , String is_done ***) { //<----------------
  66. ContentValues initialValues = new ContentValues();
  67. initialValues.put(KEY_TASK, task);
  68. initialValues.put(KEY_DATE, date);
  69. *** initialValues.put(KEY_ISDONE, isdone); *** //<----------------
  70.  
  71. // Insert the data into the database.
  72. return db.insert(DATABASE_TABLE, null, initialValues);
  73. }
  74.  
  75. // Delete a row from the database, by rowId (primary key)
  76. public boolean deleteRow(long rowId) {
  77. String where = KEY_ROWID + "=" + rowId;
  78. return db.delete(DATABASE_TABLE, where, null) != 0;
  79. }
  80.  
  81. public void deleteAll() {
  82. Cursor c = getAllRows();
  83. long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
  84. if (c.moveToFirst()) {
  85. do {
  86. deleteRow(c.getLong((int) rowId));
  87. } while (c.moveToNext());
  88. }
  89. c.close();
  90. }
  91.  
  92. // Return all data in the database.
  93. public Cursor getAllRows() {
  94. String where = null;
  95. String ALL_KEYS_ARRAY[] = new String[]{ KEY_ROWID,KEY_DATE,KEY_TASK };
  96. Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS_ARRAY, where, null, null, null, null, null, null);
  97. if (c != null) {
  98. c.moveToFirst();
  99. }
  100. return c;
  101. }
  102.  
  103. // Get a specific row (by rowId)
  104. public Cursor getRow(long rowId) {
  105. String where = KEY_ROWID + "=" + rowId;
  106. String ALL_KEYS_ARRAY[] = new String[]{ KEY_ROWID,KEY_DATE,KEY_TASK };
  107.  
  108. Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS_ARRAY,
  109. where, null, null, null, null, null);
  110. if (c != null) {
  111. c.moveToFirst();
  112. }
  113. return c;
  114. }
  115.  
  116. public Cursor getTermValues(String term)
  117. {
  118. String where = KEY_DATE + "=?";
  119. String whereArgs[] = new String[]{ term };
  120.  
  121. String ALL_KEYS_ARRAY[] = new String[]{ KEY_ROWID,KEY_DATE,KEY_TASK };
  122. Cursor cursor = db.query(DATABASE_TABLE, ALL_KEYS_ARRAY , where, whereArgs, null, null, null, null);
  123. return cursor;
  124. }
  125.  
  126. // Change an existing row to be equal to new data.
  127. public boolean updateRow(long rowId, String task, String date ***, String is_done *** ) {
  128. String where = KEY_ROWID + "=" + rowId;
  129. ContentValues newValues = new ContentValues();
  130. newValues.put(KEY_TASK, task);
  131. newValues.put(KEY_DATE, date);
  132. newValues.put(KEY_ISDONE, isdone);
  133.  
  134.  
  135. // Insert it into the database.
  136. return db.update(DATABASE_TABLE, newValues, where, null) != 0; <------- tu też wypieprza błąd
  137. }
  138.  
  139. private static class DatabaseHelper extends SQLiteOpenHelper
  140. {
  141. DatabaseHelper(Context context) {
  142. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  143. }
  144.  
  145. @Override
  146. public void onCreate(SQLiteDatabase _db) {
  147. _db.execSQL(DATABASE_CREATE_SQL);
  148. }
  149.  
  150. @Override
  151. public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
  152. Log.w(TAG, "Upgrading application's database from version " + oldVersion
  153. + " to " + newVersion + ", which will destroy all old data!");
  154.  
  155. // Destroy old database:
  156. _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
  157.  
  158. // Recreate new database:
  159. onCreate(_db);
  160. }
  161. }
  162. }
Add Comment
Please, Sign In to add comment