Code sample: Deleting table data

The following code sample illustrates the DELETE statement.

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.*;

public class DeleteData extends UiApplication
{
    public static void main(String[] args)
    {
        DeleteData theApp = new DeleteData();
        theApp.enterEventDispatcher();
    }
    
    public DeleteData()
    {
        pushScreen(new DeleteDataScreen());
    }   
}

class DeleteDataScreen extends MainScreen
{
    Database d;
    public DeleteDataScreen()
    {
        LabelField title = new LabelField("SQLite Delete Database Data",
                                           LabelField.ELLIPSIS |
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Attempting to delete data from " + 
                              "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("DELETE FROM People");
            st.prepare();
            st.execute();
            st.close();
            d.close();

        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
             
    }
}

Was this information helpful? Send us your comments.