-1



In [emulator][DDMS]
When I put database manualy in data/data/mypackage/databases/ it run smooth.

[Android device]

When I export to apk file and install to android device. The apps will force close. when I check in folder path in android devices data/data/myPackage/databases/ there are empty databases in it I dont know where it come from. When I delete that empty database and replace with my existing database. The apps run smooth as feather.

The question is, How to replace database[have data] in asset folder and replace the empty database create itself with database[have data] in asset folder.

Below my coding, If I have mistake please tell me

Thanks who reply me, regards Hafizul Reza

public class MainActivity extends Activity {

ArrayList<OverlayItem> anotherOverlayItemArray;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapView mapView = new MapView(this, 256);
setContentView(R.layout.activity_main);
 mapView.setClickable(true);
 mapView.setMultiTouchControls(true);
 mapView.setBuiltInZoomControls(true);

setContentView(mapView);
mapView.getController().setZoom(4);
mapView.getController().setCenter(new GeoPoint(4.415895,101.383082));
mapView.setUseDataConnection(false);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);

SQLiteDatabase myDB = null;
String TableName = "Landslide";

 myDB = this.openOrCreateDatabase("Landslide.sqlite", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);
 if (c != null) {
if (c.moveToFirst()) {
do {
Double lat = c.getDouble(c.getColumnIndex("Latitude"));
Double lng = c.getDouble(c.getColumnIndex("Longitude"));

anotherOverlayItemArray = new ArrayList<OverlayItem>();
anotherOverlayItemArray.add(new OverlayItem(
                "Ringlet", "Cameron Highland", new GeoPoint(lat, lng)));

ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay 
            = new ItemizedIconOverlay<OverlayItem>(this, anotherOverlayItemArray, null);

mapView.getOverlays().add(anotherItemizedIconOverlay);
} while (c.moveToNext());
}
}

2 Answers2

1

Check these lines myDB = this.openOrCreateDatabase("Landslide.sqlite", MODE_PRIVATE, null); Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);

In first line you are trying to open database. If it is not present it will create a new one and it will return that database instance.(Empty Database) Your next line is confusing. You are making query to database. But I am not seeing any code to add or insert any rows. Can you share more information about that crash?

Suhas K
  • 74
  • 8
  • I use eclipse. so I put database in asset folder and I thought It will automatic copy database in folder data/data/myPackage/databases/ .So now I just want to read my data in my database only not to add or insert database. so what code can i read database direct from asset folder. Thanks for your reply sir – Hafizul Reza Apr 13 '15 at 07:42
  • CHeck this link http://stackoverflow.com/questions/10738623/copy-database-from-assets-folder-in-unrooted-device – Suhas K Apr 13 '15 at 07:47
  • sir why this line error private void StoreDatabase() { File DbFile = new File >> The error is storeDatabases and DbFile. Did I miss something? Sir can u edit my above code and add reference that u give in link. Im blur now – Hafizul Reza Apr 13 '15 at 07:51
  • I guess you have not seen answer in that link. Use copyDataBase() method as mentioned in answer – Suhas K Apr 13 '15 at 07:55
  • InputStream myInput =myContext.getAssets().open(dbname); I have problem in this line sir. I got error for myContext . Thanks sir. Sorry for ask many questions – Hafizul Reza Apr 13 '15 at 08:14
  • I Hope you have solved the problem which you were facing. No problem. You can ask questions. Its always better to run your application in debug mode. It will help you in troubleshooting problems most of the time. – Suhas K Apr 13 '15 at 12:32
0

Use this code to copy database from your asset folder to your app folder :

private void copy(String file, String folder) throws IOException {
    File CheckDirectory;
    CheckDirectory = new File(folder);

    String parentPath = CheckDirectory.getParent();
    File filedir = new File(parentPath);
    if (!filedir.exists()) {
        if (!filedir.mkdirs()) {
            return;
        }
    }           
    InputStream in = this.getApplicationContext().getAssets().open(file);
    File newfile = new File(folder);
    OutputStream out = new FileOutputStream(newfile);

    byte[] buf = new byte[1024];
    int len; while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close(); out.close();
}

file is your db name and folder is where you want copy it

Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41