Android SQLite: SQLiteException: near ?“语法错误

时间:2022-07-31 22:44:37

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

我知道有一些类似的东西是Stack Overflow,但是我没有找到答案,所以也许有人可以帮助我解释一下这个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:

还有我的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;
}    

And there is error from LogCat

还有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.. :)

谢谢你的帮助。:)

1 个解决方案

#1


3  

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 ?.

您可以使用吗?在从句或类似的东西的占位符。不能将它们用于表名等,如在各种连接子句AFAIK中。我相信您将需要在实际的字符串中拼接而不是使用。

#1


3  

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 ?.

您可以使用吗?在从句或类似的东西的占位符。不能将它们用于表名等,如在各种连接子句AFAIK中。我相信您将需要在实际的字符串中拼接而不是使用。