I am building a simple app which stores some contacts and retrieves contacts in android phone device.
I have created my own database and a table and inserting the values to the table in phone.
My phone is not rooted. So I cannot access the files, but I see that values are stored in the table. And tested on a emulator also. Till here it is fine.
Display all the contacts in a list by fetching data from table. This is also fine.
But the problem is When I am trying to delete the record, it shows the table name is null in the logcat(not an exception), and the data is not deleted. But in emulator the data is getting deleted from table. I am not able to achieve this through phone.
This is my code for deleting,
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = getResources().getStringArray(R.array.menu);
String menuItemName = menuItems[menuItemIndex];
String listItemName = Customers[info.position];
if (item.getTitle().toString().equalsIgnoreCase("Delete")) {
Toast.makeText(
context,
"Selected List item is: " + listItemName + "MenuItem is: "
+ menuItemName, Toast.LENGTH_LONG).show();
DB = context.openOrCreateDatabase("CustomerDetails.db",
MODE_PRIVATE, null);
try {
int pos = info.position;
pos = pos + 1;
Log.d("", "customers[pos]: " + Customers[info.position]);
Cursor c = DB
.rawQuery(
"Select customer_id,first_name,last_name from CustomerInfo",
null);
int rowCount = c.getCount();
DB.delete(Table_name,
"customer_id" + "=" + String.valueOf(pos), null);
DB.close();
Log.d("", "" + String.valueOf(pos));
Toast.makeText(context, "Deleted Customer", Toast.LENGTH_LONG)
.show();
// Customers[info.position]=null;
getCustomers();
} catch (Exception e) {
Toast.makeText(context, "Delete unsuccessfull",
Toast.LENGTH_LONG).show();
}
}
this is my logcat,
07-02 10:12:42.976: D/Cursor(1560): Database path: CustomerDetails.db
07-02 10:12:42.976: D/Cursor(1560): Table name : null
07-02 10:12:42.984: D/Cursor(1560): Database path: CustomerDetails.db
07-02 10:12:42.984: D/Cursor(1560): Table name : null
Don't know the reason why data is not being deleted. Data exists in the table.
This is the specification I have given for creating the table
public static String customer_id="customer_id";
public static String site_id="site_id";
public static String last_name="last_name";
public static String first_name="first_name";
public static String phone_number="phone_number";
public static String address="address";
public static String city="city";
public static String state="state";
public static String zip="zip";
public static String email_address="email_address";
public static String custlat="custlat";
public static String custlng="custlng";
public static String Table_name="CustomerInfo";
final SQLiteDatabase DB = context.openOrCreateDatabase(
"CustomerDetails.db", MODE_PRIVATE, null);
final String CREATE_TABLE = "create table if not exists " + Table_name
+ " (" + customer_id + " integer primary key autoincrement, "
+ first_name + " text not null, "
+ last_name + " text not null, "
+ phone_number+ " integer not null, "
+ address+ " text not null, "
+ city+ " text not null, "
+ state+ " text not null, "
+ zip+ " integer not null, "
+ email_address+ " text not null, "
+ custlat+ " double, "
+ custlng+ " double "
+" );";
DB.execSQL(CREATE_TABLE);
DB.close();
Please correct my code. I am struggling from two days.
Any help is appreciated!!