Krystian
Krystian

Reputation: 2290

Android SQLite: SQLiteException: near "?" syntax error

I know that there was something similar is Stack Overflow, but I didn't find the answer, so maybe someone could help me and explain what is wrong in that rawQuery..?

There are my constants:

    // 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 ? = ?";

And there is my rawQuery method:

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;
}    

And there is error from 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 ? = ?

Thank you for any help.. :)

Upvotes: 1

Views: 2486

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006674

You can use ? placeholders for things in WHERE clauses and the like. You cannot use them for table names and such, as in your various JOIN clauses, AFAIK. I believe that you will need to splice in the actual strings instead of using ?.

Upvotes: 3

Related Questions