0

我正在开发一个 Android 3.1 应用程序。

我有以下表格:

FormBlock
---------
FormId  (INTEGER) FOREIGN KEY TO Form.id
BlockId (INTEGER) FOREIGN KEY TO Block.id

Block
----------
id          (INTEGER) PRIMARY KEY
name        (TEXT)    NOT NULL
orderInForm (INTEGER) NOT NULL

我想做以下选择:

SELECT block.blockId, block.name, block.orderInForm FROM Block, FormBlock WHERE FormBlock.FormId = 1 AND block.blockId = FormBlock.blockId;

如何使用 SQLiteDatabase.query(...) 或 执行该选择语句rawQuery()

4

2 回答 2

2
SELECT 
    block.blockId, block.name, block.orderInForm
FROM
    Block, FormBlock 
WHERE
    FormBlock.FormId = 1 AND block.blockId = FormBlock.blockId;

原样query_

String table = "Block, FormBlock";
String[] columns = new String[] { "block.blockId", "block.name", "block.orderInForm" };
String where = "FormBlock.FormId = 1 AND block.blockId = FormBlock.blockId";
Cursor result = db.query(table, columns, where, null, null, null, null);

如果1是动态的,那么

int formId = 1;
String table = "Block, FormBlock";
String[] columns = new String[] { "block.blockId", "block.name", "block.orderInForm" };
String where = "FormBlock.FormId = ? AND block.blockId = FormBlock.blockId";
String[] whereArgs = new String[] { String.valueOf(formId) };
Cursor result = db.query(table, columns, where, whereArgs, null, null, null);
于 2012-04-20T10:27:05.480 回答
1

尝试这个:

String query = "SELECT * FROM Block WHERE id IN
                  (SELECT BlockId FROM FormBlock WHERE FormId = 1)";

Cursor c = database.rawQuery(query, null);
于 2012-04-20T10:23:01.817 回答