user275788
user275788

Reputation: 833

Open sqlite database failed

I put my sqlite database file in the "assets" folder And i write a DAO calss to get data from database,But the information from log.e means i can not open the database.

public class GetData {

 private static String DB_PATH = "/data/data/com.SGMalls/databases/mallMapv2.sqlite";

private static SQLiteDatabase myDataBase;

public static ArrayList<Mall> getMall(){

   ArrayList<Mall> mallArrayList=new ArrayList<Mall>();

    String queryString="select id,title from malls order by title";

    myDataBase = SQLiteDatabase.openDatabase(DB_PATH,      null,SQLiteDatabase.OPEN_READONLY);

    Cursor cursor=myDataBase.rawQuery(queryString, null);

  if(cursor!=null){

    cursor.moveToFirst();

    while(!cursor.isLast()){

     Mall mall=new Mall();

     mall.setMallid(cursor.getInt(0));

     mall.setMallname(cursor.getString(1));            
    mallArrayList.add(mall);
cursor.moveToNext();
   } }
 myDataBase.close();

 return mallArrayList;

}}

Upvotes: 1

Views: 3877

Answers (2)

ccheneson
ccheneson

Reputation: 49410

Have a look at this link .

You will have to call first createDataBase() method. If createDataBase() runs successfully, you can check your /data/data/com.SGMalls/databases/mallMapv2.sqlite is really present.

If it exists already, it won't do any harm to it.

copyDataBase() should give you some explanations about how it does to copy from assets to ../databases/..

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006664

The assets/ folder has nothing to do with databases, directly. If you put a database in the assets/ folder, you need to copy it from the assets/ folder to where you want the database to reside in the actual filesystem.

Upvotes: 1

Related Questions