import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
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.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DBAdapter extends SQLiteOpenHelper {
private static String DB_PATH = "";
private static String DB_BOOKMARK = "bookmark";
private static String DB_ROWID = "item_id";
private static final String DB_NAME = "NewDB.sqlite";
private static String DB_TABLE = "item_master";
private static String DB_SYNCDATE="sync_date";
private static String DB_ITEMIMG="item_img";
private static String DB_TABITEMDETAIL = "item_detail";
private static String DB_TABSETT = "app_setting";
private static String DB_TABITTEMMEDIA="item_media";
private static String DB_TABCMS = "cms_media";
private static String DB_TABCAT = "category_master";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static DBAdapter mDBConnection;
private DBAdapter(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = "/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/";
}
public static synchronized DBAdapter getDBAdapterInstance(Context context) {
if (mDBConnection == null) {
mDBConnection = new DBAdapter(context);
}
return mDBConnection;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling following method
// 1) an empty database will be created into the default system path of your application
// 2) than we overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getLogin_info()
{
return myDataBase.query("item_master",new String[]{"item_id","item_name","item_img","item_s_desc","item_desc","item_map","bookmark","category_id"}, null, null,null,null,null);
}
public void updateentry(long rid, String bookmark)throws SQLException {
// TODO Auto-generated method stub
try{
ContentValues cvupdate=new ContentValues();
cvupdate.put(DB_BOOKMARK,bookmark);
myDataBase.update(DB_TABLE, cvupdate, DB_ROWID+"="+rid, null);
}catch(Exception e){
Toast.makeText(myContext,e.toString(),Toast.LENGTH_SHORT).show();
}
}
public void InsertData(String sql){
myDataBase.execSQL(sql);
}
public void UpdateDate(String date){
try{
ContentValues cvupdate=new ContentValues();
cvupdate.put(DB_SYNCDATE,date);
myDataBase.update(DB_TABSETT, cvupdate, null, null);
}catch(Exception e){
Toast.makeText(myContext,e.toString(),Toast.LENGTH_SHORT).show();
}
}
public void UpdateItemImage(String image,long id){
try{
ContentValues cvupdate=new ContentValues();
cvupdate.put(DB_ITEMIMG,image);
myDataBase.update(DB_TABLE, cvupdate,DB_ROWID+"="+id, null);
}catch(Exception e){
Toast.makeText(myContext,e.toString(),Toast.LENGTH_SHORT).show();
}
}
public Cursor getbookmark_info(){
return myDataBase.query("item_master",new String[]{"item_id","item_name","item_img","item_s_desc","item_desc","item_map","bookmark","category_id"}, null, null,null,null,null);
}
public Cursor search(String word,Boolean check){
String Search = null;
if(check){
//Search="Select DISTINCT item_master.item_name,item_master.item_id FROM item_master ,item_detail where (item_master.item_name LIKE '%"+word+"%' OR item_master.item_desc LIKE '%"+word+"%' OR item_detail.detail_menu LIKE '%"+word+"%' OR item_detail.detail_menu_titel LIKE '%"+word+"%' OR item_detail.detail_desc LIKE '%"+word+"%') AND item_master.item_id=item_detail.item_id OR item_master.item_name LIKE '%"+word+"%' OR item_master.item_desc LIKE '%"+word+"%'";
Search="Select DISTINCT item_name,item_desc,item_id,item_img FROM item_master where item_name LIKE '%"+word+"%' OR item_s_desc LIKE '%"+word+"%' OR item_desc LIKE '%"+word+"%'";
}/*else{
/*Search="SELECT DISTINCT item_master.item_name,item_master.item_id," +
"(SELECT DISTINCT item_master.item_desc FROM item_master WHERE item_master.item_desc LIKE '%"
+word+"%') as item_desc,(SELECT DISTINCT item_detail.detail_desc FROM item_detail WHERE item_detail.detail_desc LIKE '%"
+word+"%') as detail_desc FROM item_master,item_detail WHERE item_master.item_name LIKE '%"
+word+"%' OR item_master.item_desc LIKE '%"+word+"%' OR item_detail.detail_desc LIKE '%"
+word+"%' OR item_detail.detail_menu LIKE '%"+word+"%' OR item_detail.detail_menutitle LIKE '%"
+word+"%' AND(item_master.item_id=item_detail.item_id)";
*/
// Search="Select DISTINCT item_master.item_desc,item_master.item_id FROM item_master where (item_master.item_desc LIKE '%"+word+"%')";
//}
return myDataBase.rawQuery(Search,null);
}
public Cursor Searchdetail_Des(String word,String result){
String Search;
String ForFilter=result;
// Search="Select DISTINCT item_master.item_name,item_master.item_id FROM item_master ,item_detail where (item_master.item_name LIKE '%"+word+"%' OR item_master.item_desc LIKE '%"+word+"%' OR item_detail.detail_menu LIKE '%"+word+"%' OR item_detail.detail_menutitle LIKE '%"+word+"%' OR item_detail.detail_desc LIKE '%"+word+"%') AND item_master.item_id=item_detail.item_id OR item_master.item_name LIKE '%"+word+"%' OR item_master.item_desc LIKE '%"+word+"%'";
Search="Select DISTINCT item_detail.detail_desc,item_detail.item_id FROM item_detail where item_detail.item_id NOT IN ("+ForFilter+") AND (item_detail.detail_desc LIKE '%"+word+"%')";
return myDataBase.rawQuery(Search,null);
}
//////////////for application setting//////////////////////
public Cursor getSettingInfo(){
return myDataBase.query(DB_TABSETT,new String[]{"set_id","item_wise_img","item_wise_video","has_img","has_video","sync_date","has_cms","has_category","has_text_to_speech","has_common_img","has_common_video","has_map"}, null, null,null,null,null);
}
public Cursor getCategoryInfo(){
return myDataBase.query(DB_TABCAT,new String[]{"category_id","category_name","category_desc","perantid"}, null, null,null,null,null);
}
public Cursor getItemDetailInfo(){
//return myDataBase.rawQuery("SELECT DISTINCT detail_title FROM item_detail", null);
return myDataBase.query(DB_TABITEMDETAIL,new String[]{"detail_id","item_id","detail_menu","detail_desc","detail_menu_titel"}, null, null,null,null,null);
}
public Cursor getAboutInfo(){
return myDataBase.query(DB_TABCMS,new String[]{"cms_id","cms_name","cms_desc"}, null, null,null,null,null);
}
public Cursor getItemMediaInfo(){
return myDataBase.query(DB_TABITTEMMEDIA,new String[]{"media_id","item_id","media_url","media_type"}, null, null,null,null,null);
}
}Add a code snippet to your website: www.paste.org