Advertisement
Guest User

Untitled

a guest
Jun 9th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package info.androidhive.imageslider;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.content.ContentValues;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.database.SQLException;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import android.util.Log;
  12.  
  13. public class DbAdapter {
  14.  
  15. public static final String KEY_ROWID = "_id";
  16. public static final String KEY_PATH = "path";
  17.  
  18. private static final String TAG = "ImagesDbAdapter";
  19. private DatabaseHelper mDbHelper;
  20. private SQLiteDatabase mDb;
  21.  
  22. private static final String DATABASE_NAME = "images.db";
  23. private static final String SQLITE_TABLE = "Images";
  24. private static final int DATABASE_VERSION = 1;
  25.  
  26. private final Context mCtx;
  27.  
  28. private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
  29. + SQLITE_TABLE + " (" + KEY_ROWID
  30. + " integer PRIMARY KEY autoincrement," + KEY_PATH + ");";
  31.  
  32. private static class DatabaseHelper extends SQLiteOpenHelper {
  33.  
  34. DatabaseHelper(Context context) {
  35. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  36. }
  37.  
  38. @Override
  39. public void onCreate(SQLiteDatabase db) {
  40. Log.w(TAG, DATABASE_CREATE);
  41. db.execSQL(DATABASE_CREATE);
  42. }
  43.  
  44. @Override
  45. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  46. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  47. + newVersion + ", which will destroy all old data");
  48. db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
  49. onCreate(db);
  50. }
  51. }
  52.  
  53. public DbAdapter(Context ctx) {
  54. this.mCtx = ctx;
  55. }
  56.  
  57. public DbAdapter open() throws SQLException {
  58. mDbHelper = new DatabaseHelper(mCtx);
  59. mDb = mDbHelper.getWritableDatabase();
  60. return this;
  61. }
  62.  
  63. public void close() {
  64. if (mDbHelper != null) {
  65. mDbHelper.close();
  66. }
  67. }
  68.  
  69. public long addPath(String path) {
  70.  
  71. ContentValues initialValues = new ContentValues();
  72. initialValues.put(KEY_PATH, path);
  73.  
  74. return mDb.insert(SQLITE_TABLE, null, initialValues);
  75. }
  76.  
  77. public boolean deleteAllCountries() {
  78.  
  79. int doneDelete = 0;
  80. doneDelete = mDb.delete(SQLITE_TABLE, null, null);
  81. Log.w(TAG, Integer.toString(doneDelete));
  82. return doneDelete > 0;
  83.  
  84. }
  85.  
  86. public ArrayList<String> fetchAll() {
  87. ArrayList<String> arrayList = new ArrayList<String>();
  88. Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] { KEY_ROWID,
  89. KEY_PATH}, null, null, null, null,
  90. null);
  91.  
  92. if (mCursor.moveToFirst()) {
  93. do {
  94. arrayList.add(mCursor.getString(1));
  95. } while (mCursor.moveToNext());
  96. }
  97. mCursor.close();
  98. return arrayList;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement