Failed to open Database in android app

前端 未结 2 1844
余生分开走
余生分开走 2021-01-22 09:58

in my app i am trying to fetch some data from database and i am showing it in a list. When i run the app in my Motorola(2.1) and in Samsung Ace(2.3) and i some of my emulators t

相关标签:
2条回答
  • 2021-01-22 10:16

    you try this code

    try {
            db = this.openOrCreateDatabase("chaek.db", MODE_PRIVATE, null);
            db.close();
            openDatabase();
            createTable();
        } catch (SQLException se) {
            Toast.makeText(this, se.getMessage(), 1).show();
        }
    private void openDatabase() {
        try {
    db = SQLiteDatabase.openDatabase("/data/data/com.pack.store/databases/chaek.db",
                    null, SQLiteDatabase.CREATE_IF_NECESSARY);
            //Toast.makeText(this, "DB was opened!", 1).show();
        } catch (SQLiteException e) {
            Toast.makeText(this, e.getMessage(), 1).show();
        }
    }// createDatabase
    
    0 讨论(0)
  • 2021-01-22 10:39
    public class BaseDAO extends OrmLiteSqliteOpenHelper {
    
    public static Dao<?, Integer> instanseDao(Context cont, Class c,String Dbname) {
       File path=new File(DATABASE_PATH);
       File f = new File(DATABASE_PATH+DB_NAME);//改默认数据库目录需先手动创建数据库文件
    if (!path.exists()) {
          path.mkdirs();// 创建一个目录
    }
    if (!f.exists()) {
          try {
              f.createNewFile();//创建文件
          } catch (IOException e) {
              e.printStackTrace();
          }
        }
    
    
    
    
    //how do??????
    
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f,null);//创建数据库实例
        BaseDAO baseDAO = new BaseDAO(cont);
        //baseDAO.onCreate(db); 
        //baseDAO.onUpgrade(db,baseDAO.getConnectionSource(), OLD_DB_VERSION, DB_VERSION);
        //baseDAO.close();//注意要先关掉
        //db =SQLiteDatabase.openDatabase(DATABASE_PATH+DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
    
        try {
            Dao = baseDAO.getDao(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
        CONT=cont;
        C=c;
        DB_NAME = Dbname;
        return Dao;
    }
    
    0 讨论(0)
提交回复
热议问题