• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to access the non-default table of a sqlite database ?

vam92

Lurker
Nov 14, 2011
7
1
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. :D

============= # 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
 
in this line here
Code:
Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);

you are sending the cursor your table name in the tableName string, that's how you chose which table the cursor is reading from

send the proper table name as a parameter to the method selectData() to chose which table to read from

also thank you for fixing your post [and not starting a new thread or double posting like some other ppl]
and welcome to android forums :D
 
  • Like
Reactions: vam92
Upvote 0
in this line here
Code:
Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
you are sending the cursor your table name in the tableName string, that's how you chose which table the cursor is reading from

send the proper table name as a parameter to the method selectData() to chose which table to read from

also thank you for fixing your post [and not starting a new thread or double posting like some other ppl]
and welcome to android forums :D

heh.. i tried that before but failed.. i tried again just now and realize that i did not uninstall from my emulator so the database it read is the old one (with only 1 tables). thanks for helping. you helped to confirm that my code is right and thus allows me to find out the reason. thanks alot. :D
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones