Friday, 29 April 2016

database helper

package com.ibv.ibvfinance;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
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.os.IBinder;
import android.util.Log;

public class Databasehelper extends SQLiteOpenHelper {

private SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "ibv";// "db.sqlite";
@SuppressLint("SdCardPath")
public final static String DATABASE_PATH = "/data/data/com.ibv.ibvfinance/databases/";
public static final int DATABASE_VERSION = 1;
private static final String TABAL_ibv = "ibvtb";

private static final String TABAL_goal = "goal";
private static final String TABAL_COUNTER = "counter";


@SuppressLint("SimpleDateFormat")
private SimpleDateFormat dateformate = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");

public Databasehelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}

// Create a empty database on the system
public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();

if (dbExist) {
Log.v("DB Exists", "db exists");

} else {

this.getReadableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}

}

}

// Check database already exist or not
private boolean checkDataBase() {
boolean checkDB = false;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
} catch (SQLiteException e) {
System.out.println("delete database file.");
}
return checkDB;
}

// Copies your database from your local assets-folder to the just created
// empty database in the system folder
private void copyDataBase() throws IOException {

String outFileName = DATABASE_PATH + DATABASE_NAME;

OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}

// delete database
public void db_delete() {
File file = new File(DATABASE_PATH + DATABASE_NAME);
if (file.exists()) {
file.delete();
System.out.println("delete database file.");
}
}

// Open database
public void openDatabase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void closeDataBase() throws SQLException {
if (myDataBase != null)
myDataBase.close();
super.close();
}

public boolean onSubmitForGcNo(HashMap<String, String> caclulatedData) {
return false;
}


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
Log.v("Database Upgrade", "Database version higher than old.");
db_delete();
}
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}


//--------------chating function----

// --insert chating data to offline-----


public boolean Insert_Goal(String date , String time , String userid, String goal, String task)

{

String Quiry = "insert into "+ TABAL_ibv+ " (date , time , userid , goal , work) values ('"+date+"' , '"+time+"' , '"+userid+"' , '"+goal+"' , '"+task+"')";
try {

myDataBase.execSQL(Quiry);
Log.v("insert", "success");
return true;



} catch (Exception e) {
Log.v("insert error", ""+e.toString());
return false;

}


}


public void Insert_place(String place)

{

String Quiry = "insert into "+ TABAL_goal+ " (name) values ('"+place+"')";
try {

myDataBase.execSQL(Quiry);
Log.v("insert", "success place");


} catch (Exception e) {
Log.v("insert error place", ""+e.toString());

}

}


public void InsertCounter(int value)

{
int id = 1;
String Quiry = "insert into "+ TABAL_COUNTER+ " (id , value) values ('"+id+"','"+value+"',)";
try {

myDataBase.execSQL(Quiry);
Log.v("insert", "success");


} catch (Exception e) {
Log.v("insert error", ""+e.toString());

}

}


public void UpdateCounter(int value)

{

String Quiry = "Update "+ TABAL_COUNTER + " set value='"+value+"' WHERE id=1";

try {

myDataBase.execSQL(Quiry);
Log.v("insert", "success");


} catch (Exception e) {
Log.v("insert error", ""+e.toString());

}

}



public List<String> get_place()
{

String quiry = "Select * from "+TABAL_goal+ "";

List<String> goallist = new ArrayList<String>();

try {
Cursor cursor = myDataBase.rawQuery(quiry, null);
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
goallist.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}

} catch (Exception e) {
// TODO: handle exception
}

return goallist;

}


public List<Datatype> get_all_from_goalName(String goalname  ,String userid  ,String fromdt  ,String todt)
{
fromdt = "\'"+fromdt+"\'";
todt = "\'"+todt+"\'";

String quiry = "SELECT * FROM ibvtb WHERE goal='"+goalname +"' AND userid='"+userid +"' AND date BETWEEN "+fromdt+" AND "+todt+" order by id desc";


List<Datatype> goallist = new ArrayList<Datatype>();

try {
Cursor cursor = myDataBase.rawQuery(quiry, null);
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
Datatype d = new Datatype();
d.setDate_offline(cursor.getString(0));
d.setTime_offline(cursor.getString(1));
d.setGoal_offline(cursor.getString(3));
d.setWork_offline(cursor.getString(4));

goallist.add(d);
} while (cursor.moveToNext());
}
}

} catch (Exception e) {
// TODO: handle exception
}

return goallist;

}

public List<Datatype> get_all(String fromdt ,String todt , String userid )
{


fromdt = "\'"+fromdt+"\'";
todt = "\'"+todt+"\'";

String quiry = "SELECT * FROM ibvtb WHERE userid='"+userid +"' AND date BETWEEN "+fromdt+" AND "+todt+" order by id desc";


// String quiry = "Select * from "+TABAL_ibv+ "";

List<Datatype> goallist = new ArrayList<Datatype>();

try {
Cursor cursor = myDataBase.rawQuery(quiry, null);
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
Datatype d = new Datatype();
d.setDate_offline(cursor.getString(0));
d.setTime_offline(cursor.getString(1));
d.setGoal_offline(cursor.getString(3));
d.setWork_offline(cursor.getString(4));


goallist.add(d);
} while (cursor.moveToNext());
}
}

} catch (Exception e) {
// TODO: handle exception
}

return goallist;

}

public List<String> getcounter(String id)
{

String quiry = "Select * from "+TABAL_COUNTER+ " Where id='"+id+"'";

List<String> chatlist = new ArrayList<String>();

try {
Cursor cursor = myDataBase.rawQuery(quiry, null);
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
chatlist.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}

} catch (Exception e) {
// TODO: handle exception
}

return chatlist;

}







}

No comments:

Post a Comment