want to delete database file in my app. .tried with a program, that did not work - android-sqlite

I am trying to delete database table either programmatically or manually.
I created my own app which is under development.
My app is named SWULJ CT Conductor
But I do not find it under android/data/data folder with any com.xxx.xxx name format
code:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "BusDetails.db";
public static final String TABLE_NAME = "bus_details_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NUMBER";
public static final String COL_3 = "ROUTE";
public static final String COL_4 = "CITY";
public static final String COL_5 = "STATUS";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String myPath = TABLE_NAME + DATABASE_NAME;
SQLiteDatabase.deleteDatabase(new File(myPath));
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NUMBER TEXT,ROUTE TEXT,CITY TEXT,STATUS INTEGER)");
}
It is the partial code for the databasehelper class
It is called from Activity like this:
public class GenerateQrCodeActivity extends AppCompatActivity {
ImageButton imgButtonGenerateBulk;
ImageButton imgButtonGenerateSingle;
DatabaseHelper myDb;
EditText edit;
boolean flag = false;
String data_bus_number=null;
int ID_bus_number = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate_qr_code);
myDb = new DatabaseHelper(this);
imgButtonGenerateBulk =(ImageButton)findViewById(R.id.imageButtonGenerateBulk);
imgButtonGenerateSingle =(ImageButton)findViewById(R.id.imageButtonGenerateSingle);
edit = (EditText) findViewById(R.id.bus_number);
.....
.....
I placed lines
String myPath = TABLE_NAME + DATABASE_NAME;
SQLiteDatabase.deleteDatabase(new File(myPath));
But the old data from the database shows up. It is not deleted. Why? how to fix?

done using the function->
public void deleteDatabase() {
// Are you sure? (y/n)
final SQLiteDatabase db = this.getWritableDatabase();
final File dbFile = new File(db.getPath());
db.close();
Toast.makeText(cntxt, "db deleted", Toast.LENGTH_SHORT).show();
edit.setText("db deleted");
if (dbFile.exists()) {
SQLiteDatabase.deleteDatabase(dbFile);
}
//mOpenHelper = new DatabaseHelper(getContext());
}

Related

Select by ResultHandler will put a empty list objcet in localCache

