(android) how to use variable in sql?

Viewed 31

I want to use variable value in where condition in select query.

But database reconnized it as a String.

What should i do?

Here is code

public static void getCommentListOnlyTwoFromDB(int id) {
       
        String sql = "SELECT writer, contents, rating, time, recommend FROM CommentList WHERE movieID=id"; // HERE
        ArrayList<CommentModel> data = new ArrayList<>();
        Cursor cursor = database.rawQuery(sql, null );
    }

When I execute this code. 'WHERE movieID=id' is just String as movieID=id...

I want to put id value in movieID.

please help me.

THANK YOU!

1 Answers
public static void getCommentListOnlyTwoFromDB(int id) {
       
        String sql = "SELECT writer, contents, rating, time, recommend FROM CommentList WHERE movieID=" + id; // HERE
        ArrayList<CommentModel> data = new ArrayList<>();
        Cursor cursor = database.rawQuery(sql, null );
    }
Related