Error inserting Currency characters in sqlite database from eclipse / android - android

I'm inserting queries like below in sqlite database from sql file via eclipse:
INSERT INTO "Country_Table"
("countryid", "country_name" , "language", "currency", "VAT_abbr", "VAT_rate", "VAT_label", "creation_date" , "update_date", "status")
VALUES
("3","Austria","German","€","USt.","20,00","UID-Nr.","2013/06/27","2013/06/27","1");
Execution is as below:
07-19 16:46:43.739: I/System.out(23733): ---------------------executing----------------INSERT INTO "Country_Table" ("countryid", "country_name" , "language", "currency", "VAT_abbr", "VAT_rate", "VAT_label", "creation_date" , "update_date", "status") VALUES ("3","Austria","German","�","USt.","20,00","UID-Nr.","2013/06/27","2013/06/27","1");
This sql file is placed in assets folder.
But executing this queries, I found problem with the symbols "€" & "£", but it works fine with "$".
Insertion of the symbols takes place with '?' symbol whenever executed.
EDIT
I'm executing this query in SQLite then it works properly, but when executing this for android local database, the symbols of EURO and POUND are inserted as '?' in the database.

You have to ensure that the SQL file is encoded as UTF-8, and that your code that reads the file (which you have not shown) does not change the encoding.

Related

Error in studio SQLiteException: json_each

I have json Array in my column and i want to extract json array as separate values I am using sqlite query
SELECT
json_extract(t2.value, '$.time') as time
FROM
json_each((SELECT column_name
FROM tbl_name)) AS t2
My column looks like this
[{"time":0,"value":0},{"time":2,"value":0},{"time":0,"value":0}]
It works fine in sqlite browser but I get an error in Android studio.
SQLiteException: no such table: json_each (code 1):
Update: I am using room database and it works fine for other queries
json_each and json_extract would need JSON1 extension. It's not available on Android. You need to do your json mapping in code.

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.

Android One Plus Two: Failed to change locale for db

I am using Sqlite database in one of my Android project. It works fine in all the devices except One Plus Two device.
I am getting exception while trying to open the database. This is the crashlog.
12-23 19:14:35.235: E/SQLiteLog(3133): (11) database corruption at line 53216 of [9491ba7d73]
12-23 19:14:35.235: E/SQLiteLog(3133): (11) statement aborts at 7: [SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1]
12-23 19:14:35.237: E/SQLiteDatabase(3133): Failed to open database '/data/data/com.mycompany.myapp/databases/alcochange.sqlite'.
12-23 19:14:35.237: E/SQLiteDatabase(3133): android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/com.mycompany.myapp/databases/myapp.sqlite' to 'en_US'.
Please click here to see the full crash log
I have seen other questions like that here, I have tried almost every answers, but nothing works.
I have tried this and this.
EDIT: The device runs on Oxygen Lollipop.
I can't find the reason why I am having this error, but somehow I have avoided this by doing the following.
Previously, I had a .sqlite database in the bundle and when the user open our app for the first time, I copied the .sqlite database from the bundle to the correct path.
But now, I have exported the raw queries from the .sqlite and added it with the bundle as a txt file. Now when the user opens the app for the first time, I will first create an empty .sqlite file in the path and then I read all the .sql queries from the txt file from the bundle and execute them over the .sqlite file in the path.
Try to change locale manually.
Open your database file with a SQLite editor such as SQLiteStudio, then open android_metadata table. if it does not exist, create it. (you may create it with the query editor (tools>open query editor) and copy/paste the DDL code below)
CREATE TABLE android_metadata (
locale TEXT
);
for inserting the record you may copy/paste this line in the query editor:
INSERT INTO android_metadata VALUES ('en_us');
Hint: to run the query in SQLiteStudio you should push the button with icon in the toolbar.

How to insert cyrillic characters in a sqlite table using fts3 in android

I have a sqlite database in my android app where all tables are virtual because i need a fast search over some whole tables. I'm also using the and extended class of SQLiteOpenHelper to open the my database.
Here an example of a table create I use:
CREATE VIRTUAL TABLE person USING fts3(firstName VARCHAR(50), lastName VARCHAR(50))
Everything works fine as long as every text I insert is in latin characters. Unfortunatly whenever I try to insert some cyrillic characters I get a SQLiteException near the cyrillic character which it says is a syntax error.
Example:
INSERT INTO person (firstName, lastName) VALUES ('John','Россия')
android.database.sqlite.SQLiteException: near "Россия":
syntax-error: INSERT INTO person (firstName, lastName) VALUES ('John','Россия')
As far as I know SQLite uses utf-8 as standard.
I have already tried to do it without using fts3 (no virtual table), but the result is the same.
Could somebody help me to find out what I'm missing to insert cyrillic text into my database?

Error in Sqlite. Android

I create a database. In this datebase create 3 lines. When I using this database in Android i get this error:
12-18 10:30:10.882: INFO/Database(477): sqlite returned: error code = 11, msg = database corruption found by source line 40107
12-18 10:30:10.882: INFO/Database(477): sqlite returned: error code = 11, msg = database disk image is malformed
This error is obtained, when I do: Select or Insert. How to solve this error?
Delete your app from emulator then install it again, this will create a new database.
EDIT:
Make sure you have a table named android_metadata with a column locale (TEXT), put a string in it (en_US)
A little trick that i do is to create the database normally in the application, I populate it, then i retrieve it from emulator using DDMS or adb (from /data/data/mypackage/databases/nameofthedatabase), and then I put it in the assets. This way I can be sure the database is valid.
Or you can create it in the application, retrieve it, then populate it or modify it (I use sqlitebrowser in linux, works beautifully), then I put it back in /data/data/mypackage/databases/nameofthedatabase, or in assets and copy it there.

Categories

Resources