Helper.java
package com.example.feedback;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.util.Log;
public class Helper extends SQLiteOpenHelper{
// database variables.
//instead of com.mmad.irctc you have to write your own package name.
private static String DB_PATH = "/data/data/com.example.feedback/databases/";
//database name with sqllite extension
private static String DB_NAME = "feedback.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private String TAG = "Helper";
Cursor cursorGetData;
SQLiteDatabase checkDB = null;
public Helper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e(TAG, "Error is" + e.toString());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
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);
}
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
private Cursor getData(String sql) {
openDataBase();
cursorGetData = getReadableDatabase().rawQuery(sql, null);
return cursorGetData;
}
private long insertData(String tableName, ContentValues values) {
openDataBase();
return myDataBase.insert(tableName, null, values);
}
private int updateData(String tableName, ContentValues values,String condition) {
openDataBase();
return myDataBase.update(tableName, values, condition, null);
}
private int deleteData(String tableName, String condition) {
return myDataBase.delete(tableName, condition, null);
}
//Select from the database
public List<CustomerInformationBeen> getGRCInfo() {
ArrayList<CustomerInformationBeen> cList=new ArrayList<CustomerInformationBeen>();
try {
String sql="Select * from customer_registration";
Cursor cursor=getData(sql);
int size = cursor.getCount();
cursor.moveToFirst();
for(int i=0;i<size;i++){
CustomerInformationBeen ci=new CustomerInformationBeen();
ci.setID(cursor.getInt(cursor.getColumnIndex("cr_id")));
ci.setName(cursor.getString(cursor.getColumnIndex("cr_name")));
ci.setAddress(cursor.getString(cursor.getColumnIndex("cr_address")));
ci.setCity(cursor.getString(cursor.getColumnIndex("cr_city")));
ci.setState(cursor.getString(cursor.getColumnIndex("cr_state")));
ci.setCountry(cursor.getString(cursor.getColumnIndex("cr_country")));
ci.setPin(cursor.getString(cursor.getColumnIndex("cr_pin")));
ci.setNationality(cursor.getString(cursor.getColumnIndex("cr_nationality")));
ci.setSex(cursor.getString(cursor.getColumnIndex("cr_sex")));
ci.setAge(cursor.getInt(cursor.getColumnIndex("cr_age")));
ci.setCompany(cursor.getString(cursor.getColumnIndex("cr_company")));
ci.setMobile(cursor.getString(cursor.getColumnIndex("cr_mobile")));
ci.setPhone(cursor.getString(cursor.getColumnIndex("cr_phone")));
ci.setEmail(cursor.getString(cursor.getColumnIndex("cr_email")));
ci.setDob(cursor.getString(cursor.getColumnIndex("cr_dob")));
ci.setDoa(cursor.getString(cursor.getColumnIndex("cr_doa")));
cList.add(ci);
cursor.moveToNext();
}
cursor.close();
cursorGetData.close();
myDataBase.close();
} catch (Exception e) {
// TODO: handle exception
}
return cList;
}
//getting selected data
public Map<Integer, String> getSelectedCusInfo(int id) {
Map<Integer, String> sMap = new HashMap<Integer, String>();
try {
String sql="Select * from customer_registration where cr_id="+id;
Cursor cursor=getData(sql);
int size = cursor.getCount();
cursor.moveToFirst();
for(int i=0;i<size;i++){
sMap.put(1,cursor.getString(cursor.getColumnIndex("cr_name")));
sMap.put(2,cursor.getString(cursor.getColumnIndex("cr_address")));
sMap.put(3,cursor.getString(cursor.getColumnIndex("cr_city")));
sMap.put(4,cursor.getString(cursor.getColumnIndex("cr_state")));
sMap.put(5,cursor.getString(cursor.getColumnIndex("cr_country")));
sMap.put(6,cursor.getString(cursor.getColumnIndex("cr_pin")));
sMap.put(7,cursor.getString(cursor.getColumnIndex("cr_nationality")));
sMap.put(8,cursor.getString(cursor.getColumnIndex("cr_sex")));
sMap.put(9,cursor.getString(cursor.getColumnIndex("cr_age")));
sMap.put(10,cursor.getString(cursor.getColumnIndex("cr_company")));
sMap.put(11,cursor.getString(cursor.getColumnIndex("cr_mobile")));
sMap.put(12,cursor.getString(cursor.getColumnIndex("cr_phone")));
sMap.put(13,cursor.getString(cursor.getColumnIndex("cr_email")));
sMap.put(14,cursor.getString(cursor.getColumnIndex("cr_dob")));
sMap.put(15,cursor.getString(cursor.getColumnIndex("cr_doa")));
}
} catch (Exception e) {
// TODO: handle exception
}
return sMap;
}
/*
//Select from database
public ArrayList<UserContacts> getContactAdult() {
ArrayList<UserContacts> passengerInfo = new ArrayList<UserContacts>();
try {
Cursor cursor = getData("select * from Contacts where C_Age>4");
int size = cursor.getCount();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
UserContacts contact=new UserContacts();
contact.setContactID(cursor.getString(cursor.getColumnIndex("C_ID")));
contact.setContactName(cursor.getString(cursor.getColumnIndex("C_Name")));
contact.setContactAge(cursor.getInt(cursor.getColumnIndex("C_Age")));
contact.setContactDOB(cursor.getString(cursor.getColumnIndex("C_DOB")));
contact.setContactGender(cursor.getString(cursor.getColumnIndex("C_Gender")));
contact.setContactGCode(cursor.getString(cursor.getColumnIndex("C_GCode")));
contact.setContactFoodPref(cursor.getString(cursor.getColumnIndex("C_FoodPref")));
contact.setContactFPCode(cursor.getString(cursor.getColumnIndex("C_FPCode")));
contact.setContactIDCardType(cursor.getString(cursor.getColumnIndex("C_IDCardType")));
contact.setContactIDCCode(cursor.getString(cursor.getColumnIndex("C_IDCCode")));
contact.setContactIDCardNo(cursor.getString(cursor.getColumnIndex("C_IDCardNo")));
contact.setContactSeniorCitizen(cursor.getString(cursor.getColumnIndex("C_SenoirCitizen")));
contact.setContactFlag(cursor.getString(cursor.getColumnIndex("C_flag")));
passengerInfo.add(contact);
cursor.moveToNext();
}
cursor.close();
cursorGetData.close();
myDataBase.close();
} catch (Exception ex) {
}
return passengerInfo;
}
*/
/*
//Update the database
public long updateChildPassenger(String childID, UserContacts usercontact) {
long value = 0;
try
{
ContentValues initialValues = new ContentValues();
SQLiteDatabase db=this.getWritableDatabase();
openDataBase();
initialValues.put("tc_Name",usercontact.getContactName());
initialValues.put("tc_Gender",usercontact.getContactGender());
initialValues.put("tc_Age",usercontact.getContactAge());
initialValues.put("tc_DOB",usercontact.getContactDOB());
initialValues.put("C_ID",new RandomNumber().GenerateRandomNumber());
value = db.update("temp_child", initialValues, "tc_Id="+childID, null);
myDataBase.close();
}
catch (Exception e) {
Log.e("error is",""+e);
}
return value;
}
*/
//delete the database
public int deleteAdultContact(String contactID) {
int value = 0;
try {
openDataBase();
value = deleteData("temp_adult","ta_Id ="+contactID);
myDataBase.close();
} catch (Exception ex) {
Log.d(TAG, "Error in deleteAdultContact()" + ex.toString());
}
return value;
}
public long insertUser(String name, String address,String city,String state,String country,String pin,String nationality,String sex,String age,String company,String mobile,String phone,String email,String dob,String doa) {
long value = 0;
try {
ContentValues initialValues = new ContentValues();
initialValues.put("cr_name", name);
initialValues.put("cr_address", address);
initialValues.put("cr_city", city);
initialValues.put("cr_state", state);
initialValues.put("cr_country", country);
initialValues.put("cr_pin", pin);
initialValues.put("cr_nationality", nationality);
initialValues.put("cr_sex", sex);
initialValues.put("cr_age", age);
initialValues.put("cr_company", company);
initialValues.put("cr_mobile", mobile);
initialValues.put("cr_phone", phone);
initialValues.put("cr_email", email);
initialValues.put("cr_dob", dob);
initialValues.put("cr_doa", doa);
value = insertData("customer_registration", initialValues);
Log.d("value1=", "" + value);
}catch (Exception e) {
// TODO: handle exception
}
return value;
}
}
activity
for insert call the method
long value = helper.insertUser(nameValue,address.getText().toString(),city.getText().toString(),state.getText().toString(),country.getText().toString(),pin.getText().toString(),nationality.getText().toString(),sex.getText().toString(),age.getText().toString(),company.getText().toString(),mobile.getText().toString(),phone.getText().toString(),email.getText().toString(),dob.getText().toString(),doa.getText().toString());
Log.d("Value","Inserted = "+value);
for display
List<CustomerInformationBeen> list=new ArrayList<CustomerInformationBeen>();
list=helper.getGRCInfo();
Iterator<CustomerInformationBeen> itr=list.iterator();
while (itr.hasNext()) {
CustomerInformationBeen ci = itr.next();
int cusID=ci.getID();
String cname=ci.getName();
//creating dynamic textview
TextView cnameText=new TextView(this);
cnameText.setText(cname);
cnameText.setId(cusID);
cusListinfo.addView(cnameText);
}
using map
Map<Integer, String> sMap=new HashMap<Integer, String>();
sMap=helper.getSelectedCusInfo(id);
name.setText(sMap.get(1));
package com.example.feedback;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.util.Log;
public class Helper extends SQLiteOpenHelper{
// database variables.
//instead of com.mmad.irctc you have to write your own package name.
private static String DB_PATH = "/data/data/com.example.feedback/databases/";
//database name with sqllite extension
private static String DB_NAME = "feedback.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private String TAG = "Helper";
Cursor cursorGetData;
SQLiteDatabase checkDB = null;
public Helper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e(TAG, "Error is" + e.toString());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
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);
}
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
private Cursor getData(String sql) {
openDataBase();
cursorGetData = getReadableDatabase().rawQuery(sql, null);
return cursorGetData;
}
private long insertData(String tableName, ContentValues values) {
openDataBase();
return myDataBase.insert(tableName, null, values);
}
private int updateData(String tableName, ContentValues values,String condition) {
openDataBase();
return myDataBase.update(tableName, values, condition, null);
}
private int deleteData(String tableName, String condition) {
return myDataBase.delete(tableName, condition, null);
}
//Select from the database
public List<CustomerInformationBeen> getGRCInfo() {
ArrayList<CustomerInformationBeen> cList=new ArrayList<CustomerInformationBeen>();
try {
String sql="Select * from customer_registration";
Cursor cursor=getData(sql);
int size = cursor.getCount();
cursor.moveToFirst();
for(int i=0;i<size;i++){
CustomerInformationBeen ci=new CustomerInformationBeen();
ci.setID(cursor.getInt(cursor.getColumnIndex("cr_id")));
ci.setName(cursor.getString(cursor.getColumnIndex("cr_name")));
ci.setAddress(cursor.getString(cursor.getColumnIndex("cr_address")));
ci.setCity(cursor.getString(cursor.getColumnIndex("cr_city")));
ci.setState(cursor.getString(cursor.getColumnIndex("cr_state")));
ci.setCountry(cursor.getString(cursor.getColumnIndex("cr_country")));
ci.setPin(cursor.getString(cursor.getColumnIndex("cr_pin")));
ci.setNationality(cursor.getString(cursor.getColumnIndex("cr_nationality")));
ci.setSex(cursor.getString(cursor.getColumnIndex("cr_sex")));
ci.setAge(cursor.getInt(cursor.getColumnIndex("cr_age")));
ci.setCompany(cursor.getString(cursor.getColumnIndex("cr_company")));
ci.setMobile(cursor.getString(cursor.getColumnIndex("cr_mobile")));
ci.setPhone(cursor.getString(cursor.getColumnIndex("cr_phone")));
ci.setEmail(cursor.getString(cursor.getColumnIndex("cr_email")));
ci.setDob(cursor.getString(cursor.getColumnIndex("cr_dob")));
ci.setDoa(cursor.getString(cursor.getColumnIndex("cr_doa")));
cList.add(ci);
cursor.moveToNext();
}
cursor.close();
cursorGetData.close();
myDataBase.close();
} catch (Exception e) {
// TODO: handle exception
}
return cList;
}
//getting selected data
public Map<Integer, String> getSelectedCusInfo(int id) {
Map<Integer, String> sMap = new HashMap<Integer, String>();
try {
String sql="Select * from customer_registration where cr_id="+id;
Cursor cursor=getData(sql);
int size = cursor.getCount();
cursor.moveToFirst();
for(int i=0;i<size;i++){
sMap.put(1,cursor.getString(cursor.getColumnIndex("cr_name")));
sMap.put(2,cursor.getString(cursor.getColumnIndex("cr_address")));
sMap.put(3,cursor.getString(cursor.getColumnIndex("cr_city")));
sMap.put(4,cursor.getString(cursor.getColumnIndex("cr_state")));
sMap.put(5,cursor.getString(cursor.getColumnIndex("cr_country")));
sMap.put(6,cursor.getString(cursor.getColumnIndex("cr_pin")));
sMap.put(7,cursor.getString(cursor.getColumnIndex("cr_nationality")));
sMap.put(8,cursor.getString(cursor.getColumnIndex("cr_sex")));
sMap.put(9,cursor.getString(cursor.getColumnIndex("cr_age")));
sMap.put(10,cursor.getString(cursor.getColumnIndex("cr_company")));
sMap.put(11,cursor.getString(cursor.getColumnIndex("cr_mobile")));
sMap.put(12,cursor.getString(cursor.getColumnIndex("cr_phone")));
sMap.put(13,cursor.getString(cursor.getColumnIndex("cr_email")));
sMap.put(14,cursor.getString(cursor.getColumnIndex("cr_dob")));
sMap.put(15,cursor.getString(cursor.getColumnIndex("cr_doa")));
}
} catch (Exception e) {
// TODO: handle exception
}
return sMap;
}
/*
//Select from database
public ArrayList<UserContacts> getContactAdult() {
ArrayList<UserContacts> passengerInfo = new ArrayList<UserContacts>();
try {
Cursor cursor = getData("select * from Contacts where C_Age>4");
int size = cursor.getCount();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
UserContacts contact=new UserContacts();
contact.setContactID(cursor.getString(cursor.getColumnIndex("C_ID")));
contact.setContactName(cursor.getString(cursor.getColumnIndex("C_Name")));
contact.setContactAge(cursor.getInt(cursor.getColumnIndex("C_Age")));
contact.setContactDOB(cursor.getString(cursor.getColumnIndex("C_DOB")));
contact.setContactGender(cursor.getString(cursor.getColumnIndex("C_Gender")));
contact.setContactGCode(cursor.getString(cursor.getColumnIndex("C_GCode")));
contact.setContactFoodPref(cursor.getString(cursor.getColumnIndex("C_FoodPref")));
contact.setContactFPCode(cursor.getString(cursor.getColumnIndex("C_FPCode")));
contact.setContactIDCardType(cursor.getString(cursor.getColumnIndex("C_IDCardType")));
contact.setContactIDCCode(cursor.getString(cursor.getColumnIndex("C_IDCCode")));
contact.setContactIDCardNo(cursor.getString(cursor.getColumnIndex("C_IDCardNo")));
contact.setContactSeniorCitizen(cursor.getString(cursor.getColumnIndex("C_SenoirCitizen")));
contact.setContactFlag(cursor.getString(cursor.getColumnIndex("C_flag")));
passengerInfo.add(contact);
cursor.moveToNext();
}
cursor.close();
cursorGetData.close();
myDataBase.close();
} catch (Exception ex) {
}
return passengerInfo;
}
*/
/*
//Update the database
public long updateChildPassenger(String childID, UserContacts usercontact) {
long value = 0;
try
{
ContentValues initialValues = new ContentValues();
SQLiteDatabase db=this.getWritableDatabase();
openDataBase();
initialValues.put("tc_Name",usercontact.getContactName());
initialValues.put("tc_Gender",usercontact.getContactGender());
initialValues.put("tc_Age",usercontact.getContactAge());
initialValues.put("tc_DOB",usercontact.getContactDOB());
initialValues.put("C_ID",new RandomNumber().GenerateRandomNumber());
value = db.update("temp_child", initialValues, "tc_Id="+childID, null);
myDataBase.close();
}
catch (Exception e) {
Log.e("error is",""+e);
}
return value;
}
*/
//delete the database
public int deleteAdultContact(String contactID) {
int value = 0;
try {
openDataBase();
value = deleteData("temp_adult","ta_Id ="+contactID);
myDataBase.close();
} catch (Exception ex) {
Log.d(TAG, "Error in deleteAdultContact()" + ex.toString());
}
return value;
}
public long insertUser(String name, String address,String city,String state,String country,String pin,String nationality,String sex,String age,String company,String mobile,String phone,String email,String dob,String doa) {
long value = 0;
try {
ContentValues initialValues = new ContentValues();
initialValues.put("cr_name", name);
initialValues.put("cr_address", address);
initialValues.put("cr_city", city);
initialValues.put("cr_state", state);
initialValues.put("cr_country", country);
initialValues.put("cr_pin", pin);
initialValues.put("cr_nationality", nationality);
initialValues.put("cr_sex", sex);
initialValues.put("cr_age", age);
initialValues.put("cr_company", company);
initialValues.put("cr_mobile", mobile);
initialValues.put("cr_phone", phone);
initialValues.put("cr_email", email);
initialValues.put("cr_dob", dob);
initialValues.put("cr_doa", doa);
value = insertData("customer_registration", initialValues);
Log.d("value1=", "" + value);
}catch (Exception e) {
// TODO: handle exception
}
return value;
}
}
activity
for insert call the method
long value = helper.insertUser(nameValue,address.getText().toString(),city.getText().toString(),state.getText().toString(),country.getText().toString(),pin.getText().toString(),nationality.getText().toString(),sex.getText().toString(),age.getText().toString(),company.getText().toString(),mobile.getText().toString(),phone.getText().toString(),email.getText().toString(),dob.getText().toString(),doa.getText().toString());
Log.d("Value","Inserted = "+value);
for display
List<CustomerInformationBeen> list=new ArrayList<CustomerInformationBeen>();
list=helper.getGRCInfo();
Iterator<CustomerInformationBeen> itr=list.iterator();
while (itr.hasNext()) {
CustomerInformationBeen ci = itr.next();
int cusID=ci.getID();
String cname=ci.getName();
//creating dynamic textview
TextView cnameText=new TextView(this);
cnameText.setText(cname);
cnameText.setId(cusID);
cusListinfo.addView(cnameText);
}
using map
Map<Integer, String> sMap=new HashMap<Integer, String>();
sMap=helper.getSelectedCusInfo(id);
name.setText(sMap.get(1));
No comments:
Post a Comment