Android SQLite:SQLiteException:接近“?”语法错误

时间:2013-10-27 14:53:06

标签: android database sqlite syntax-error

我知道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 coulumnIndexDate = resultRows.getColumnIndex(GAME_COLUMN_DATE);
    int coulumnIndexDuration = 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(coulumnIndexDate)), resultRows.getInt(coulumnIndexDuration), 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 ? = ?

感谢您的帮助.. :))

1 个答案:

答案 0 :(得分:3)

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