tags:

views:

113

answers:

2

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;

}}
A: 

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.

CommonsWare
Thanks very much. So if i copy the database to sdcard. Then field path also is /data/data/com.SGMalls/databases/mallMapv2.sqlite"?Thanks you
I mean i just set the copy path is :/data/data/com.SGMalls/databases/mallMapv2.sqlite"Then this file is store in where? embed in this app?I am sorry. I am a newbie about android development.
+1  A: 

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/..

ccheneson