首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黑莓和sqlite

问黑莓和sqlite
EN

Stack Overflow用户
提问于 2010-07-27 06:01:05
回答 2查看 3.3K关注 0票数 0

当我尝试在黑莓EclipsePlugin 1.1中运行下面的代码时

我得到了

net.rim.device.api.database.DatabaseIOException:文件系统错误(12),我的Sqlite在项目的资源文件夹中。

我在模拟器中添加了SDCard

因此,请帮助我解决这个错误,我也不能复制现有的DB到SD卡。

代码语言:javascript
复制
package com.bb.readdb;

/*
* ReadData.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;

import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseException;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.DatabaseSecurityOptions;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.io.URI;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.system.CodeSigningKey;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public class Readdbdata extends UiApplication
{
    //public String nydb="nycway232128.db";
public static void main(String[] args)
{
Readdbdata theApp = new Readdbdata();
theApp.enterEventDispatcher();
}
public Readdbdata()
{
pushScreen(new ReadDataScreen());
}
}
class ReadDataScreen extends MainScreen
{
Database d;
public ReadDataScreen()
{
LabelField title = new LabelField("SQLite Read Table Data Sample",
LabelField.ELLIPSIS |
LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Attempting to retrieve data from " +"Mypeople.db on the SDCard."));
try
{
    String nydb="nycway232128.db";
URI myURI = URI.create("file:///SDCard/databases/NycWay/" +"Mypeople.db");
d = DatabaseFactory.openOrCreate(myURI);
//Statement st =  d.createStatement("SELECT Name FROM gross ");


//st.prepare();
//st.execute();
//st.close();
//d.close();
boolean sdCardPresent = false;
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
copyFile("Mypeople", "file:///SDCard/databases/NycWay/" );

DatabaseFactory.open(myURI);
Statement st =  d.createStatement("SELECT Name FROM People ");
st.prepare();
st.close();

d.close();
}
catch ( Exception e )
{
System.out.println( "@@@@@@@@@NYC IKnwdf pre@@@@@@@@@@@@"+e.toString() );
e.printStackTrace();
}

}
public  void copyFile(String srFile, String dtFile)
{
    try
    {

        FileConnection fconn;


        fconn = (FileConnection) Connector.open(dtFile,Connector.READ_WRITE);

        if(!fconn.exists()) // if  file does not exists , create a new one
        {
            fconn.create();
        }
        InputStream is = getClass().getResourceAsStream(srFile);
        OutputStream os =fconn.openOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0)
        {
            os.write(buf, 0, len);
        }
       is.close();
       os.close();
    }
    catch(IOException e)
    {

    }
}
public void readAndWriteDatabaseFile(FileConnection fileConnection) throws IOException
{
    OutputStream outputStream = null;
    InputStream inputStream = null;       

    // Open an input stream to the pre-defined encrypted database bundled
    // within this module.
    String nydb="Mypeople";
    {        
        /*\*/
    inputStream = getClass().getResourceAsStream("/" + nydb);

    // Open an output stream to cthe newly created file
    outputStream = (OutputStream)fileConnection.openOutputStream();                                       

    // Read data from the input stream and write the data to the
    // output stream.            
    byte[] data = new byte[256];
    int length = 0;
    while (-1 != (length = inputStream.read(data)))
    {
        outputStream.write(data, 0, length);                
    }     

    // Close the connections
    if(fileConnection != null)
    {
        fileConnection.close();
    }
    if(outputStream != null)
    {
        outputStream.close();
    }
    if(inputStream != null)
    {
        inputStream.close();
    }            
}

}


}
EN

回答 2

Stack Overflow用户

发布于 2010-08-24 18:27:06

您必须确保关闭了文件的所有输入/输出流,还必须在调用DatabaseFactory.open()之前关闭文件;

希望能帮上忙

票数 2
EN

Stack Overflow用户

发布于 2011-11-29 18:13:45

如果您希望同时以读写和只读方式打开数据库,请先以读-写方式打开它。试图在数据库已打开时(作为读-写或只读)以读-写方式打开数据库,将生成“文件系统错误12",这表示有人试图打开到同一数据库的多个读-写连接。

找到了这个描述这里。我希望它能帮你解决问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3341101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档