Android - Database SQLite - convert boolean values - android

I am working on an Android project with a SQLite database and have a column in the database consisting of a boolean value (which store 1s and 0s) of whether an individual in the database is important.
What I am trying to do is output into a textview listview a "!" if an individual is important, and " " if they are not important using a cursor and cursor adaptor. I can get the "1" and "0"s to appear in the listview, but my question is how/where do I convert these to "!" and " "?
The query I am currently using is
return database.query("people", new String[] {"_id", "important"}, null, null, null, null, "important" + " DESC, " + "name" + " ASC");

The easiest way is to do this in the database query.
The query function is too inflexible for that, so you have to use rawQuery instead:
db.rawQuery("SELECT _id, " +
"CASE WHEN important THEN '!' ELSE ' ' END AS important " +
"FROM people " +
"ORDER BY important DESC, name ASC", null);

If it is a custom ListView which I am pretty sure it is, then you'll have to do that in your CustomAdapter for the ListView, which takes care of populating the ListView with the data you pass to it.Check this out.
Doing it is pretty simple, just loop through your Cursor and do if else statement.

Related

SQLite Statement in Android Studio 3.5.3

I'm a newbie with Android Studio so please be patient... This forum often leads me with suggestions and examples (as a reader), but today I decided to ask for help:
Since hours, I try to build an SQLite statement in Android Studio: There is a column COLUMN_LAST_ATTEMPT with date and time as String, e.g. 2020-01-09 17:23, see screenshot, and I want to get the newest date (without time) from the table, e.g. 2020-09-01. I tried various options but I can't get it to run.
What I need is an Android SQLite Statement for
SELECT MAX(SUBSTR(last_attempt,11,20)) FROM quiz_questions
(which runs on DBBrowser), where 'last attempt' is a column of table 'quiz_questions', screenshot of that column in table 'quiz_questions'
I tried the following rawQueries, none of them works:
In QuizDBHelper-Class
//...
final QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
//...
public String newestQuiz(){
db = getReadableDatabase();
String result = null;
Cursor cursor = db.rawQuery("SELECT MAX(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ") FROM "
+ QuizContract.QuestionsTable.TABLE_NAME, null);
//Cursor cursor = db.rawQuery("SELECT MAX(SUBSTR(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT +
// ",11,20)) FROM " + QuizContract.QuestionsTable.TABLE_NAME, null);
//Cursor cursor = db.rawQuery("SELECT " + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + " FROM " +
// QuizContract.QuestionsTable.TABLE_NAME, null);
if(cursor.moveToFirst()){
do {
result = cursor.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT));
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
In Statistics-Class
String LastUse = dbHelper.newestQuiz();
LastUsage.setText("Letzte Challenge: " + LastUse);
//LastUsage is a TextView in activity_Statistics.xml
//attached with LastUsage = findViewById(R.id.text_lastUsage);
Either the SQLite statements are totally wrong or I make (basic?) mistakes in statistics class. I need ...newbie help!
I need something like Select column from table where substring of date-Entry == newest
Your issue appear to be column names. That is a Cursor only contains the columns extracted, not all the columns from the table. Although you are basing your query on the column as per QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT that will not be the column name in the cursor.
Rather it will will MAX(SUBSTR(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT +
// ",11,20))
The simplest way of managing this is to give the column in the Cursor a specific name using AS. As such perhaps use :-
Cursor cursor = db.rawQuery("SELECT MAX(" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ") AS " + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + " FROM "
+ QuizContract.QuestionsTable.TABLE_NAME, null);
However, you may prefere to use a column name (AS ????) specififc to the situation e.g.
........ AS max_" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT + ........
You would then have to use :-
result = cursor.getString(c.getColumnIndex("max_" + QuizContract.QuestionsTable.COLUMN_LAST_ATTEMPT));
Alternately, as it's just a single value/column that is returned in the cursor you could use the column offset of 0, in which case the column name is irrelevant as long as it is valid. However, using offsets is not typically recommended due to the lack of validation of the column being accessed.
re the comment :-
I just need the date part
As the date is a recognised DateTime format (and also that such formats are directly sortable/orderable), use max(date(column_name)) or even max(column_name).

Content loader reverse cursor order

I have a simple chat application. I am using content loaders to display messages inside a channel. Since I want 100 recent messages I sort messages based on the time stamp desc and limit it to 100. but I want to display recent messages at the bottom of the screen. Right now the cursor adaptor displays recent message first. How do I reverse the order of the cursor ?
According to me, I will have to write some smart query inside content provider or somehow have to manipulate the cursor inside cursor adaptor's bindView.
Try to sort your messages based on timestamp ASC instead of DESC.
edit: Sorry, didn't grasp the subtility here.
You can try something like this to get data in reverse order:
for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious())
{
// do your magic
}
I would probably use subquery
SELECT * FROM table ORDER BY timestamp WHERE _id
IN (SELECT _id FROM table ORDER BY timestamp DESC LIMIT 100)
To use that with content provider:
String sel = BaseColumns._ID + " IN (SELECT " + BaseColumns._ID +
" FROM " + MyDb.TableName._TABLE_NAME + " ORDER BY " +
MyDb.TableName.TIMESTAMP + " DESC LIMIT 100)";
Cursor q = getContentResolver().query(MyDb.TableName.CONTENT_URI,
null, sel, null, MyDb.TableName.TIMESTAMP);
MyDb is yours class defining tables and its fields, UriS etc.

Delete the last row in a table SQLite android

I am developing an android app where I want to delete the last row in one of my database table. I have tried the code below, but its throwing a syntax error.
public void deletelatestprofilefromsystemsettings()
{
String maxid = System_id + "="+"SELECT MAX ("+System_id+") FROM" +TABLE_SYSTEM_SETTINGS;
getWritableDatabase().delete(TABLE_SYSTEM_SETTINGS, maxid ,null);
}
Please help! Thanks!
You are lacking a space after the FROM, and subqueries must be written in parentheses:
String maxid = System_id + "=" +
"(SELECT MAX("+System_id+") FROM " + TABLE_SYSTEM_SETTINGS + ")";
You are trying to execute a DELETE with a SELECT in the same query. AFAIK you shouldn't do it. You have to execute the SELECT query first, in order to retrieve the desired id, then execute the deletion. In other words, execute Cursor c = getWritableDatabase().query(), read the id from the cursor, then use it in getWritableDatabase().delete().
Also, add a space after ") FROM", so it becomes ") FROM " in order to avoid a syntax error.

Query doesnt work on android, column '_id' does not exist?

I hope anyone can help me with this.
I'm using sqlite3 on my android app. I'm trying to do this query:
mCursor = conexion.getDatabase().rawQuery("SELECT employee.name, company.name FROM employee "
+ " INNER JOIN company "
+ " ON (employee.idCompany=company._id) ORDER BY employee.name", null);
This is my schema:
CREATE table company ("_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "name TEXT NOT NULL);";
CREATE table employee ("_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "name TEXT NOT NULL, "
+ "fullName TEXT NOT NULL, "
+ "idCompany INTEGER NOT NULL, "
+ "FOREIGN KEY(idCompany) REFERENCES company (_id));";
When I run this query on my android app I get the following error:
05-09 22:35:23.170: E/AndroidRuntime(10920): FATAL EXCEPTION: main
05-09 22:35:23.170: E/AndroidRuntime(10920): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ej.ej/com.ej.ej.controlador.AplicacionActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
If I run the same query but with select *:
mCursor = conexion.getDatabase().rawQuery("SELECT * FROM employee "
+ " INNER JOIN company "
+ " ON (employee.idEmployee=company._id) ORDER BY employee.name", null);
No problem I get, it works fine. But since in my schema I have name for company and name for employee I need to differentiate both.
I tried doing this query using cmd and adb commands and it works. I really don't get why the error!!
EDITED: I made it work, as #HalR suggested in the query (dont know why) I have to retrieve the ids. So now I have:
mCursor = conexion.getDatabase().rawQuery("SELECT company._id, employee.name as employee, company.name as company FROM employee "
+ " INNER JOIN company "
+ " ON (employee.idCompany=company._id) ORDER BY employee.name", null);
But I had one more issue and it wasnt in the query, it was on my ActivityFragment (thanks to #Hoan Nguyen, he made me see this part of the code again):
Listing my employees, I was getting the name of my employee and the idCompany. Don't know why, but this was looking for the _id of Company instead of idCompany of Employee.
String[] from = new String[] { "name", "idCompany"};
int[] to = new int[] { R.id.textViewNombreIzq, R.id.textViewNombreCent };
ListView lvEmployee = (ListView) view.findViewById (android.R.id.list);
ListClientsCursorAdapter notes = new ListClientsCursorAdapter(context, R.layout.activity_file_client, mCursorEmployee, from, to, 0);
lvEmployee .setAdapter(notes);
I just changed:
String[] from = new String[] { "name", "idCompany"};
For:
String[] from = new String[] { "employee", "company"};
And now is fine. Thanks to all, for your help I really apreciated :).
first, I in your CREATE TABLE employee statement you used the lable full name as a column label. If it is not a typo here, please change it to something like full_name. you can't just use spaces in column labels this way.
second, use this query string:
SELECT employee.name, company.name FROM employee INNER JOIN company ON (employee.idCompany=company._id) ORDER BY employee.name. Notice the ON expression. it's employee.idCompany not employee.idEmployee.
fix those first, and let me know if the error still exists.
I think you want this:
mCursor = conexion.getDatabase().rawQuery("SELECT employee.idCompany, employee.name, company.name FROM employee "
+ " INNER JOIN company "
+ " ON (employee.idCompany=company._id) ORDER BY employee.name", null);
Note: You need to make sure you specify the column name you are comparing in the section just after the SELECT.
Actually you don't need to name your column as _id in your table. for a example, you can create column as company_id in your company table and when you write your query, you can specify this column as below.
select company_id as _id, name from company.

How do I join two SQLite tables in my Android application?

Background
I have an Android project that has a database with two tables: tbl_question and tbl_alternative.
To populate the views with questions and alternatives I am using cursors. There are no problems in getting the data I need until I try to join the two tables.
Tbl_question
-------------
_id
question
categoryid
Tbl_alternative
---------------
_id
questionid
categoryid
alternative
I want something like the following:
SELECT tbl_question.question, tbl_alternative.alternative where
categoryid=tbl_alternative.categoryid AND tbl_question._id =
tbl_alternative.questionid.`
This is my attempt:
public Cursor getAlternative(long categoryid) {
String[] columns = new String[] { KEY_Q_ID, KEY_IMAGE, KEY_QUESTION, KEY_ALT, KEY_QID};
String whereClause = KEY_CATEGORYID + "=" + categoryid +" AND "+ KEY_Q_ID +"="+ KEY_QID;
Cursor cursor = mDb.query(true, DBTABLE_QUESTION + " INNER JOIN "+ DBTABLE_ALTERNATIVE, columns, whereClause, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
I find this way to form queries harder than regular SQL, but have gotten the advice to use this way since it is less error prone.
Question
How do I join two SQLite tables in my application?
You need rawQuery method.
Example:
private final String MY_QUERY = "SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
Use ? bindings instead of putting values into raw sql query.
An alternate way is to construct a view which is then queried just like a table.
In many database managers using a view can result in better performance.
CREATE VIEW xyz SELECT q.question, a.alternative
FROM tbl_question AS q, tbl_alternative AS a
WHERE q.categoryid = a.categoryid
AND q._id = a.questionid;
This is from memory so there may be some syntactic issues.
http://www.sqlite.org/lang_createview.html
I mention this approach because then you can use SQLiteQueryBuilder with the view as you implied that it was preferred.
In addition to #pawelzieba's answer, which definitely is correct, to join two tables, while you can use an INNER JOIN like this
SELECT * FROM expense INNER JOIN refuel
ON exp_id = expense_id
WHERE refuel_id = 1
via raw query like this -
String rawQuery = "SELECT * FROM " + RefuelTable.TABLE_NAME + " INNER JOIN " + ExpenseTable.TABLE_NAME
+ " ON " + RefuelTable.EXP_ID + " = " + ExpenseTable.ID
+ " WHERE " + RefuelTable.ID + " = " + id;
Cursor c = db.rawQuery(
rawQuery,
null
);
because of SQLite's backward compatible support of the primitive way of querying, we turn that command into this -
SELECT *
FROM expense, refuel
WHERE exp_id = expense_id AND refuel_id = 1
and hence be able to take advanatage of the SQLiteDatabase.query() helper method
Cursor c = db.query(
RefuelTable.TABLE_NAME + " , " + ExpenseTable.TABLE_NAME,
Utils.concat(RefuelTable.PROJECTION, ExpenseTable.PROJECTION),
RefuelTable.EXP_ID + " = " + ExpenseTable.ID + " AND " + RefuelTable.ID + " = " + id,
null,
null,
null,
null
);
For a detailed blog post check this
http://blog.championswimmer.in/2015/12/doing-a-table-join-in-android-without-using-rawquery
"Ambiguous column" usually means that the same column name appears in at least two tables; the database engine can't tell which one you want. Use full table names or table aliases to remove the ambiguity.
Here's an example I happened to have in my editor. It's from someone else's problem, but should make sense anyway.
select P.*
from product_has_image P
inner join highest_priority_images H
on (H.id_product = P.id_product and H.priority = p.priority)

Categories

Resources