2016-04-02 14 views
0

私は自分のデータベースをあらかじめ入力します。だから私はブラウザからSQLクエリを生成し、DBに挿入するコードを書いています。単一行のクエリではうまくいきますが、ほとんどの挿入クエリには複数の行があります。例えば、 `Androidは、挿入クエリごとに複数の行を持つファイルからDbにデータを挿入します

INSERT INTO `poem` VALUES (780,'سلام القلب واشواقه 

من قلب يحب مجنونه 

ياليت هالقلب يحن 


ويقول هالدنيا 

ماتسوى دون *_*','0',NULL);` 

私の要件はそのままです。それは

public static int countLines(InputStream is) throws IOException { 
    try { 
     byte[] c = new byte[1024]; 
     int count = 0; 
     int readChars = 0; 
     boolean empty = true; 
     int index = 0; 

     while ((readChars = is.read(c)) != -1) { 
      empty = false; 
      String temp = new String(c); 
      for (int i = 0; i < readChars; i++) { 

       if (c[i] == ';' && index == 0) { 
        index++; 
       } else if (c[i] == '\n' && index == 1) { 
        index++; 
       } else if (c[i] == '\t' && index == 2) { 
        index++; 
       } else if (c[i] == 'I' && index == 3) { 
        count++; 
        index = 0; 
       } else { 
        i -= index; 
        index = 0; 
       } 
      } 
     } 
     return (count == 0 && !empty) ? 1 : count + 1; 
    } finally { 
     is.close(); 
    } 
} 

(単一行の問合せに対して完璧な作品)私は複数行のために書いた方法であり、誰もがどのようにDBにファイルからこのようなクエリを挿入するためにアイデアを持っていますか。? 助けがあれば助かります。ありがとう

+0

誰でも?助けて??? –

+0

あなたは[アプリケーションと共にDBを発送する]だけではどうですか(http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database)? –

答えて

0

はステップ

により溶液の段階である私は、インサートのクエリが含まれている.sqlファイルを作成します(一部は複数行のクエリを含める)で終わります:。

私のonCreate()の方法では、SQLiteOpenHelperの方法では、getFileLineCount()を使用して私のファイルの行をカウントしています。このメソッドは、私にラインのカウントを返します。この行数は決して使用されませんが、すべての照会が1行であれば、それは有効になるかもしれないからです。この方法はスキップできます。

public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE \"" + TABLE_POEM + "\" (\"" + POEM_LOCAL_ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT , \"" + POEM_TEXT + "\" TEXT,\"" + POEM_ID + "\" INTEGER , \"" + POEM_IS_FAV + "\" TEXT);"); 

      int totalLineCount = getFileLineCount("poem.sql"); 
      int insertCount = insertFromFile(db, "poem.sql", totalLineCount); 

      Log.d("DatabaseHelper", "------------onCreate(SQLiteDatabase db) >>>>>>> completed--------"); 
     } 
    private int getFileLineCount(String assetFilePath) { 
     InputStream insertStream = null; 
     int totalCount = 0; 
     try { 
      insertStream = context.getAssets().open(assetFilePath); 
      totalCount = FileUtil.countLines(new BufferedInputStream(insertStream)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (insertStream != null) { 
       try { 
        insertStream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return totalCount; 
    } 

この方法を使用すると、ファイルからクエリを挿入します。これは、シングルラインクエリとマルチラインクエリの両方を処理します。まず、行に";"が含まれているかどうかをチェックします。かなりシンプルだったのは最後でした。

while ((currentLine = insertReader.readLine()) != null) { 
          if (currentLine.length() != 0) { 
           if (currentLine.charAt(currentLine.length() - 1) == ';') { 
            insertInDb(db, insertStr + "\n" + currentLine); 
            insertStr = ""; 
           } else { 
            insertStr += currentLine; 
           } 
          } 
         } 




private int insertFromFile(SQLiteDatabase db, String assetFilePath, int totalCount) { 
      int result = 0; 
      BufferedReader insertReader = null; 
      try { 
       InputStream insertStream = context.getAssets().open(assetFilePath); 
       insertReader = new BufferedReader(new InputStreamReader(insertStream)); 

       db.beginTransaction(); 

       while (insertReader.ready()) { 

        // String insertStr = insertReader.toString(); 

        String insertStr = ""; 
        String currentLine; 
        while ((currentLine = insertReader.readLine()) != null) { 
         if (currentLine.length() != 0) { 
          if (currentLine.charAt(currentLine.length() - 1) == ';') { 
           insertInDb(db, insertStr + "\n" + currentLine); 
           insertStr = ""; 
          } else { 
           insertStr += currentLine; 
          } 
         } 
        } 

       } 
       db.setTransactionSuccessful(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       if (insertReader != null) { 
        try { 
         insertReader.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       db.endTransaction(); 
      } 

      return result; 
     } 

     private void insertInDb(SQLiteDatabase db, String assetFilePath) throws IOException { 
      if (assetFilePath != null && assetFilePath.length() > 0) { 
       SQLiteStatement statement = db.compileStatement(assetFilePath); 
       statement.execute(); 

      } 

     } 

このテクニックを使用して4000個のレコードをdbに挿入しました。そして、それは遅れのほとんどなくうまくいきました。誰かがそれのためのバッターの解決策を持っている場合。親切に投稿してください。

0

あなたが求めているのは物事を行う醜い方法です。あらかじめ用意されたデータベースとテーブルを作成するにはsqliteassethelperを使用してください。これはSQLiteOpenHelperと非常によく似ていますので、非常に使いやすいです。

ここで同じ問題

を持つ誰の場合、後半の応答のため申し訳ありません

+0

非常に醜いです。しかしそれは必要条件です。クライアントはそのようにしたい。一方、私は私の問題を解決してあなたの助けに感謝します。 –

+0

既に解決している場合は、ここにソリューションを貼り付けてください。誰かに助けてくれるかもしれない – suku

+0

すぐに投稿されます。 –

関連する問題