1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by vam92, Nov 14, 2011.

  1. vam92

    vam92 Lurker
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    7
    Joined:
    Nov 14, 2011

    Nov 14, 2011
    7
    1
    26
    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 (Text):
    1.  
    2. public class DatabaseHelper extends SQLiteOpenHelper {
    3.  
    4.     private static String DB_PATH = "/data/data/com.sg/databases/";
    5.  
    6.     private static String DB_NAME = "TestDatabase";
    7.  
    8.     private SQLiteDatabase myDatabase;
    9.  
    10.     private final Context myContext;
    11.  
    12.     public DatabaseHelper(Context context) {
    13.  
    14.         super(context, DB_NAME, null, 1);
    15.         this.myContext = context;
    16.     }//constructor    
    17.  
    18.     public void createDatabase() throws IOException {
    19.  
    20.         boolean dbExist = checkDatabase();
    21.  
    22.         if(dbExist)
    23.         {
    24.             //do nothing - database already exist
    25.         }//if
    26.        
    27.         else
    28.         {
    29.             this.getReadableDatabase();
    30.  
    31.             try
    32.             {
    33.                 copyDatabase();
    34.  
    35.             } catch (IOException e) {
    36.  
    37.                 throw new Error("Error copying database");
    38.  
    39.             }//catch
    40.         }//else
    41.  
    42.     }//createDatabase
    43.  
    44.     private boolean checkDatabase() {
    45.  
    46.         SQLiteDatabase checkDB = null;
    47.  
    48.         try
    49.         {
    50.             String myPath = DB_PATH + DB_NAME;
    51.             checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    52.  
    53.         } catch(SQLiteException e) {
    54.  
    55.             //database does't exist yet.
    56.  
    57.         }//catch
    58.  
    59.         if(checkDB != null)
    60.         {
    61.             checkDB.close();
    62.  
    63.         }//if
    64.  
    65.         return checkDB != null ? true : false;
    66.        
    67.     }//checkDatabase
    68.  
    69.     private void copyDatabase() throws IOException {
    70.  
    71.         //Open your local db as the input stream
    72.         InputStream myInput = myContext.getAssets().open(DB_NAME);
    73.  
    74.         // Path to the just created empty db
    75.         String outFileName = DB_PATH + DB_NAME;
    76.  
    77.         //Open the empty db as the output stream
    78.         OutputStream myOutput = new FileOutputStream(outFileName);
    79.  
    80.         //transfer bytes from the inputfile to the outputfile
    81.         byte[] buffer = new byte[1024];
    82.         int length;
    83.        
    84.         while ((length = myInput.read(buffer))>0)
    85.         {
    86.             myOutput.write(buffer, 0, length);
    87.         }
    88.  
    89.         //Close the streams
    90.         myOutput.flush();
    91.         myOutput.close();
    92.         myInput.close();
    93.  
    94.     }//copyDatabase
    95.  
    96.     // # open database #
    97.     public void openDatabase() throws SQLException {
    98.  
    99.         //Open the database
    100.         String myPath = DB_PATH + DB_NAME;
    101.         myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    102.  
    103.     }//openDatabase
    104.  
    105.     @Override
    106.     public synchronized void close()
    107.     {
    108.         if(myDatabase != null)
    109.             myDatabase.close();
    110.  
    111.         super.close();
    112.  
    113.     }//close
    114.  
    115.     @Override
    116.     public void onCreate(SQLiteDatabase db) {
    117.  
    118.     }
    119.  
    120.     @Override
    121.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    122.  
    123.     }
    124.  
    125.    // Add your public helper methods to access and get content from the database.
    126.    // You could return cursors by doing "return myDatabase.query(....)" so it'd be easy
    127.    // to you to create adapters for your views.
    128.    
    129.     public List<String> selectData
    130.         (String tableName, String[] columns, String selection, String[] selectionArgs,
    131.                    String groupBy, String having, String orderBy) {
    132.        
    133.         List<String> list = new ArrayList<String>();
    134.          
    135.         Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
    136.          
    137.         if (cursor.moveToFirst())
    138.         {
    139.             do
    140.             {
    141.                 list.add(cursor.getString(0));
    142.             }
    143.            
    144.             while (cursor.moveToNext());
    145.         }
    146.        
    147.         if (cursor != null && !cursor.isClosed())
    148.         {
    149.             cursor.close();
    150.         }
    151.         return list;
    152.        
    153.     }//selectData
    154.  
    155. }//class
    156.  
    *ps. te select data method works, but i can only select from table 1 and not table 2
     

    Advertisement

  2. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    I'd like to help you , but your code is so annoying to read like this >.<
    please use
    Code (Text):
    1.  tags >_<
     
  3. vam92

    vam92 Lurker
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    7
    Joined:
    Nov 14, 2011

    Nov 14, 2011
    7
    1
    26
     
  4. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    in this line here
    Code (Text):
    1.  
    2. Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
    3.  
    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
     
    vam92 likes this.
  5. vam92

    vam92 Lurker
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    7
    Joined:
    Nov 14, 2011

    Nov 14, 2011
    7
    1
    26
    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
     

Share This Page

Loading...