Thursday, December 26, 2013

Android SQLite - Access data from local database where sqlite database kept inside assets folder

public class MyDataBaseHelper extends SQLiteOpenHelper {
private SQLiteDatabase myDataBase;
private final Context myContext;

private static String DB_DIR = "/data/data/com.test/";
private static String DB_NAME = "TestDB.sqlite";
private static String DB_PATH = DB_DIR + DB_NAME;
boolean dbfnd;
Cursor chk;
private String TAG = "Database Helper";

public WineDataHelperNew(Context context) {

super(context, MyDataBaseHelper.DB_NAME, null, 1);
this.myContext = context;
// DB_PATH=myContext.getDatabasePath(DB_NAME).getAbsolutePath();
System.out.println("My DataBase Path: " + DB_PATH);
try {
copyDataBase();
openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void createDataBase() throws IOException {

Log.e("createDataBase1", "+++++++++++++");
boolean dbExist = checkDataBase();

if (dbExist) {
Log.e("createDataBase2", "++++++ database already exist");
// do nothing - database already exist
} else {

// this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

private boolean checkDataBase() {

/*
* //SQLiteDatabase checkDB = null; String myPath = DB_PATH; File dbFile
* = new File(myPath); return dbFile.exists();
*/
SQLiteDatabase checkDB = null;

try {
String myPath = DB_PATH;
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;

// 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 static void copyFile(InputStream fromFile, OutputStream toFile)
throws IOException {
// transfer bytes from the inputfile to the outputfile
System.out.println("In copying.....");
byte[] buffer = new byte[1024];
int length;

try {
while ((length = fromFile.read(buffer)) > 0) {
toFile.write(buffer, 0, length);
System.out.println("Reading & writing the file....");
}
} catch (Exception e) {
System.out.println("Error in copy1 file :" + e.toString());
}
// Close the streams
finally {
try {
if (toFile != null) {
try {
toFile.flush();
} finally {
toFile.close();
}
}
} catch (Exception e) {
System.out.println("Error in copy2 file :" + e.toString());
} finally {
if (fromFile != null) {
fromFile.close();
System.out.println("File copied successfully.....");
}
}
}
}

public static void copyFile(String fromFile, String toFile)
throws IOException {
copyFile(new FileInputStream(fromFile), new FileOutputStream(toFile));
}

// Open the database
public void openDataBase() {

String myPath = DB_PATH;
try {

myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
System.out.println(myDataBase.toString()
+ " Database found....................!!!!!!!");
dbfnd = true;
} catch (Exception e) {
System.out.println("Database not found....................!!!!!!!");
// TODO: handle exception
dbfnd = false;
}
}

public SQLiteDatabase getReadableDatabase() {
myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,

SQLiteDatabase.OPEN_READONLY);
return myDataBase;
}

public SQLiteDatabase getWritableDatabase() {
myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,

SQLiteDatabase.OPEN_READWRITE);

return myDataBase;
}

@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 get_Data() {
chk = null;

try {
System.out.println("select Distinct Bang from city");
chk = myDataBase.rawQuery(
"select Distinct Bangalore from city", null);

} catch (Exception e) {
Log.e(TAG, "Error in city_table");
e.printStackTrace();
return null;
}
return chk;

}

public Cursor get_Brand(String selBrand) {
// TODO Auto-generated method stub
chk = null;

try {
System.out
.println("select brand from city where Bangalore='"
+ selBrand + "'");
chk = myDataBase.rawQuery(
"select OvalBar from city where Bangalore='"
+ selBrand + "'", null);

} catch (Exception e) {
Log.e(TAG, "Error in brands_table");
e.printStackTrace();
return null;
}
return chk;
}

public Cursor get_Brand() {
chk = null;

try {
System.out.println("select Distinct brand_type from brand");
chk = myDataBase.rawQuery(
"select Distinct alcohol_type from brand", null);

} catch (Exception e) {
Log.e(TAG, "Error in brand");
e.printStackTrace();
return null;
}
return chk;

}

public Cursor get_SuperBrand(String selType) {
// TODO Auto-generated method stub
chk = null;

try {
System.out
.println("select brand_name from brand where type='"
+ selSuperType + "'");
chk = myDataBase.rawQuery(
"select brand_name from brand_alcohol where type='"
+ selType + "'", null);

} catch (Exception e) {
Log.e(TAG, "Error in brand");
e.printStackTrace();
return null;
}
return chk;
}

}

5 comments:

  1. 1) Make a list View

    2) Fetch images from the following links and set it on listview (Do all the heavy tasks on background thread for better efficiency)



    "http://farm9.staticflickr.com/8425/7692173050_0cbb01eb57_s.jpg",
    "http://farm8.staticflickr.com/7119/7692005756_8ae8482a99_s.jpg",
    "http://farm8.staticflickr.com/7276/7757589608_685b3f622f_s.jpg",
    "http://farm9.staticflickr.com/8281/7759124280_b0b734dc45_s.jpg",
    "http://farm8.staticflickr.com/7247/7735765962_f58d5c084c_s.jpg",
    "http://farm9.staticflickr.com/8439/7758562400_368bd12463_s.jpg",

    ReplyDelete
  2. http://android-er.blogspot.in/2010/07/load-listview-in-background-asynctask.html

    see this and download demo also

    ReplyDelete
  3. https://www.opendrive.com/files?NF8yMjk1Nzg5OV80d2tkbw

    ReplyDelete
    Replies
    1. Thanks for this Link which is used to show images from server using Lazy loader in the list.

      Delete