title: Codename One: Connecting and populating a drop-down menu with an SQLite database
body: <p>I am trying to connect an SQLite database file to a picker component (accepting strings). This should act similar to a drop-down menu. I have tried to follow previous advice and examples, but without success.</p>
  
  <p>As indicated in a previous post, I have saved the database file in the source folder of the application. <a href="http://i.stack.imgur.com/adkIY.jpg" rel="nofollow">View of the source folder where I have saved the database file (highlighted).</a></p>
  
  <p>The code I have used to implement my app is as follows with the below layout.</p>
  
  <p>//-----------------------</p>
  
  <p>database code</p>
  
  <p>//-----------------------</p>
  
  <pre><code>public class MyApplication {
  
  private Form current;
  private Resources theme;
  
  public void init(Object context) {
      theme = UIManager.initFirstTheme("/theme");
  
      // Pro only feature, uncomment if you have a pro subscription
      // Log.bindCrashProtection(true);
  }
  
  private Container Home() {
      Container home = new Container(new BoxLayout(BoxLayout.Y_AXIS));
      return home;
  }
  
  private Container AddItem() {
      Container addItem = new Container(new BoxLayout(BoxLayout.Y_AXIS));
  
      TextArea item = new TextArea("Add Item");
      addItem.addComponent(item);
      Picker selectItem = new Picker();
      selectItem.setType(Display.PICKER_TYPE_STRINGS);
  
  //----------------------------------------------------------------------------------        
  
  Database db = null;
  Cursor cur = null;
  try {
      db = Display.getInstance().openOrCreate("FoodAndBeverage.db");
      if(selectItem.getText().startsWith("Still Water")) {
          cur = db.executeQuery(selectItem.getText());
          int columns = cur.getColumnCount();
          addItem.removeAll();
          if(columns &gt; 0) {
              boolean next = cur.next();
              if(next) {
                  ArrayList&lt;String[]&gt; data = new ArrayList&lt;&gt;();
                  String[] columnNames = new String[columns];
                  for(int iter = 0 ; iter &lt; columns ; iter++) {
                      columnNames[iter] = cur.getColumnName(iter);
                  }
                  while(next) {
                      Row currentRow = cur.getRow();
                      String[] currentRowArray = new String[columns];
                      for(int iter = 0 ; iter &lt; columns ; iter++) {
                          currentRowArray[iter] = currentRow.getString(iter);
                      }
                      data.add(currentRowArray);
                      next = cur.next();
                  }
                  Object[][] arr = new Object[data.size()][];
                  data.toArray(arr);
                  addItem.add(BorderLayout.CENTER, new Table(new DefaultTableModel(columnNames, arr)));
              } else {
                  addItem.add(BorderLayout.CENTER, "Query returned no results");
              }
          } else {
              addItem.add(BorderLayout.CENTER, "Query returned no results");
          }
      } else {
          db.execute(selectItem.getText());
          addItem.add(BorderLayout.CENTER, "Query completed successfully");
      }
      addItem.revalidate();
  } catch(IOException err) {
      Log.e(err);
      addItem.removeAll();
      addItem.add(BorderLayout.CENTER, "Error: " + err);
      addItem.revalidate();
  } finally {
      Util.cleanup(db);
      Util.cleanup(cur);
  }
  
  //---------------------------------------------------------------------------------------------
  
      addItem.addComponent(selectItem);
      TextField quantity = new TextField("", "Quantity (ml or g)", 4, TextArea.NUMERIC);
      addItem.addComponent(quantity);
      Button add = new Button("Add");
      addItem.addComponent(add);
      TextArea results = new TextArea("Results");
      addItem.addComponent(results);
      return addItem;
  }
  
  private Container Settings() {
      Container settings = new Container(new BoxLayout(BoxLayout.Y_AXIS));     
      TextArea nutrients = new TextArea("Target");
      settings.addComponent(nutrients);
      TextField volume = new TextField("", "Volume (ml)", 4, TextArea.NUMERIC);
      settings.addComponent(volume);
      TextArea duration = new TextArea("Hydration Duration");
      settings.addComponent(duration);
      settings.add("Start:");
      Picker start = new Picker();
      start.setType(Display.PICKER_TYPE_TIME);
      settings.addComponent(start);
      settings.add("End:");
      Picker end = new Picker();
      end.setType(Display.PICKER_TYPE_TIME);
      settings.addComponent(end);       
      Button save = new Button("Save");
      settings.addComponent(save);
      return settings;
  }
  
  public void start() {
      if(current != null)
      {
        current.show();
        return;
      }
  
      Form home = new Form("Hydrate", new BorderLayout());
      Tabs t = new Tabs();
      t.addTab("Home", Home());
      t.addTab("Intake", AddItem());
      t.addTab("Settings", Settings());
      home.add(BorderLayout.NORTH, t);
      home.show();
  
  }
  
  public void stop() {
      current = Display.getInstance().getCurrent();
  }
  
  public void destroy() {
  }
  
  }
  </code></pre>
  
  <p>I would therefore appreciate any advice and guidance on exactly where I am going wrong and how to implement the suggested changes in my code.</p>