List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Thursday, June 27, 2013

Creating a persistent store in Blackberry

To create a persistent store, you create at least one PersistentObject. Each PersistentObject has a unique key of type long.

Creating Store - 
static PersistentObject store;
static {
store = PersistentStore.getPersistentObject( 0xa1a569278238dad2L );
}
 
Insert datas to store -  
String[] userinfo = {username, password};
synchronized(store) {
store.setContents(userinfo); 
store.commit();
} 

Retrive datas from store -
synchronized(store) {
String[] currentinfo = (String[])store.getContents(); 
if(currentinfo == null) {
Dialog.alert(_resources.getString(APP_ERROR));
} 
else {
currentusernamefield.setText(currentinfo[0]);
currentpasswordfield.setText(currentinfo[1]);
}
}

Creating Folder on SD Card in Blackberry

The File Connection API is implemented in the javax.microedition.io.file package.
Internal Storage - FileConnection fc = (FileConnection)Connector.open("file:///Store")
External Storage - FileConnection fc = (FileConnection)Connector.open("file:///SDCard")

Creating a folder-

try 
        {    
             FileConnection fc = (FileConnection)Connector.open(
"file:///SDCard/myfolder/");
             if (!fc.exists())
             {
                 fc.mkdir(); 
             }
             fc.close();
         }
         catch (IOException ioe) 
         {
            System.out.println(ioe.getMessage() );
         }
 

Creating a file - 

 try 
      {
          FileConnection fc = 
               (FileConnection)Connector.open(
"file:///store/home/user/newfile.txt");
          if (!fc.exists())
          {
              fc.create();  
          }
          fc.close();
       }
       catch (IOException ioe) 
       {
          System.out.println(ioe.getMessage() );
       }
 

Writing text to a file - 

try 
    {
      FileConnection fc = (FileConnection)Connector.open(
"file:///store/home/user/newfile.txt"); 
      if (!fc.exists())
      {
          fc.create();  
      }
      OutputStream outStream = fc.openOutputStream(); 
      outStream.write("test content".getBytes());
      outStream.close();
      fc.close();
     }
     catch (IOException ioe) 
     {
        System.out.println(ioe.getMessage() );
     }
 

SQLite database in Blackberry

Creating a SQLite database-

     try
       {
           URI myURI = URI.create("file:///SDCard/Databases/" + 
                                  "MyDatabase.db"); 
           d = DatabaseFactory.create(myURI);
           d.close();
       }
       catch ( Exception e ) 
       {         
           System.out.println( e.getMessage() );
           e.printStackTrace();
       }

 Creating Table - 

try
        {
            URI myURI = URI.create("/SDCard/Databases/" +
                                   "MyDatabase.db"); 
            d = DatabaseFactory.open(myURI);
            Statement st = d.createStatement( "CREATE TABLE 'MyTable' ( " +
                                              "'Name' TEXT, " +
                                              "'Number' INTEGER )" );
            
            st.prepare();
            st.execute();
            st.close();
            d.close();
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
 

Insert Data - 

try
        {
            URI myURI = URI.create("file:///SDCard/Databases/" +
                                   "MyDatabase.db"); 
            d = DatabaseFactory.open(myURI);
            
            Statement st = d.createStatement("INSERT INTO MyTable(Name,Number) " +
                                             "VALUES ('abc',123)");
            st.prepare();
            st.execute();
            st.close();
            d.close();

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

 Retrive data - 

try
        {
            URI myURI = URI.create("file:///SDCard/Databases/" +
                                   "MyDatabase.db"); 
            d = DatabaseFactory.open(myURI);
            
            Statement st = d.createStatement("SELECT Name,Number FROM MyTable");

            st.prepare();
            net.rim.device.api.database.Cursor c = st.getCursor();
            
            Row r;
            int i = 0;
            while(c.next()) 
            {
                r = c.getRow();
                i++;
                add(new RichTextField(i + ". Name = " + r.getString(0) +
                                          " , " +
                                          "Number= " + r.getInteger(1)));
            }
            if (i==0)
            {
                add(new RichTextField("No data in the MyTable table."));
            }
            st.close();
            d.close();

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

Delete Table - 

 try
        {
            URI myURI = URI.create("file:///SDCard/Databases/" +
                                   "MyDatabase.db"); 
            d = DatabaseFactory.open(myURI);
            
            Statement st = d.createStatement("DELETE MyTable");
            st.prepare();
            st.execute();
            st.close();
            d.close();

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

 Update Table - 

 try
       {
        URI myURI = URI.create("file:///SDCard/Databases/" +
                               "MyDatabase.db"); 
        d = DatabaseFactory.open(myURI);
            
        Statement st = d.createStatement("UPDATE MyTable SET Number=1" +
                                         "WHERE Name='abc'");
        st.prepare();
        st.execute();
        st.close();
        d.close();

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

 Delete Database - 

 try
        {
            URI myURI = URI.create("file:///SDCard/Databases/" +
                                   "MyDatabase.db"); 
            DatabaseFactory.delete(myURI);       
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }

 

Storing data in Blackberry

There are several ways you can store, share, and manage data for your apps:
Data storage approach Description and API
SQLite database Store data in relational databases with the Database API.
File system Store data in files and folders with the FileConnection API.
Persistent store Save objects across smartphone restarts with the PersistentStore API.
Runtime store Save objects nonpersistently, which is useful for sharing data between applications and creating system-wide singletons, with the RuntimeStore API.
Record store Store data in the MIDP Record Management System with the RMS API.