質問編集履歴

1

SQL文とコードを書き換えました。

2022/09/06 14:05

投稿

gemfighter
gemfighter

スコア38

test CHANGED
File without changes
test CHANGED
@@ -1,14 +1,14 @@
1
1
  ### 実現したいこと
2
2
 
3
3
  中のデータベースを検索したいのですが、Android Studioでのデータベースの検索方法が解らなくて困っています。
4
- 一応selectionArgsにselectionの値を入れるのは理解しているのですが、うまく動作しません。
4
+ "and Child.parent = name_p;"無し状態だと動作しますが、変数の値を条件に入れる動作しません。
5
5
 
6
6
  以下のSQL文を実行したいのですが、書き方が解らないのです。
7
7
 
8
8
  SELECT Child._id,Child.Name
9
9
  FROM Parent, Child
10
10
  WHERE Parent.Name = Child.parent
11
-
11
+ and Child.parent = name_p;
12
12
  ### 該当のソースコード
13
13
 
14
14
  // データを一覧表示
@@ -18,16 +18,14 @@
18
18
  helper = new DatabaseHelper(this);
19
19
 
20
20
  // データベースを検索する項目を定義
21
- String[] cols = {DBEntry_2._ID, DBEntry_2.Name };
22
- String selection = DBEntry_2.Name + "= ?;";
21
+ String sql = "SELECT Child._id,Child.Name FROM Parent, Child WHERE Parent.Name = Child.parent and Child.parent = " + name_p +";";
23
- String[] selectionArgs ={name_p};
22
+
24
23
 
25
24
  // 読み込みモードでデータベースをオープン
26
25
  try (SQLiteDatabase db = helper.getReadableDatabase()){
27
26
 
28
27
  // データベースを検索
29
- Cursor cursor = db.query(DBEntry_2.Table_Name, cols, selection,
28
+ Cursor cursor = db.rawQuery(sql, null);
30
- selectionArgs, null, null, null, null);
31
29
 
32
30
  }
33
31
  }