1

我知道有类似的东西是 Stack Overflow,但我没有找到答案,所以也许有人可以帮助我并解释那个 rawQuery 有什么问题..?

有我的常数:

    // id column for all tables & DB version
public static final String KEY_ID = "_id";
private static final int DATABASE_VERSION = 1;

// TABLE PLAYER
private static final String DATABASE_TABLE_PLAYER = "Player";
// columns in player table
public static final String PLAYER_COLUMN_NAME = "name";
public static final int PLAYER_NAME_INDEX = 1;

// TABLE RESULT
private static final String DATABASE_TABLE_RESULT = "Result";
// columns in result table
public static final String RESULT_COLUMN_GAME_ID = "game_id";
public static final int RESULT_GAME_ID_INDEX = 1;
public static final String RESULT_COLUMN_PLAYER_ID = "player_id";
public static final int RESULT_PLAYER_ID_INDEX = 2;
public static final String RESULT_COLUMN_POSITION = "position";
public static final int RESULT_POSITION_INDEX = 3;

// TABLE GAME
public static final String DATABASE_TABLE_GAME = "Game";
// columns in game_result tabe
public static final String GAME_COLUMN_DATE = "date";
public static final int GAME_DATE_INDEX = 1;
public static final String GAME_COLUMN_DURATION = "duration";
public static final int GAME_DURATION_INDEX = 2;

private static final String DBCREATE_SQL_1 = "create table " + DATABASE_TABLE_PLAYER + " ( " + KEY_ID
        + " integer primary key autoincrement, " + PLAYER_COLUMN_NAME + " text not null); ";
private static final String DBCREATE_SQL_2 = "create table " + DATABASE_TABLE_GAME + " ( " + KEY_ID
        + " integer primary key autoincrement, " + GAME_COLUMN_DATE + " string not null, " + GAME_COLUMN_DURATION + " int not null); ";
private static final String DBCREATE_SQL_3 = "create table " + DATABASE_TABLE_RESULT + " ( " + KEY_ID
        + " integer primary key autoincrement, " + RESULT_COLUMN_POSITION + " integer not null, " + RESULT_COLUMN_GAME_ID
        + " integer not null, " + RESULT_COLUMN_PLAYER_ID + " integer not null, FOREIGN KEY( " + RESULT_COLUMN_GAME_ID
        + " ) REFERENCES " + DATABASE_TABLE_GAME + "(" + KEY_ID + "), " + "FOREIGN KEY( " + RESULT_COLUMN_PLAYER_ID + " ) REFERENCES "
        + DATABASE_TABLE_PLAYER + "(" + KEY_ID + ") );";

// JOIN STATEMENT NEED TO JOIN TABLES WHEN GIVING TO STATS TABLE
private static final String DBJOIN_SQL = "SELECT * FROM ( ? JOIN ? ON ? = ? ) JOIN ? ON ? = ?";

还有我的 rawQuery 方法:

public List<GameResultOut> getRowsToTable() {
    // SELECT * FROM (? JOIN ? ON ?=?) JOIN ? ON ?=?
    Cursor resultRows = database.rawQuery(DBJOIN_SQL, new String[] { DATABASE_TABLE_PLAYER, DATABASE_TABLE_RESULT,
            DATABASE_TABLE_PLAYER + "." + KEY_ID, DATABASE_TABLE_RESULT + "." + RESULT_COLUMN_PLAYER_ID, DATABASE_TABLE_GAME,
            DATABASE_TABLE_RESULT + "." + RESULT_COLUMN_GAME_ID, DATABASE_TABLE_GAME + "." + KEY_ID } );

    database.ra

    List<GameResultOut> resultList = new ArrayList<GameResultOut>();

    resultRows.moveToFirst();
    boolean isLast = resultRows.isLast();
    int columnIndexName = resultRows.getColumnIndex(PLAYER_COLUMN_NAME);
    int columnIndexPosition = resultRows.getColumnIndex(RESULT_COLUMN_POSITION);
    int columnIndexDate = resultRows.getColumnIndex(GAME_COLUMN_DATE);
    int columnIndexDuration = resultRows.getColumnIndex(GAME_COLUMN_DURATION);
    DateFormat df = DateFormat.getInstance();

    while (!isLast) {
        try {
            GameResultOut result = new GameResultOut(resultRows.getString(columnIndexName), resultRows.getInt(columnIndexPosition),
                    df.parse(resultRows.getString(columnIndexDate)), resultRows.getInt(columnIndexDuration), 0);
            resultList.add(result);
        } catch (java.text.ParseException e) {
            Log.e("PARSE EXCEPTION (Krystian)", "Error during date parse in DBAdapter.getRowsToTable");
            return null;
        }
        isLast = !resultRows.moveToNext();
    }
    return resultList;
}    

LogCat 有错误

10-27 15:05:39.825: E/AndroidRuntime(2344): Caused by: android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: SELECT * FROM ( ? JOIN ? ON ? = ? ) JOIN ? ON ? = ?

感谢您的任何帮助.. :)

4

1 回答 1

3

您可以对子句等中的?内容使用占位符。WHERE您不能将它们用于表名等,例如在您的各种JOIN子句中,AFAIK。我相信您将需要拼接实际的字符串而不是使用?.

于 2013-10-27T14:56:38.743 回答