18800

Android how to write the query with a proper proje

Question:

I have a sqlite table called TermTable with id, selected, term and type columns:

static final String TermTable = "Terms"; static final String ID = "id"; static final String Selected = "selected"; static final String Term = "term"; static final String Type = "type";

I create the table as follows:

private static final String TERM_TABLE_CREATE = "Create table " + TermTable + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Selected + " VARCHAR(5) ," + Term + " VARCHAR(20) ," + Type + " VARCHAR(10))";

The column "selected" gets the strings "true" or "false".

Now I want to query and get the cursor which gets a string as a term and if its selected column is true, returns the columns id and type:

public Cursor getFavoriteValues(String term) { String from[] = { "id", "type" }; String where = ??? String whereArgs[] = ??? Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null); return cursor; }

How are this query attributes defined?

Answer1:

Try out below:

public Cursor getTermValues(int index) { String from[] = { "id", "Type" }; String where = WordsDB.ID + "=?"; // You add column as per your Query String[] whereArgs = new String[]{index + ""}; Cursor cursor = db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null); return cursor; }

Answer2:

public Cursor getTermValues(String term) { String from[] = { "id", "Type" }; String where = WordsDB.Selected + "=? AND " + WordsDB.Term + "=?"; String whereArgs[] = new String[]{ "true", term }; return db.query(WordsDB.TermTable, from, where, whereArgs, null, null, null, null); }

Answer3:

String where = "Selected = '?'" String whereArgs[] = new String[] { "True" }

Recommend

  • Android how to write the query with a proper projection?
  • How to use _COUNT in BaseColumns
  • SQLite: *prevent* PRIMARY KEY value from resetting after delete all rows [duplicate]
  • Can I create dynamic pivot query from key value table with different data types?
  • use variable in FROM statement
  • How can I replace all key fields in a string with replacement values from a table in T-SQL
  • String matching
  • DELETE statement issues within a trigger definition
  • Get Quarters StartDate and EndDate from Year
  • Memory usage of storing strings as varchar in MySQL
  • MS SQL Server 2008 :Getting start date and end date of the week to next 8 weeks
  • How to find data from last week in MySQL
  • How to Compose OSGi Based project with C++ based project?
  • Cassandra: What is a subcolumn
  • How can I count unique terms in a plaintext file case-insensitively?
  • Identifier too long in Oracle
  • Image map in Flex
  • Who propagate bugfixes across branches (corporate development)?
  • Eliminate partial duplicate rows from result set
  • MySQL Order by column = x, column asc?
  • Allowing both email and username for authentication
  • Javascript + PHP Encryption with pidCrypt
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Error creating VM instance in Google Compute Engine
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Recursive/Hierarchical Query Using Postgres
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?