Code sample: Creating a parameterized update

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.database.*;
import net.rim.device.api.io.*;
import java.util.*;

public class ParameterizedUpdate extends UiApplication
{
    public static void main(String[] args)
    {
        ParameterizedUpdate theApp = new ParameterizedUpdate();
        theApp.enterEventDispatcher();
    }
    public ParameterizedUpdate()
    {
        pushScreen(new ParameterizedUpdateScreen());
    }
    }
    class ParameterizedUpdateScreen extends MainScreen
    {
        Database d;
        public ParameterizedUpdateScreen()
        {
            LabelField title = new LabelField("SQLite Parameterized Update Sample",
            LabelField.ELLIPSIS |
            LabelField.USE_ALL_WIDTH);
            setTitle(title);
            
            add(new RichTextField("Attempting to update data in " +
              "MyTestDatabase.db on the SDCard."));
            try
            {
               URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                 "MyTestDatabase.db");
               d = DatabaseFactory.open(myURI);
            
               Statement st = d.createStatement(
                 "UPDATE People SET Age=? WHERE Name=?");
               st.prepare();
            
               Hashtable ht = new Hashtable(2);
               ht.put("Sophie", new Integer(10));
               ht.put("Karen",  new Integer(7));
            
               Enumeration names = ht.keys();
               Enumeration ages  = ht.elements();
            
               while (names.hasMoreElements())
               {
                   Integer iAge   = (Integer)ages.nextElement();
                   String strName = (String)names.nextElement();
                   st.bind(1,iAge.intValue());
                   st.bind(2,strName);
                   st.execute();
                   st.reset();
               }
               st.close();
               d.close();
        }
        catch ( Exception e )
        {
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
    }
}
Previous topic: Using SQL parameters

Was this information helpful? Send us your comments.