2011-11-18 110 views
0

我安裝了黑莓JDE和Eclipse。我想爲我的應用程序創建一個SqLite數據庫。我應該單獨安裝SqLite還是附帶BlackBerry JDE?用黑莓JDE安裝SqLite

回答

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

感謝您詳細的答覆 – vks

+0

@vks:歡迎您 –

+0

嗨阿倫·庫馬爾,當我做上面的代碼中,我得到了錯誤:無效的路徑名。路徑不包含正確的根目錄。有關詳細信息,請參閱FileSystemRegistry類。你知道什麼是問題?我將SD卡安裝到我的系統上的一個文件夾中 – vks