黑莓开发之数据存储(五)

SQLite

代码示例:创建参数化更新

/*
* ParameterizedUpdate.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
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();
}
}
}

代码示例:删除 SQLite 数据库

/*
* DeleteDatabase.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
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();
}
}
}

代码示例:列出数据库表

/*
* ListTables.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
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();
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 + ". 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();
}
}
}

代码示例:使用事务处理

/*
* UsingTransactions.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
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 UsingTransactions extends UiApplication
{
public static void main(String[] args)
{
UsingTransactions theApp = new UsingTransactions();
theApp.enterEventDispatcher();
}
public UsingTransactions()
{
}
}
class UsingTransactionsScreen extends MainScreen
{
Database d;
public UsingTransactionsScreen()
{
LabelField title = new LabelField("SQLite Using Transactions Sample",
LabelField.ELLIPSIS |LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Trying to update data in a single transaction inMyTestDatabase.db."));
try
{
URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
"MyTestDatabase.db");
d = DatabaseFactory.open(myURI);
d.beginTransaction();
Statement st = d.createStatement("UPDATE People SET Age=7 " +
"WHERE Name='Sophie'");
st.prepare();
st.execute();
st.reset();
st = d.createStatement("UPDATE People SET Age=4 " +
"WHERE Name='Karen'");
st.prepare();
st.execute();
d.commitTransaction();
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
}
}

本文来自Awnlab.com麦芒实验室,转载请注明出处,谢谢合作。