1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| 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_NOM = "nom";
public static final String KEY_NUM = "num";
public static final String KEY_TITRE = "titre";
public static final String KEY_IMG = "img";
public static final String KEY_FORMAT = "format";
public static final String KEY_ROWID = "_id";
private static final String TAG = "DBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table lesImagesTB (_id integer primary key autoincrement, "
+ "abonne text not null,"
+ "num integer not null,"
+ "titre text not null,"
+ "img longtext not null,"
+ "format text not null)"
+";";
private static final String DATABASE_NAME = "ImagesDB";
private static final String DATABASE_TABLE_IMAGES = "lesImagesTB";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
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 lesImagesTB");
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() {
mDbHelper.close();
}
public long createImages(String nom, String num, String titre,
String img, String format) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NOM, nom);
initialValues.put(KEY_NUM, num);
initialValues.put(KEY_TITRE, titre);
initialValues.put(KEY_IMG, img);
initialValues.put(KEY_FORMAT, format);
return mDb.insert(DATABASE_TABLE_IMAGES, null, initialValues);
}
public long createNouveau(String nom) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NOM, nom);
return mDb.insert(DATABASE_TABLE_IMAGES, null, initialValues);
}
public boolean deleteOeuvre(long rowId) {
return mDb.delete(DATABASE_TABLE_IMAGES, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAllImage(String lenom) {
return mDb.query(DATABASE_TABLE_IMAGES, new String[] {KEY_ROWID, KEY_NOM,
KEY_NUM, KEY_TITRE, KEY_IMG, KEY_FORMAT
}, KEY_NOM + "='" + lenom + "'", null, null, null, null);
}
public Cursor fetchAllListImages() {
return mDb.query(DATABASE_TABLE_IMAGES, new String[] {KEY_ROWID, KEY_NOM,
KEY_NUM, KEY_TITRE, KEY_IMG, KEY_FORMAT
}, null, null, null, null, null);
}
public Cursor fetchLesImages(long rowId) throws SQLException {
Cursor oCursor =
mDb.query(true, DATABASE_TABLE_IMAGES, new String[] {KEY_ROWID,
KEY_NOM, KEY_NUM, KEY_TITRE, KEY_IMG, KEY_FORMAT}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (oCursor != null) {
oCursor.moveToFirst();
}
return oCursor;
}
public boolean updateImages(long rowId, String nom, String num,
String titre, String img, String format) {
ContentValues args = new ContentValues();
args.put(KEY_NOM, nom);
args.put(KEY_NUM, num);
args.put(KEY_TITRE, titre);
args.put(KEY_IMG, img);
args.put(KEY_FORMAT, format);
return mDb.update(DATABASE_TABLE_IMAGES, args, KEY_ROWID + "=" + rowId, null) > 0;
}
} |