我正在尝试创建一个使用一次登录的应用程序。当它打开时,它会检查sqlite数据库是否存在。如果它存在,则转到本地登录页面。 如果db不存在,则会创建一个新的db。 我一直在检查数据库部分。我尝试使用this和this。它不起作用。
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));
}
}
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);
}
答案 0 :(得分:0)
我认为您的问题是,因为从未尝试打开数据库,因此永远不会调用onCreate
方法。
我建议在SQLiteDBHelper的构造函数中添加强制尝试来打开数据库。例如使用: -
public SQLiteDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
this.getWritableDatabase(); //<<<< force invocation of the onCreate method if the database doesn't exist.
}
executeSQLScript的以下替换也可能有助于(添加的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++) {
Log.d("DBCreate","Read Line :- " + createScript[i].toString());
String sqlStatement = createScript[i].trim();
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e) {
} catch (SQLException e) {
}
}
如果遇到故障,上述更改将导致数据库被创建,因此修复并重新运行将要求删除数据库(删除应用程序数据或卸载应用程序)