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.
Related
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.
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.
I am working on an app and need some help. I have a table where I store some informations and another which has a reference. The column where the reference is made is named "COLUMN_MODULE_ID". In my DBHelper, I have declared it as an Integer. But the value I get (which is the ID from the other inserted table row which I get as a long type) seems to not get into the table.
If I put i.e. 600 hard coded into the column "COLUMN_MODULE_ID", everything works fine. But if I set the long value "module_id" hard coded to 600 and put the variable into the column, it doesn't work.
Any suggestions?
edit:
Okay, after further inspection, I have the following code:
Uri moduleUri = mContext.getContentResolver().insert(DataContract.ModuleEntry.CONTENT_URI,
moduleValues);
String moduleId = moduleUri.getLastPathSegment();
Log.v("Module ID: ", moduleId);
getQuestionsArrayFromJson(moduleName, century, moduleId);
Lets expect, that the Log says "Module ID: 887". My App doesn't work.
But if I add
moduleId = "887"
after
String moduleId = moduleUri.getLastPathSegment();
, the log says the same but my App works. I have no clue why but I think it must be some type thing.
edit #2:
So I came further to the point where the whole thing blows up. When I fetch my data from a JSON String, I implement it into a table, which works fine. However, I get the _id from the inserted entry, which inserts just fine, with the following snippet:
long moduleId = ContentUris.parseId(moduleUri);
so when I log the variable moduleId, it shows the right _id.
When I try to insert it into another sqlite table, it doesn't work.
questionValues.put(DataContract.QuestionEntry.COLUMN_QUESTION_NO, questionNo);
questionValues.put(DataContract.QuestionEntry.COLUMN_QUESTION_TEXT, text);
questionValues.put(DataContract.QuestionEntry.COLUMN_MODULE_ID, moduleId);
Uri questionUri = mContext.getContentResolver().insert(DataContract.QuestionEntry.CONTENT_URI,
questionValues);
I just can't access it from my app. If I change the code to have a fix number in it like:
questionValues.put(DataContract.QuestionEntry.COLUMN_QUESTION_NO, questionNo);
questionValues.put(DataContract.QuestionEntry.COLUMN_QUESTION_TEXT, text);
questionValues.put(DataContract.QuestionEntry.COLUMN_MODULE_ID, 555);
Uri questionUri = mContext.getContentResolver().insert(DataContract.QuestionEntry.CONTENT_URI,
questionValues);
I can access the module with the id 555 just fine from my app. So I think it has something to do with the type that the ContentUris.parseId gives back.
I can also put the 555 in a long or int variable and put the variable into the questionValues.
Any suggestions?
Okay I figuered it out. My Code was fine. I had another problem, clearing the table everytime I fetched new data into my database. Bad mistake, sorry.
I created a table in the database that has the data like this:
Now i have written a query that updates the contact field by concatinating name and email fields:
UPDATE MyContacts SET contact=(SELECT name||'--'||email FROM MyContacts);
Here the problem is after executing the query the table is as below:
Why is it happening like this? In oracle i never faced this problem. Please help me. Thank you
Right now you're not specifying the correct row to retrieve the values from. Try something like this:
UPDATE MyContacts SET contact = name||'--'||email;
EDIT: Glad it worked. Your first issue was that your sub-select uses a SELECT statement with no WHERE clause (SELECT name||'--'||email FROM MyContacts will return 3 rows). One possible solution would be for SQLite to throw an error and say You've tried to set a column to the result of an expression that returns more than 1 row: I've seen this with MySQL and SQL Server. However, in this case SQLite appears to just use only the very first value returned. However, your second error then kicks in: since you don't narrow your UPDATE statement with a WHERE clause, it uses that first value returned to update EVERY single row, which is what you see.
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.