実現したいこと
android studioでデータベースを使ったアプリを作ってみています。既存のデータベースからデータを引っ張り出してきたいです。
前提
javaで書いています。
androidstudioに既存のデータベースをインポート(?)するところまではできました。ちゃんとandroidstudio内でインポートできていることも確認できました。とりあえず、1列目のデータを上から10個表示させようとしましたが2個目から読み込めませんのエラーが出ました。
MainActivity.javaからSearchFragment.javaを呼び出しています
データベースは510x52の大きさです
3日ほど格闘しています。どうかよろしくお願いします。
発生している問題・エラーメッセージ
#sql="select colum1 from table1 limit 10" のエラー 2023-03-07 19:13:53.996 15655-15655/com.example.dicapp E/CursorWindow: Failed to read row 2, column 1 from a CursorWindow which has 10 rows, 1 columns. 2023-03-07 19:13:54.001 15655-15655/com.example.dicapp E/AndroidRuntime: FATAL EXCEPTION: main ~省略 Caused by: android.view.InflateException: Binary XML file line #81: Binary XML file line #81: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class fragment Caused by: java.lang.IllegalStateException: Couldn't read row 2, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. .... #sql="PRAGMA table_info(table1)" のときのエラー 2023-03-07 19:09:21.315 15481-15481/com.example.dicapp E/CursorWindow: Failed to read row 7, column 6 from a CursorWindow which has 52 rows, 6 columns. 2023-03-07 19:09:21.319 15481-15481/com.example.dicapp E/AndroidRuntime: FATAL EXCEPTION: main ~省略 Caused by: android.view.InflateException: Binary XML file line #81: Binary XML file line #81: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class fragment Caused by: java.lang.IllegalStateException: Couldn't read row 7, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. .....
該当のソースコード
java
1public class SearchFragment extends Fragment { 2 private SearchViewModel homeViewModel; 3 private FragmentSearchBinding binding; 4 private DatabaseHelper helper=null; 5 6 public View onCreateView(@NonNull LayoutInflater inflater, 7 ViewGroup container, Bundle savedInstanceState) { 8 homeViewModel =new ViewModelProvider(this).get(SearchViewModel.class); 9 binding = FragmentSearchBinding.inflate(inflater, container, false); 10 View root = binding.getRoot(); 11 12//ここが問題のコードです 13 setConj(helper); 14 15 return root; 16 } 17 18 19 20 public void setConj(DatabaseHelper helper){ 21 //helperを準備 22 helper=new DatabaseHelper(getContext());//元々this 23 //データベース取得 24 try(SQLiteDatabase db=helper.getWritableDatabase()){ 25 Toast.makeText(getContext(),"接続済み", Toast.LENGTH_SHORT).show(); 26 } 27 28 StringBuilder builder = new StringBuilder(); 29 SQLiteDatabase db = helper.getWritableDatabase(); 30 try { 31 int i =0; 32 //何個か試したうちの3つです 33 String sql="select colum1 from table1 limit 10"; //error 34 //sql="PRAGMA table_info(table1)"; //error 35 //sql="SELECT name FROM sqlite_master WHERE type='table';" //ちゃんとテーブル名が返ってきました 36 37 Cursor cursor = db.rawQuery(sql , null); 38 cursor.moveToFirst(); 39 builder.append(cursor.getString(i)); 40 while (cursor.moveToNext()) { 41//ここがエラーの出る箇所です。 42 //cursor.getString(i)の行をコメントアウトしたらエラーは出なかったので、クエリ自体はうまく処理されている?? 43 builder.append(cursor.getString(i)); 44 i++; 45 } 46 Log.e("debug", builder.toString()); 47 }finally{ 48 db.close(); 49 } 50 } 51}
試したこと
sql="SELECT name FROM sqlite_master WHERE type='table';"
でクエリを実行したときは、ちゃんとテーブルの名前が返ってきました。
コメントアウトして、どこでエラーが出ているのか確認したら、cursorからwhileを使って順に値を挿入している部分でエラーが発生しているようでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答2件
あなたの回答
tips
プレビュー