Hi I have one question related to database
in my project i need to make database at specific folder so can any buddy tell me where i am wrong
this class creates database in /data/data/package_folder/database_name
I want to change it to sdcard/database_folder/database_name
please check my below code
package com.example.qrcode.data;
public class LocalDatabase extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
// database name
public static final String DATABASE_NAME = "qrcode_database";
// table name
public static final String TABLE_NAME = "scan_result";
// table field name
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "scan_url";
public static final String SAVE_TABLE_NAME = "DEVICE";
public static final String SAVE_COLUMN_ID = "ID";
public static final String SAVE_COLUMN_NAME = "DEVICE_ID";
public static final String SAVE_COLUMN_KEY = "KEY";
public static final String SAVE_COLUMN_VALUE = "VALUE";
public static final String CREATE_SCAN_TABLE = "CREATE TABLE" + " "
+ TABLE_NAME + "(" + COLUMN_ID + " "
+ "INTEGER primary key autoincrement," + COLUMN_NAME + " TEXT,"
+ ");";
public static final String CREATE_SAVE_TABLE = "CREATE TABLE" + " "
+ SAVE_TABLE_NAME + "(" + SAVE_COLUMN_ID
+ "INTEGER PRIMERY KEY AUTOINCREMENT" + SAVE_COLUMN_NAME + " TEXT"
+ SAVE_COLUMN_KEY + " TEXT" + SAVE_COLUMN_VALUE + " TEXT" + ");";
public LocalDatabase(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_SCAN_TABLE);
db.execSQL(CREATE_SAVE_TABLE);
} catch (Exception r) {
r.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addname(String name) {
SQLiteDatabase database = this.getWritableDatabase();
Log.i("path ", database.getPath());
ContentValues values = new ContentValues();
// values.put(COLUMN_ID, scan_id);
values.put(COLUMN_NAME, name);
@SuppressWarnings("unused")
Long int1 = database.insert(TABLE_NAME, null, values);
database.close();
}
public void add_device(String data,
ArrayList<HashMap<String, String>> jsonlist) {
SQLiteDatabase database = this.getWritableDatabase();
for (HashMap<String, String> map : jsonlist) {
ContentValues values = new ContentValues();
values.put(SAVE_COLUMN_NAME, data);
values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
@SuppressWarnings("unused")
Long int1 = database.insert(SAVE_TABLE_NAME, null, values);
}
database.close();
/*
* for(HashMap<String, String> map : mylist){ ContentValues cv = new
* ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
* map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
* cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
* cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
* cv); }
*/
}
public List<String> getemp() {
List<String> name_list = new ArrayList<String>();
String selectQuery = "SELECT " + SAVE_COLUMN_NAME + " FROM "
+ SAVE_TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(0);
// Adding contact to list
name_list.add(name);
} while (cursor.moveToNext());
}
return name_list;
}
}
thanks in advance