MyBatis version
3.5.4
Database vendor and version
MySQL 5.7.14
Test case or example project
public static final String URL = "jdbc:mysql://localhost:3306/myTest?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
public static final String Driver = "com.mysql.cj.jdbc.Driver";
public static final String User_Name = "root";
public static final String Password = "";
public static void main(String[] args) {
DataSource dataSource = new PooledDataSource(Driver,URL, User_Name,Password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.select("selectCount",resultContext -> {
System.out.println(resultContext.getResultCount());
System.out.println(resultContext.getResultObject());
});
Object c = sqlSession.selectOne("selectCount");
System.out.println(c);
}
public interface BlogMapper {
#Select("select count(*) from datas")
public int selectCount();
}
Steps to reproduce
Expected result
Object c = sqlSession.selectOne("selectCount");
selectOne function should retuen result ,but return null.
Actual result
sqlSession.selectOne("selectCount"); return null

Using Green DAO with content provider get error

I use GreenDao to generate ContentProvider and when I trying to use it went wrong.it tell me "DaoSession must be set during content provider is active".I dont know where to set the DaoSession.
ContentProvider class as follows
public class ContactContentProvider extends ContentProvider {
public static final String AUTHORITY = "com.junsucc.www.provider";
public static final String BASE_PATH = "contact";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ "/" + BASE_PATH;
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ "/" + BASE_PATH;
private static final String TABLENAME = ContactDao.TABLENAME;
private static final String PK = ContactDao.Properties.Id
.columnName;
private static final int CONTACT_DIR = 0;
private static final int CONTACT_ID = 1;
private static final UriMatcher sURIMatcher;
static {
sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sURIMatcher.addURI(AUTHORITY, BASE_PATH, CONTACT_DIR);
sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONTACT_ID);
}
public DaoSession daoSession=BaseApplication.getDaoSession();
#Override
public boolean onCreate() {
// if(daoSession == null) {
// throw new IllegalStateException("DaoSession must be set before content provider is created");
// }
DaoLog.d("Content Provider started: " + CONTENT_URI);
return true;
}
protected SQLiteDatabase getDatabase() {
if (daoSession == null) {
throw new IllegalStateException("DaoSession must be set during content provider is active");
}
return daoSession.getDatabase();
}
......
the error as follow
java.lang.IllegalStateException: DaoSession must be set during content provider is active
at com.junsucc.www.ContactContentProvider.getDatabase(ContactContentProvider.java:71)
at com.junsucc.www.ContactContentProvider.insert(ContactContentProvider.java:83)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:220)
at android.content.ContentResolver.insert(ContentResolver.java:1190)
at com.junsucc.junsucc.MD5UtilsTest.testProvider(MD5UtilsTest.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
but I had setted th DaoSession inside my Application
public class BaseApplication extends Application {
private static Context mContext;
private static DaoMaster mDaoMaster;
private static DaoSession mDaoSession;
public static DaoMaster getDaoMaster() {
return mDaoMaster;
}
public static Context getContext() {
return mContext;
}
#Override
public void onCreate() {
mContext = getApplicationContext();
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(mContext, Constants.DB_NAME, null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
mDaoSession = mDaoMaster.newSession();
super.onCreate();
}
}
Follow the advice of the framework
/**
* This must be set from outside, it's recommended to do this inside your Application object.
* Subject to change (static isn't nice).
*/
public static DaoSession daoSession;
In your applicaction code
#Override
public void onCreate() {
super.onCreate();
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(this, Constants.DB_NAME, null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
mDaoSession = mDaoMaster.newSession();
/***********************************************/
ContactContentProvider.daoSession = mDaoSession;
/***********************************************/
}
Because ContentProvider is created ahead of Application.
So daoSession will be null when ContentProvider created.

Not able to insert in second table (Transaction) in android sqlite

I have two tables, category and transaction. I have single SQLiteOpenHelper class and two classes to insert and retrieve category and transaction. But I am not able to insert in transaction table. Getting negative number while inserting.
DatabaseHelper Class
package com.mm.bipin.mm;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="mm";
private static final int DATABASE_VERSION=1;
public static final String CATEGORY_TABLE_NAME="category";
public static final String CATEGORY_CID="_id";
public static final String CATEGORY_TITLE= "title";
public static final String CATEGORY_DATE="date";
public static final String CATEGORY_TYPE="type";
public static final String CATEGORY_NOTE="note";
private static final String CATEGORY_CREATE_TABLE= "CREATE TABLE "+CATEGORY_TABLE_NAME+" ( "+CATEGORY_CID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
" "+CATEGORY_TITLE+" VARCHAR(255), "+CATEGORY_DATE+" VARCHAR(255),"+CATEGORY_TYPE+" INTEGER,"+CATEGORY_NOTE+" VARCHAR(255) )";
private static final String CATEGORY_DROP_TABLE=
" DROP TABLE IF EXISTS " +CATEGORY_TABLE_NAME;
public static final String TRANSACTION_TABLE_NAME="transaction";
public static final String TRANSACTION_TID="_id";
public static final String TRANSACTION_CATEGORY_ID="category_id";
public static final String TRANSACTION_TITLE="title";
public static final String TRANSACTION_AMOUNT="amount";
public static final String TRANSACTION_DATE="date";
public static final String TRANSACTION_NOTE="note";
private static final String TRANSACTION_CREATE_TABLE= "CREATE TABLE "+TRANSACTION_TABLE_NAME+" " +
" ("+TRANSACTION_TID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
" "+TRANSACTION_CATEGORY_ID+" INTEGER , " +
" "+TRANSACTION_TITLE+" VARCHAR(255) , " +
" "+TRANSACTION_AMOUNT+" VARCHAR(255) , " +
" "+TRANSACTION_DATE+" VARCHAR(255) , " +
" "+TRANSACTION_NOTE+" VARCHAR(255) ) ";
private static final String TRANSACTION_DROP_TABLE=
" DROP TABLE IF EXISTS " +TRANSACTION_TABLE_NAME;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
}
#Override
public void onCreate(SQLiteDatabase db) {
try {
//getWritableDatabase();
db.execSQL(CATEGORY_CREATE_TABLE);
db.execSQL(TRANSACTION_CREATE_TABLE);
Message.message(context, "DB created");
}
catch(android.database.SQLException e){
Message.message(context," "+e);
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
db.execSQL(CATEGORY_DROP_TABLE);
db.execSQL(TRANSACTION_DROP_TABLE);
Message.message(context,"DB dropped");
}
catch(android.database.SQLException e){
Message.message(context," "+e);
}
}
}
CategoryDatabaseAdapter class
package com.mm.bipin.mm.category;
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.SQLiteOpenHelper;
import android.widget.Toast;
import com.mm.bipin.mm.Category;
import com.mm.bipin.mm.DatabaseHelper;
import com.mm.bipin.mm.Message;
import java.util.ArrayList;
public class CategoryDatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
public static String[] columns={DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE,DatabaseHelper.CATEGORY_NOTE};
public CategoryDatabaseAdapter(Context context){
helper=new DatabaseHelper(context);
}
Context context;
public long insertCategory(String title,int year,int month,int day,int type,String note){
db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.CATEGORY_TITLE,title);
String date= convertDateToString(year, month, day);
contentValues.put(DatabaseHelper.CATEGORY_DATE,date);
contentValues.put(DatabaseHelper.CATEGORY_TYPE,type);
contentValues.put(DatabaseHelper.CATEGORY_NOTE,note);
long id=db.insert(DatabaseHelper.CATEGORY_TABLE_NAME,null,contentValues);
return id;
}
public Cursor getAllRows(){
db=helper.getWritableDatabase();
String[] columns={DatabaseHelper.CATEGORY_CID,DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE,
DatabaseHelper.CATEGORY_TYPE,DatabaseHelper.CATEGORY_NOTE};
Cursor mCursor=db.query(DatabaseHelper.CATEGORY_TABLE_NAME,columns,null,null,null,null,null);
if(mCursor!=null){
mCursor.moveToNext();
}
return mCursor;
}
public String convertDateToString(int year,int month,int day){
StringBuilder str=new StringBuilder();
str.append(year);
str.append("-");
str.append(month);
str.append("-");
str.append(day);
return str.toString();
}
}
TransactionDatabaseAdapter class
package com.mm.bipin.mm.transaction;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import com.mm.bipin.mm.DatabaseHelper;
import com.mm.bipin.mm.Message;
import com.mm.bipin.mm.category.CategoryDatabaseAdapter;
public class TransactionDatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
public static String[] columns= {DatabaseHelper.TRANSACTION_TITLE,DatabaseHelper.TRANSACTION_DATE,
DatabaseHelper.TRANSACTION_NOTE};
TransactionDatabaseAdapter(Context context){
helper=new DatabaseHelper(context);
}
public long insertTransaction(String title,int category_id ,String amount,int year,int month,int day,String note){
//SQLiteDatabase db=helper.getWritableDatabase();
db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.TRANSACTION_CATEGORY_ID,category_id);
contentValues.put(DatabaseHelper.TRANSACTION_TITLE,title);
contentValues.put(DatabaseHelper.TRANSACTION_AMOUNT,amount);
String date= convertDateToString(year, month, day);
contentValues.put(DatabaseHelper.TRANSACTION_DATE,date);
contentValues.put(DatabaseHelper.TRANSACTION_NOTE,note);
long id=db.insert(DatabaseHelper.TRANSACTION_TABLE_NAME,null,contentValues);
return id;
}
public String convertDateToString(int year,int month,int day){
StringBuilder str=new StringBuilder();
str.append(year);
str.append("-");
str.append(month);
str.append("-");
str.append(day);
return str.toString();
}
public Cursor getAllTransaction(){
db=helper.getWritableDatabase();
String[] transactionColumns={DatabaseHelper.TRANSACTION_TID,DatabaseHelper.TRANSACTION_CATEGORY_ID, DatabaseHelper.TRANSACTION_TITLE,
DatabaseHelper.TRANSACTION_AMOUNT,DatabaseHelper.TRANSACTION_DATE,DatabaseHelper.TRANSACTION_NOTE};
Cursor mCursor=db.query(DatabaseHelper.TRANSACTION_TABLE_NAME,transactionColumns,null,null,null,null,null);
if(mCursor!=null) mCursor.moveToNext();
return mCursor;
}
}
These are my three classes of database functions. insertTransaction() is returning negative value which means data are not being inserted.
There might be problem due openhelper conflict. But I dont know how and where. Any help will be appreciated. Thanks!
I found the answer myself. I had created table name 'transaction' which happened to be sqlite keyword. I should have paid attention to sql keyword. Now I changed my table name to 'transactions' and it's working now.
Thankyou.

