Skip to content

Instantly share code, notes, and snippets.

@iamnoah
Created May 7, 2010 18:44
Show Gist options
  • Save iamnoah/393842 to your computer and use it in GitHub Desktop.
Save iamnoah/393842 to your computer and use it in GitHub Desktop.
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* Dear Android, why do I have to write this class myself? Is this to discourage SQL use?
*/
public class QueryBuilder {
private String[] columns;
private boolean distinct;
private String table;
private String selection;
private String[] args;
private String groupBy;
private String having;
private String limit;
private String orderBy;
public QueryBuilder select(String... columns) {
this.columns = columns;
return this;
}
public QueryBuilder distinct() {
this.distinct = true;
return this;
}
public QueryBuilder from(String table) {
this.table = table;
return this;
}
public QueryBuilder where(String selection, String... args) {
this.selection = selection;
this.args = args;
return this;
}
public QueryBuilder groupBy(String groupBy) {
this.groupBy = groupBy;
return this;
}
public QueryBuilder having(String having) {
this.having = having;
return this;
}
public QueryBuilder orderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
public QueryBuilder limit(String limit) {
this.limit = limit;
return this;
}
public Cursor query(SQLiteDatabase db) {
return db.query(distinct, table, columns, selection, args, groupBy,
having, orderBy, limit);
}
}
@iamnoah
Copy link
Author

iamnoah commented Apr 30, 2012

Compare the actual APIs. SQLiteQueryBuilder is a huge pain to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment