Android java SQLite queries and single quotes - android

When attempting to query text fields in SQLite I get force closes if the text field contains a single quote character. I know why it's happening but how can I fix it?

Use the ? operator. Your query will be like FIELD=?, and the next argument to the query() call will be a String array where you provide an argument for each ? you used.

Related

SQLite in Android adding quotes to start and end of datelike string

Interesting issue while using SQLite in Android. I am seeing an inconsistency in the string length and quoting of a string between what is stored in the database and the materialized value seen in Java.
We are using an ORM called SugarORM to query the DB, but I've traced the offending code to the internal android.database.sqlite.SQLiteCursor class used within SugarORM, specifically the cursor.getString(columnIndex) method.
I have a string in the database that is an ISO data string 2019-03-25T19:19:39.664Z and is stored in a VARCHAR column . I have confirmed using DB Browser for SQLite that the length of the string as its stored in the database is indeed 24 characters. SELECT LENGTH(MyStringColumn) FROM MyTable WHERE ...
When I get the value of this string via cursor.getString(columnIndex), it is returning the string "2019-03-25T19:19:39.664Z". Notice the leading and trailing quotes. Java reports to me that the string is 26 characters long.
Any value that I store in this column that is not an ISO data does not have this behavior. I tried tracing the SQLiteCursor source back, but ultimately it ends up being a Native method and that's where my skill set stops.
Can anyone explain what might be going on here? I am probably just going to write a wrapper around my queries to get rid of the quotes, but its all very perplexing. The date string is being fed to a JavaScript interpreter and causing it to fail when creating a JavaScript Date object.
If it helps, I have replicated the behavior on both my S7 physical device and a Pixel 6 emulator.
As a quick get around you could use :-
SELECT length(replace(mystringcolumn,'"','')) FROM mytable;
or before using the original SELECT use :-
UPDATE mytable SET mystringcolumn = replace(mystringcolumn,'"','');
If this doesn't fix the issue, then for some reason it is the code that retrieves the data that is at fault.
e.g. consider :-
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (mystringcolumn VARCHAR);
INSERT INTO mytable VALUES('2019-03-25T19:19:39.664Z'),('"2019-03-25T19:19:39.664Z"');
SELECT length(mystringcolumn), length(replace(mystringcolumn,'"','')) FROM mytable;
which results in :-
i.e. The 2nd row, 2nd column retrieves the appropriate value by using the replace function to strip of the quotes, if they exist.
As to why the quotes exist could depend upon either the way that the data is inserted (perhaps you have inadvertenly coded the quotes but the db being looked at isn't the actual database as copied from the App) or the way in which the data is being retrieved that for some reason adds them.
I don't believe it likely that the Cursor getString method has a bug in which the quotes are added, otherwise such an issue would likely be a recurring issue.

SQLite: Will changing data type affect the database?

I need to change the data type for my SQLite. I am worry that it might effect users who update the App. However, after reading the SQLite document in the following link
https://sqlite.org/datatype3.html
It would seem changing the data type when creating a table column shouldn't break the App. From what I read it seems unlike other SQL database engines, SQLite datatype is associated with the value itself and not the column data type that I initially assigned.
I was going to alter the column data type when user updates the App, but it doesn't seem necessary (nor possible without dropping and recreating table). Am I reading this correctly or am I making a mistake?
The App seems to work well when I test updating, but I want to make sure I am not missing anything. Any feedback is appreciated.
Changing the type name in the column definition can affect the affinity.
This might change the type of some values (for example, attempting to store the string '123' in an INTEGER column will result in the number 123), and might change how comparisons work (WHERE SomeColumn = ? will try to convert the value to the same type as the column's affinity).
So you should change the type name only if you are sure that your app handles the values in this column correctly.

Get the value from the cursor in Android SQLite

I used the below code to get the "place" value from the "place" column using a cursor. The value of the place in my database is a string,but It returns a number that I have no idea what it is.
here's my code:
meeting.setPlace(cursor.getString(cursor.getColumnIndex("place")));
I think I've inserted them correctly... here it is:
contentValues.put("place",R.string.consert7_place);
db.insert(TBL_MEETING,null,contentValues);
R.string.consert7_place is an integer, not a String.
You need to use getResources().getString(R.string.consert7_place)
For more details, see Android: How do I get string from resources using its name?
Your cursor.getString usage is fine.

How to find index of last occurrence of a substring using sqlite?

I want to find last occurrence of space in path which is stored in _data column. But I couldn't find any lastof(X,Y) function or even reverse(X).
Is it possible? How?
There is no such built-in SQL function, and the Android database API does not allow creating user-defined SQL functions; you have to search for the space in your Java code.

cursor.getType() and CursorIndexOutOfBoundsException exception

There is a thing that I can not understand about Cursor.getType(), can anyone explain why do I get this stupid exception when I want to get columns type if cursor has no record but there are columns? I mean, if cursor has record there is no problem, I can use getType method to get columns type without any problem but if there is no any record it throws this exception out.
The question is why must I need records to get columns type? Why just knowing columns name is not enough to get types of it? Is this ridiculous?
It's because SQLite uses dynamic typing:
Most SQL database engines (every SQL database engine other than
SQLite, as far as we know) uses static, rigid typing. With static
typing, the datatype of a value is determined by its container - the
particular column in which the value is stored.
SQLite uses a more general dynamic type system. In SQLite, the
datatype of a value is associated with the value itself, not with its
container.
http://www.sqlite.org/datatype3.html
So no value, no data type.

Categories

Resources