0

我正在尝试访问我放入 Android 项目的 res/raw 文件夹中的文件。我正在使用这个 .CSV 文件与CsvJdbc jar 一起使用,它允许我像查询数据库一样查询它。我见过很多用 InputStream 抓取它的例子,例如......

 InputStream raw;
 raw = getResources().openRawResource(R.raw.vehicles);

但我需要通过创建连接来获取文件。IE

 Connection conn = DriverManager.getConnection("jdbc:relique:csv:PathToFolderWhereCsvFileIsGoesHere");

这是完整的代码。

    String[] holder = null;
    try
    {
      // load the driver into memory
      Class.forName("org.relique.jdbc.csv.CsvDriver");


      // create a connection. The first command line parameter is assumed to
      //  be the directory in which the .csv files are held
      Connection conn = DriverManager.getConnection("jdbc:relique:csv:PathToFolderWhereCsvFileIsGoesHere");


      // create a Statement object to execute the query with
      Statement stmt = conn.createStatement();


      // Select the ID and NAME columns from sample.csv
      ResultSet results = stmt.executeQuery("SELECT model FROM vehicles where id = 200");


      // dump out the results
      for(int i=0; i < results.getFetchSize(); i++){
          holder[i] = results.getString("model");
      }


      // clean up
      results.close();
      stmt.close();
      conn.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
4

1 回答 1

0

一种解决方案是编写一个实现 CsvJdbc org.relique.io.TableReader 接口的类,该接口为每个表返回一个 java.io.Reader。CsvJdbc 然后从这个 Reader 对象而不是从文件中读取。

在 CsvJdbc 网页http://csvjdbc.sourceforge.net上有一个例子

于 2013-10-20T17:50:46.500 回答