用黑莓JDE安装SqLite

时间:2011-11-18 11:24:11

标签: sqlite blackberry jde

我已经安装了Blackberry JDE和Eclipse。我想为我的应用程序创建一个SqLite数据库。我应该单独安装SqLite还是配备BlackBerry JDE?

1 个答案:

答案 0 :(得分:2)

没有什么可以单独安装sqlite数据库。您必须以编程方式创建它并且必须使用它。使用它。

在SD卡中创建数据库:

Public void CreateDatabase()
{
Database d;

try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db"); 
d = DatabaseFactory.create(myURI);
d.close();
add(new RichTextField("DB created successfully"));
}
catch ( Exception e ) 
{ 
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}
}

在数据库中创建表:

Public void CreateTable()
{
Database d;
try
{
URI myURI = URI.create("file:///SDCard/Databases/"+"Test.db"); 
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement( "CREATE TABLE 'testtable' ( " +"'id' INTEGER, " +"'name' TEXT ");

st.prepare();
st.execute();
st.close();
d.close();
add(new RichTextField("Table created successfully"));
}
catch ( Exception e ) 
{ 
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}

}

在表格中插入数据:

Public void InsertData ()
{
Database d;
try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db"); 
d = DatabaseFactory.open(myURI);

Statement st = d.createStatement("INSERT INTO testtable(id,name) " + "VALUES (1,’Arun’)");
st.prepare();
st.execute();
st.close();
d.close();
add(new RichTextField("Values Inserted"));
}
catch ( Exception e ) 
{ 
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}

}

从数据库中检索值

Public void RetriveValues()
{
Database d;

try
{
URI myURI = URI.create("file:///SDCard/Databases/" +"Test.db"); 
d = DatabaseFactory.open(myURI);
Statement st = d.createStatement("SELECT id,name FROM testtable");
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 + ". ID = " + r.getInteger(0)
+ " , " 
+". Name = " + r.getString(1)));
}
if (i==0)
{
add(new RichTextField("No data in the table."));
}
st.close();
d.close();
}
catch ( Exception e ) 
{ 
System.out.println( e.getMessage() );
e.printStackTrace();
add(new RichTextField("Error: "+e.toString()));
}
}