Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.androidhive.imageslider;
- import java.util.ArrayList;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DbAdapter {
- public static final String KEY_ROWID = "_id";
- public static final String KEY_PATH = "path";
- private static final String TAG = "ImagesDbAdapter";
- private DatabaseHelper mDbHelper;
- private SQLiteDatabase mDb;
- private static final String DATABASE_NAME = "images.db";
- private static final String SQLITE_TABLE = "Images";
- private static final int DATABASE_VERSION = 1;
- private final Context mCtx;
- private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
- + SQLITE_TABLE + " (" + KEY_ROWID
- + " integer PRIMARY KEY autoincrement," + KEY_PATH + ");";
- private static class DatabaseHelper extends SQLiteOpenHelper {
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- Log.w(TAG, DATABASE_CREATE);
- db.execSQL(DATABASE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
- onCreate(db);
- }
- }
- public DbAdapter(Context ctx) {
- this.mCtx = ctx;
- }
- public DbAdapter open() throws SQLException {
- mDbHelper = new DatabaseHelper(mCtx);
- mDb = mDbHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- if (mDbHelper != null) {
- mDbHelper.close();
- }
- }
- public long addPath(String path) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_PATH, path);
- return mDb.insert(SQLITE_TABLE, null, initialValues);
- }
- public boolean deleteAllCountries() {
- int doneDelete = 0;
- doneDelete = mDb.delete(SQLITE_TABLE, null, null);
- Log.w(TAG, Integer.toString(doneDelete));
- return doneDelete > 0;
- }
- public ArrayList<String> fetchAll() {
- ArrayList<String> arrayList = new ArrayList<String>();
- Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] { KEY_ROWID,
- KEY_PATH}, null, null, null, null,
- null);
- if (mCursor.moveToFirst()) {
- do {
- arrayList.add(mCursor.getString(1));
- } while (mCursor.moveToNext());
- }
- mCursor.close();
- return arrayList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement