Get name of constraint which caused SQLiteConstraintException - android

How do I get the name of the constraint which caused the SQLiteConstraintException.
Calling toString() on the exception just gives me: "error code 19: constraint failed"
And there is no method in the exception to get the cause. This makes it rather difficult to debug my sql.

Beginning with version 3.7.17, SQLite shows the name of the constraint in error messages:
sqlite> create table t(x, constraint x_positive check (x>0));
sqlite> insert into t values(-1);
Error: constraint x_positive failed
However, it will be some time before this version shows up in Android.
In the meantime, you could replace the constraint with a trigger that can use whatever message it likes:
CREATE TRIGGER check_x_positive
BEFORE INSERT ON t -- also UPDATE
FOR EACH ROW
WHEN NEW.x <= 0
BEGIN
SELECT RAISE(ABORT, 'RED ALERT! database on fire because x <= 0');
END;

Related

Copying an SQLite Table Applying Default Values

I have an SQLite database in my Android app that I would like to update. The update requires a change of datatype from INT to FLOAT. I understand the way to achieve this in SQLite is to create a new table, copy the data, and then replace the old table with the new one. Copying would be done as follows:
INSERT INTO newTable SELECT * FROM oldTable
where integer values would be conveniently converted into floating point values.
Now, there is the problem that another, previously optional column is now ´NOT NULL´ but has a default value. I would like to have all NULL instances replaced by the default as part of the above process using the following statement:
INSERT INTO newTable SELECT * FROM oldTable ON CONFLICT REPLACE
or, for completeness, in Android/Kotlin:
db.execSQL(“INSERT INTO newTable SELECT * FROM oldTable ON CONFLICT REPLACE”)
This gives me a syntax error, and I could not find a suitable example elsewhere. What would be the correct syntax to achieve this - and is what I'm trying to do even possible?
If you look at the documentation for INSERT, you'll see it should be INSERT OR REPLACE ..., and, yes, it'll work to replace null values in the not-null column with the default value.
Replace conflict resolution documentation:
When a UNIQUE or PRIMARY KEY constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with the default value for that column, or if the column has no default value, then the ABORT algorithm is used. If a CHECK constraint or foreign key constraint violation occurs, the REPLACE conflict resolution algorithm works like ABORT.
Example:
sqlite> CREATE TABLE foo(x, y NOT NULL DEFAULT 1);
sqlite> INSERT INTO foo(x,y) VALUES ('a', null);
Error: NOT NULL constraint failed: foo.y
sqlite> INSERT OR REPLACE INTO foo(x,y) VALUES ('a', null);
sqlite> SELECT * FROM foo;
x y
---------- ----------
a 1

get database error code on SQLite exception in android

I use insertOrThrow in my android code to insert rows in to sqlite database. I use try catch to trap SQLiteException. I have set unique constraint in a column other than _id.
In case of errors i get exception message like the following and that is what i wanted.
"column word is not unique (code 19)"
Now, if the error is about duplicate insertion then i get the above error. And i would get messages like that for different errors.
What i need is an error number like in mysql. I was searching for a property of the android DB object which would have an error code but could not. I think in this case i need to parse the error message to get the error code.
The reason to have an error code is to identify the errors. In this case i can find duplicates if the error code is 19. If the entry is a duplicate then i will display 'Data already exists' message else i will display a generic error info like 'Unable to save information'.
Is there any other way to find the error code other than parsing?
Will the error code be consistent?

Why do I get syntax error when creating a view with column names?

Developing an Android app, but this is a SQLite question.... My SQL is much ropier than my Java.
This is the SQL (copied from a log file, in code it's constructed from various constants):
CREATE VIEW albums (_id, name , type ) AS SELECT rowno, name, subtype FROM metadata WHERE subtype = 'album'
but it throws:
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling:
Specifying column names in the view seems to me to be clearly permitted, if I understand the flow chart in the sqlite documentation here, and at this time of night I really can't see what's wrong. I have tried changing the name of the _id column in case it was something to do with the initial underscore, but to no avail and in any case I will need the _id column later for the CursorAdapter that will end up using this View.
At the bottom of the documentation page you linked it states:
Note that the column-name list syntax is only supported in SQLite
versions 3.9.0 and later.
Version 3.9.0 is not supported by Android. Lollipop has 3.7.11. Marshmallow has 3.8.10.2.

SQLite delete syntax error

This line, mDatabase.execSQL(sql);, gives me the following error:
{ sqlite returned: error code = 1, msg = near "*": syntax error }
for Delete * from table_name Query
My SQL query is : DELETE * FROM table_name
How can I solve this?
DELETE * FROM table_name is a wrong sql command. Use DELETE from table_name
Syntax error means that basically your statement is spelled wrong and it can not be parsed. In this case error message states where exactly this error occurred - on "*" character. In such case you should go to database documentation and check proper syntax of the command you are trying to use. In case of SQLite it's here. You can find documentation about DELETE statement there, here is the link. It shows you the syntax in graphical way (called syntax diagrams or railroad diagrams) which should be quite easy to follow.
In this case, as mentioned earlier, you just can't specify "*" between DELETE and FROM. This is because you are always deleting whole rows and you can't delete individual selected columns.
If you need to delete the entire table can you use DROP TABLE IF EXISTS then recreate the table

How to log SQLite commands in Android?

Im getting this error:
10-05 19:36:09.904: WARN/System.err(1039): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
Is there a way to the class SQLiteDatabase show queries created in insert and update methods?
Ty
You are probably inserting two items with the same primary key, which is supposed to be unique. There is no way that I know of to get verbose SQL logs, sorry.

Categories

Resources