Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the WHERE clause and '?' in a SQLite query?

Tags:

android

sqlite

I'm trying to use the built in SQLite functions and I'm not sure how to format a query. How do I format the WHERE clause and the ? that can go in there:

column [] = {"name"};
selectionArg [] = {"john};
Select name from mytable where id = "john"

db.query(mytable, column, id = ?, selectionArg, null,null,null);

I'm not to where I can test it, but I'm trying to get it right the first time. I'm not sure if the ? needs to be "?" or if it is id = + "?" and so forth. I can't seem to find any examples of the format.

like image 231
au789 Avatar asked Mar 11 '11 17:03

au789


People also ask

Which of the following commands are used to format the output in SQLite?

The sqlite3 program is able to show the results of a query in eight different formats: "csv", "column", "html", "insert", "line", "list", "tabs", and "tcl". You can use the ". mode" dot command to switch between these output formats.


2 Answers

For the query :

select name from mytable where id = "john"

use

(SQliteDatabase) db.query(
    "mytable" /* table */,
    new String[] { "name" } /* columns */,
    "id = ?" /* where or selection */,
    new String[] { "john" } /* selectionArgs i.e. value to replace ? */,
    null /* groupBy */,
    null /* having */,
    null /* orderBy */
);
like image 123
Karan Avatar answered Nov 10 '22 01:11

Karan


You may not use quotes around the question marks

example:

db.query(
            true,
            "tablename",
            new String[] {
                "col1",
                "col2"
            },
            "col1 = ?",
            new String[]{ "value" },
            null,
            null,
            null,
            null
        );
like image 39
user634618 Avatar answered Nov 09 '22 23:11

user634618