개발 가이드

Local Navigation

데이터 업데이트

테이블의 데이터를 업데이트하려면 UPDATE 문을 실행합니다.

다음 코드 샘플은 데이터를 업데이트하는 준비된 문을 만듭니다. execute 메소드를 사용하여 문을 실행합니다.

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 UpdateData extends UiApplication
{
    public static void main(String[] args)
    {
        UpdateData theApp = new UpdateData();
        theApp.enterEventDispatcher();
    }
    public UpdateData()
    {
        pushScreen(new UpdateDataScreen());
    }   
}
class UpdateDataScreen extends MainScreen
{
    Database d;
    public UpdateDataScreen()
    {
       LabelField title = new LabelField("SQLite Update Data Sample",
                                          LabelField.ELLIPSIS | 
                                          LabelField.USE_ALL_WIDTH);
       setTitle(title);
       add(new RichTextField("Trying to update data in MyTestDatabase.db."));
       try
       {
        URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                               "MyTestDatabase.db"); 
        d = DatabaseFactory.open(myURI);
        Statement st = d.createStatement("UPDATE People SET Age=38 " +
                                         "WHERE Name='John'");
        st.prepare();
        st.execute();
        st.close();
        d.close();
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
    }
}

다음 코드 조각은 런타임 브리지를 통해 호출 수를 줄이는 대량 작업 메소드를 사용합니다. Statement.executeUpdate 메소드를 사용하여 매개 변수를 연결하고, 문을 실행하고, 연결을 재설정합니다.

Statement st = d.createStatement("UPDATE Account set Balance = ? WHERE AcctNo > ?");
try
{
     st.prepare();
     Object[] bindParams = {new Integer (2000), new Integer (100)};
     st.executeUpdate(bindParams);
}
finally 
{
     st.close();
}

코드 샘플: 테이블 데이터 삭제

다음 코드 샘플은 DELETE 문을 나타냅니다.

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();
        }
    }
}

코드 샘플: 데이터베이스 테이블 나열

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 ListTables extends UiApplication
{
    public static void main(String[] args)
    {
        ListTables theApp = new ListTables();
        theApp.enterEventDispatcher();
    }
    public ListTables()
    {
        pushScreen(new ListTablesScreen());
    }   
}
class ListTablesScreen extends MainScreen
{
    Database d;
    public ListTablesScreen()
    {
        LabelField title = new LabelField("SQLite List Database Tables", 
                                           LabelField.ELLIPSIS | 
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Attempting to list tables 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("SELECT name FROM " +
                                             " sqlite_master " +
                                             "WHERE type='table'" +
                                             "ORDER BY name");
            st.prepare();
            Cursor c = st.getCursor();
            Row r;
            int i = 0;
            while(c.next()) 
            {
                r = c.getRow();
                i++;
                add(new RichTextField(i + ". Table: " + r.getString(0)));
            }
            if (i==0)
            {
                add(new RichTextField("There are no tables " +
                                      " in the MyTestDatabase database."));
            }
            st.close();
            d.close();
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
    }
}

SQLite 데이터베이스 삭제

BlackBerry 스마트폰에서 프로그램을 제거하면 해당 프로그램과 연결된 영구 데이터베이스가 자동으로 삭제됩니다. 해당 데이터베이스는 DatabaseFactory.delete() 메소드를 사용하여 삭제해야 합니다.

코드 샘플: SQLite 데이터베이스 삭제

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 DeleteDatabase extends UiApplication
{
    public static void main(String[] args)
    {
        DeleteDatabase theApp = new DeleteDatabase();
        theApp.enterEventDispatcher();
    }
    public DeleteDatabase()
    {
        pushScreen(new DeleteDatabaseScreen());
    }   
}
class DeleteDatabaseScreen extends MainScreen
{
    Database d;
    public DeleteDatabaseScreen()
    {
        LabelField title = new LabelField("SQLite Delete Database Sample",
                                           LabelField.ELLIPSIS | 
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Deleting a database called " +
                              "MyTestDatabase.db on the SDCard."));
        try
        {
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db"); 
            DatabaseFactory.delete(myURI);       
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
    }
}

이 정보가 도움이 되었습니까? 의견을 보내 주십시오.