apache wicket - column link inside DataTable on table change (lets say filter and getting 0 rows) the opened link page fail

scenario : I am using data table with columns
one of the columns is link .
on run time I calculate the link created
the problem start when:
1. opennig table page P1
2. I clicked on link open in new tab
2.new tab is created with this URL
mit:8080/backoffice/?4-1.ILinkListener-MainPanelComponentWrapper-MainPanelComponent-table-gridForm-grid-body-rows-3-cells-2-cell-link
which is a component on P1 ,this tab generate P2.
changing P1 ,filtering on ajax and the table is empty so table-gridForm has no data
refreshing P2
getting exception
org.apache.wicket.WicketRuntimeException: Component 'MainPanelComponentWrapper:MainPanelComponent:table:gridForm:grid:body:rows:1:cells:2:cell:link' has been removed from page.
i need to create a redirected link that the new page woul be linked to .
how can i achive it ?
public class LinkPropertyColumn<T extends IEntity> extends BOPropertyColumn<T, String> implements IBOExportableColumn<T, String, Object> {
private static final long serialVersionUID = 1L;
private String headerTilte;
private GridViewType navigateTo;
private String routingByProperty;
private String entityId;
private String navigateToDynamicFunction;
private Map<String, String> filterByMap;
protected BOLinkPanel<T> linkPanel ;
public LinkPropertyColumn(String displayModel, String propertyToSortBy, String propertyExpression, String entityId,
String routingByProperty, String headerTilte, String navigateToDynamic) {
super(Model.of(displayModel), propertyToSortBy, propertyExpression);
this.headerTilte = headerTilte;
this.entityId = entityId;
this.navigateToDynamicFunction = navigateToDynamic;
this.routingByProperty = routingByProperty;
}
#Override
public Component getHeader(String componentId) {
return new Label(componentId, headerTilte);
}
#Override
public void populateItem(Item<ICellPopulator<T>> item, String componentId, final IModel<T> rowModel) {
linkPanel = new BOLinkPanel<T>(componentId, rowModel, getPropertyExpression()) {
private static final long serialVersionUID = 1L;
#Override
void onLinkClicked() {
LinkPropertyColumn.this.onLinkClicked(rowModel);
}
};
item.add(linkPanel);
}
public void onLinkClicked(IModel<T> rowModel) {
doing stuff...
params.add(HomePage.ENTITY_ID, idProperty);
final Object routingProperty = routingByProperty == null ? idProperty : BeanUtils.getProperty(object, routingByProperty);
params.set(HomePage.ROUTING_PROPERTY, routingProperty);
HomePage homePage = new HomePage(params);
final RequestCycle requestCycle = RequestCycle.get();
requestCycle.setResponsePage(homePage);
}
}
}
and :
public abstract class BOLinkPanel<T> extends Panel {
private static final long serialVersionUID = 1L;
/**
* #param id
*/
public BOLinkPanel(String id, IModel<T> model, String propertyExpression) {
super(id);
AbstractLink link = getLink();
link.add(new Label("caption", new PropertyModel<String>(model.getObject(), propertyExpression)));
add(link);
}
protected AbstractLink getLink() {
Link<Void> link = new AjaxFallbackLink<Void>("link") {
private static final long serialVersionUID = 1L;
#Override
public void onClick(AjaxRequestTarget target) {
// TODO Auto-generated method stub
BOLinkPanel.this.onLinkClicked();
}
// #Override
// public void onClick() {
// BOLinkPanel.this.onLinkClicked();
//
// }
};
return link;
}
abstract void onLinkClicked();
}
i did :
requestCycle.setResponsePage(HomePage.class, params);

How can we filter the table viewer in JFace based on the entered text

I have created a table using table viewer and now i need to filter based on the text entered in the text box so how can we filter the table the code to create table is as follows
TableViewerColumn message = new TableViewerColumn(viewer, SWT.NONE);
message.getColumn().setWidth(800);
message.getColumn().setText("Message");
message.setLabelProvider(new ColumnLabelProvider()
{
#Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if(element instanceof MyObject)
{
MyObject obj = (MyObject) element;
cell.setText(obj.getMessage());
}
}
});
}
private static class MyObject
{
private String first;
private String second;
private String message;
public MyObject(String first, String second,String message)
{
this.first = first;
this.second = second;
this.message = message;
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getSecond()
{
return second;
}
public void setSecond(String message)
{
this.second = second;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
so now how can we filter the table. Please help me as I am new to jface table viewer
Use a class derived from ViewerFilter to add a filter:
class MyFilter extends ViewerFilter
{
#Override
public boolean select(Viewer viewer, Object parentElement, Object element)
{
MyObject obj = (MyObject)element;
// TODO return true to include the object, false to exclude
}
}
Add this to the table with:
viewer.addFilter(new MyFilter());
Call
viewer.refresh();
to get the viewer to rerun the filter when the text changes.