Saving data to a database is ideal for repeating or structured data, such as contact information. This page assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android.
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DataBaseManager";
// Image table name
private static final String TABLE_NAME = "TakeAwayMenu";
// Image Table Columns names
private static final String KEY_menu_title = "menu_title";
private static final String KEY_menu_id = "menu_id";
private static final String KEY_menu_ingrediants = "menu_ingrediants";
private static final String KEY_menu_price = "menu_price";
private static final String KEY_quantity = "quantity";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_menu_id + " INTEGER PRIMARY KEY ," + KEY_menu_title + " TEXT,"
+ KEY_menu_ingrediants + " TEXT," + KEY_menu_price + " TEXT," + KEY_quantity + " TEXT" + ")";
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
// Drop older table if existed
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(sqLiteDatabase);
}
// Insert data into table
public boolean insertData(int id, String menu_title, String menu_ingrediants, String menu_price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_menu_id, id);
contentValues.put(KEY_menu_title,menu_title);
contentValues.put(KEY_menu_ingrediants,"");
contentValues.put(KEY_menu_price,menu_price);
contentValues.put(KEY_quantity,"1");
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1) {
return false;
}else{
return true;
}
}
public boolean updateData(int id, String menu_title, int quantity, double menu_price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_menu_id, id);
contentValues.put(KEY_menu_title,menu_title);
contentValues.put(KEY_menu_ingrediants,"");
contentValues.put(KEY_menu_price,String.valueOf(menu_price));
contentValues.put(KEY_quantity,String.valueOf(quantity));
db.update(TABLE_NAME, contentValues, KEY_menu_id + " = ?", new String[]{String.valueOf(id)});
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public Cursor getData(String image_name) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME +
"where KEY_IMAGE_NAME = " + image_name,null);
return res;
}
public void deleteAll(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME,null,null);
}
public Integer deleteItem (int index) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, KEY_menu_id + " = ?", new String[]{String.valueOf(index)});
}
public int getCount(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
int count = res.getCount();
return count;
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DataBaseManager";
// Image table name
private static final String TABLE_NAME = "TakeAwayMenu";
// Image Table Columns names
private static final String KEY_menu_title = "menu_title";
private static final String KEY_menu_id = "menu_id";
private static final String KEY_menu_ingrediants = "menu_ingrediants";
private static final String KEY_menu_price = "menu_price";
private static final String KEY_quantity = "quantity";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_menu_id + " INTEGER PRIMARY KEY ," + KEY_menu_title + " TEXT,"
+ KEY_menu_ingrediants + " TEXT," + KEY_menu_price + " TEXT," + KEY_quantity + " TEXT" + ")";
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
// Drop older table if existed
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(sqLiteDatabase);
}
// Insert data into table
public boolean insertData(int id, String menu_title, String menu_ingrediants, String menu_price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_menu_id, id);
contentValues.put(KEY_menu_title,menu_title);
contentValues.put(KEY_menu_ingrediants,"");
contentValues.put(KEY_menu_price,menu_price);
contentValues.put(KEY_quantity,"1");
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1) {
return false;
}else{
return true;
}
}
public boolean updateData(int id, String menu_title, int quantity, double menu_price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_menu_id, id);
contentValues.put(KEY_menu_title,menu_title);
contentValues.put(KEY_menu_ingrediants,"");
contentValues.put(KEY_menu_price,String.valueOf(menu_price));
contentValues.put(KEY_quantity,String.valueOf(quantity));
db.update(TABLE_NAME, contentValues, KEY_menu_id + " = ?", new String[]{String.valueOf(id)});
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public Cursor getData(String image_name) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME +
"where KEY_IMAGE_NAME = " + image_name,null);
return res;
}
public void deleteAll(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME,null,null);
}
public Integer deleteItem (int index) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, KEY_menu_id + " = ?", new String[]{String.valueOf(index)});
}
public int getCount(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
int count = res.getCount();
return count;
}
}
Comments
Post a Comment