【问题标题】:Execute Multi line SQLite query执行多行 SQLite 查询
【发布时间】:2014-03-30 13:47:54
【问题描述】:

我正在尝试从 Internet 下载一个 .txt 文件,然后阅读并执行它。 我成功下载、读取和执行单行查询文件。 但问题是我无法从文件中执行多行查询。我做了一些研究,但它们不适合我的需求,所以我想出了一个解决方案,但我无法找出一些错误。 我的想法是将 .txt 文件逐行转换为列表,然后为列表中的每个字符串在 sqlite 中执行查询。 这是我的一些代码:

    public void updateDB(View v){

    new DownloadFileFromURL().execute(file_url);

    File file = new File(Environment
            .getExternalStorageDirectory().toString()
            + "/update.txt")

    Log.i("UPDATE", "SQL DOWNLOADED");

    while(start.equals(true)){

        List<String> list = readUpdate2("update.txt");
        for (String string : list) {
            datasource.executeRaw(string);
            Log.i("UPDATE", "EXEC SQL # : " + list.size());
        }

    file.delete();}}

public List<String> readUpdate2(String url){
    File sdcard = Environment.getExternalStorageDirectory();
     //Get the text file
    List<String> list = null;
      File file = new File(sdcard,url);

      try {

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

          while((line = br.readLine()) != null) {
                      // this is where the error happens :
              list.add(line);
              Log.i("UPDATE", "LINE : " + line);
          }
      }
      catch (IOException e) {
      }
      return list;
}

问题是我遇到了 Nullpointer 异常。它说该行是空的。 这是日志:

03-30 18:01:18.965: E/AndroidRuntime(12474): FATAL EXCEPTION: main
03-30 18:01:18.965: E/AndroidRuntime(12474): java.lang.IllegalStateException: Could not execute method of the activity
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.view.View$1.onClick(View.java:3838)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.view.View.performClick(View.java:4475)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.view.View$PerformClick.run(View.java:18786)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.os.Handler.handleCallback(Handler.java:730)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.os.Looper.loop(Looper.java:137)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.app.ActivityThread.main(ActivityThread.java:5493)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at dalvik.system.NativeStart.main(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.reflect.InvocationTargetException
03-30 18:01:18.965: E/AndroidRuntime(12474):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at android.view.View$1.onClick(View.java:3833)
03-30 18:01:18.965: E/AndroidRuntime(12474):    ... 11 more
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.NullPointerException
03-30 18:01:18.965: E/AndroidRuntime(12474):    at com.meemarbashi.meemardictionary.UpdateActivity.readUpdate2(UpdateActivity.java:292)
03-30 18:01:18.965: E/AndroidRuntime(12474):    at com.meemarbashi.meemardictionary.UpdateActivity.updateDB(UpdateActivity.java:223)
03-30 18:01:18.965: E/AndroidRuntime(12474):    ... 14 more

所以基本上我没有从 .txt 文件中获得任何文本,然后我无法将任何内容添加到我的列表中。我该怎么办?

提前致谢

【问题讨论】:

    标签: java android sql arrays sqlite


    【解决方案1】:

    这可能是空指针异常的原因:

    List<String> list = null;
    

    尝试分配一个新列表而不是 null

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 2012-07-14
      • 1970-01-01
      • 2012-04-02
      相关资源
      最近更新 更多