Last active
August 29, 2015 14:07
-
-
Save ziedrebhi/7aad6e0f87ab60b682fc to your computer and use it in GitHub Desktop.
SQLiteDatabaseHandler for SQLite ( 1 Table per DB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tuto SQLite Android | |
* @author Zied Rebhi | |
* http://www.tutozone.info | |
*/ | |
public class SQLiteDatabaseHandler extends SQLiteOpenHelper { | |
private static final int DATABASE_VERSION = 1; | |
private static final String DATABASE_NAME = "FoodsDB"; | |
private static final String TABLE_NAME = "FoodsTable"; | |
private static final String KEY_ID = "id"; | |
private static final String KEY_NAME = "name"; | |
private static final String KEY_PRICE = "price"; | |
private static final String KEY_QUANTITY = "quantity"; | |
private static final String[] COLONNES = { KEY_ID, KEY_NAME, KEY_PRICE, | |
KEY_QUANTITY }; | |
public SQLiteDatabaseHandler(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
Log.i("SQLite DB : Constructeur ", "Constructeur"); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase arg0) { | |
String CREATION_TABLE_EXEMPLE = "CREATE TABLE FoodsTable ( " | |
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT, " | |
+ "price TEXT, " + "quantity INTEGER )"; | |
arg0.execSQL(CREATION_TABLE_EXEMPLE); | |
Log.i("SQLite DB", "Creation"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { | |
// TODO Auto-generated method stub | |
arg0.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); | |
this.onCreate(arg0); | |
Log.i("SQLite DB", "Upgrade"); | |
} | |
public void deleteOne(Food food) { | |
// 1. get reference to writable DB | |
SQLiteDatabase db = this.getWritableDatabase(); | |
db.delete(TABLE_NAME, // table | |
"id = ?", new String[] { String.valueOf(food.getId()) }); | |
db.close(); | |
Log.i("SQLite DB : Delete : ", food.toString()); | |
} | |
public Food showOne(int id) { | |
SQLiteDatabase db = this.getReadableDatabase(); | |
Cursor cursor = db.query(TABLE_NAME, // a. table | |
COLONNES, // b. column names | |
" id = ?", // c. selections | |
new String[] { String.valueOf(id) }, // d. selections args | |
null, // e. group by | |
null, // f. having | |
null, // g. order by | |
null); // h. limit | |
if (cursor != null) | |
cursor.moveToFirst(); | |
Food food = new Food(); | |
food.setId(Integer.parseInt(cursor.getString(0))); | |
food.setName(cursor.getString(1)); | |
food.setPrice(Float.parseFloat(cursor.getString(2))); | |
food.setQuantity(Integer.parseInt(cursor.getString(3))); | |
// log | |
Log.i("SQLite DB : Show one : id= "+id, food.toString()); | |
return food; | |
} | |
public List<Food> showAll() { | |
List<Food> foods = new LinkedList<Food>(); | |
String query = "SELECT * FROM " + TABLE_NAME; | |
SQLiteDatabase db = this.getWritableDatabase(); | |
Cursor cursor = db.rawQuery(query, null); | |
Food food = null; | |
if (cursor.moveToFirst()) { | |
do { | |
food = new Food(); | |
food.setId(Integer.parseInt(cursor.getString(0))); | |
food.setName(cursor.getString(1)); | |
food.setPrice(Float.parseFloat(cursor.getString(2))); | |
food.setQuantity(Integer.parseInt(cursor.getString(3))); | |
foods.add(food); | |
} while (cursor.moveToNext()); | |
} | |
Log.i("SQLite DB : Show All : ", foods.toString()); | |
return foods; | |
} | |
public void addOne(Food food) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(KEY_NAME, food.getName()); | |
values.put(KEY_PRICE, food.getPrice()); | |
values.put(KEY_QUANTITY, food.getQuantity()); | |
// insertion | |
db.insert(TABLE_NAME, // table | |
null, values); | |
db.close(); | |
Log.i("SQLite DB : Add one : id= "+food.getId(), food.toString()); | |
} | |
public int updateOne(Food food) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(KEY_NAME, food.getName()); | |
values.put(KEY_PRICE, food.getPrice()); | |
values.put(KEY_QUANTITY, food.getQuantity()); | |
int i = db.update(TABLE_NAME, // table | |
values, // column/value | |
"id = ?", // selections | |
new String[] { String.valueOf(food.getId()) }); | |
db.close(); | |
Log.i("SQLite DB : Update one : id= "+food.getId(), food.toString()); | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment