I am trying to create an app which uses one time login.When it is opened it checks if the sqlite db is present.If it is present it goes to local login page. If the db is not present it creates a new db. I'm stuck in checking the database part.I tried using this and this. It did not work.
Mainactivity.java
public class MainActivity extends Activity {
boolean exists;
SQLiteDBHelper db;
private static final String DATABASE_PATH = "/data/data/com.example.pos_1/databases";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
db=new SQLiteDBHelper(this);
exists=db.checkDataBase();
if(exists)
{
Toast.makeText(getApplicationContext(),"db present",Toast.LENGTH_LONG).show();
MainActivity.this.startActivity(new Intent(MainActivity.this, Activity_three.class));
}
else
{
Toast.makeText(getApplicationContext(),"db absent",Toast.LENGTH_LONG).show();
MainActivity.this.startActivity(new Intent(MainActivity.this, ActivityTwo.class));
}
}
SQLiteDBHelper
public class SQLiteDBHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "local";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_PATH = "/data/data/com.example.pos_1/databases/";
public static final String TABLE_NAME = "users";
Context context;
//modified constructor
public SQLiteDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
/*public class MyClass {
private Context cont;
// public MyClass(Context c) {
//cont = c;
//}
public boolean MyClass(Context context) {
File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
Toast.makeText(context, "db exists", Toast.LENGTH_LONG).show();
return dbFile.exists();
}
}*/
public boolean checkDataBase() {
/*SQLiteDatabase checkDB = null;
checkDB = SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
if(checkDB==null)
{
Toast.makeText(this.context,"error",Toast.LENGTH_LONG).show();
return false;
}
else
return true;
SQLiteDatabase checkDB = null;
try{
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
Toast.makeText(this.context,"error",Toast.LENGTH_LONG).show();
//database does't exist yet.
}
if(checkDB != null){
Toast.makeText(this.context,"check",Toast.LENGTH_LONG).show();
checkDB.close();
}
return checkDB != null ? true : false;
Log.v(TAG, "is DB present Entry!!!");
boolean checkFlag = true;
SQLiteDatabase testDb;
String testPath = DATABASE_PATH + DATABASE_NAME;
try {
Log.v(TAG, "try block1");
this.getDatabaseName();
testDb = SQLiteDatabase.openDatabase(testPath, null,
SQLiteDatabase.OPEN_READWRITE);
Log.v(TAG, "try block2");
if(testDb!=null)
return true;
}
catch(SQLiteException sqlException){
Toast.makeText(context,sqlException.getMessage(),Toast.LENGTH_LONG);
Log.v(TAG, "DB absent");
checkFlag=false;
}
//Log.v(TAG, "is DB present Exit!!!");
return checkFlag;*/
try{
Log.d("opendb","before opening");
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/com.example.pos_1/databases/local", null,0);
Log.d("opendb","EXIST");
dbe.close();
return true;
}
catch (SQLiteException sqlException)
{
Log.d("opendb","NOT EXIST");
return false;
}
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//sqLiteDatabase.execSQL("create table rhbus(USERNAME TEXT PRIMARY KEY,PASSWORD TEXT)");
executeSQLScript(sqLiteDatabase, "create.sql");
}
private void executeSQLScript(SQLiteDatabase database,String dbname) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
try{
inputStream = assetManager.open(dbname);
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
String[] createScript = outputStream.toString().split(";");
for (int i = 0; i < createScript.length; i++) {
String sqlStatement = createScript[i].trim();
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e){
} catch (SQLException e) {
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}