hi, i'm trying to access (read & write) from non-default (example 2nd table) table of a sqlite database which i created using sqlite database browser. but somehow, i cant seems to find the way to access it.
could anyone help me ? thanks alot.
============= # my database helper # ============
*ps. te select data method works, but i can only select from table 1 and not table 2
could anyone help me ? thanks alot.
============= # my database helper # ============
Code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.sg/databases/";
private static String DB_NAME = "TestDatabase";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}//constructor
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if(dbExist)
{
//do nothing - database already exist
}//if
else
{
this.getReadableDatabase();
try
{
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}//catch
}//else
}//createDatabase
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch(SQLiteException e) {
//database does't exist yet.
}//catch
if(checkDB != null)
{
checkDB.close();
}//if
return checkDB != null ? true : false;
}//checkDatabase
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
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();
}//copyDatabase
// # open database #
public void openDatabase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}//openDatabase
@Override
public synchronized void close()
{
if(myDatabase != null)
myDatabase.close();
super.close();
}//close
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDatabase.query(....)" so it'd be easy
// to you to create adapters for your views.
public List<String> selectData
(String tableName, String[] columns, String selection, String[] selectionArgs,
String groupBy, String having, String orderBy) {
List<String> list = new ArrayList<String>();
Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}//selectData
}//class
*ps. te select data method works, but i can only select from table 1 and not table 2