I have a sqlite database, and i put this file in "assets" folder.
The code like below, Pls help and tell what's wrong in this code,
How to use my own sqlite database.
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.SGMalls/databases/";
private static String DB_NAME = "mallMapv2.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
File dbDir = new File(DB_PATH);
if (!dbDir.exists()) {
dbDir.mkdir();
}
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
boolean isnull=false;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
isnull=true;
checkDB.close();
}
return isnull;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class GetData {
private static String DB_PATH = "/data/data/com.SGMalls/databases/mallMapv2.sqlite";
// private static String DB_NAME = "mallMapv2.sqlite";
public static ArrayList<Mall> getMalls() {
ArrayList<Mall> mallsList = new ArrayList<Mall>();
SQLiteDatabase malldatabase = SQLiteDatabase.openDatabase(DB_PATH,
null, SQLiteDatabase.OPEN_READONLY);
String queryString="select id,title from malls order by title";
Cursor cursor=malldatabase.rawQuery(queryString, null);
if(cursor!=null){
cursor.moveToFirst();
while(!cursor.isLast()){
Mall mall=new Mall();
mall.setMallid(cursor.getInt(0));
mall.setMallname(cursor.getString(0));
mallsList.add(mall);
cursor.moveToNext();
}
}
malldatabase.close();
return mallsList;
}
}
The error message:
ERROR/Database(725): sqlite3_open_v2("/data/data/com.SGMalls/databases/
mallMapv2.sqlite", &handle, 1, NULL) failed
03-15 22:34:11.747: ERROR/AndroidRuntime(725): Uncaught handler:
thread main exiting due to uncaught exception
03-15 22:34:11.766: ERROR/AndroidRuntime(725): java.lang.Error: Error
copying database
Thanks very much