I have a method that runs a INSERT SQL statment that are got from external soruces and not from the android device. The incoming statments can cause duplicate records and I want the android device to ignore the sql statments if they cause duplicates. The method is:
ourDatabase.execSQL(sql)
The table its inserting the data into has the following column that prevents duplicates
KEY_CONCATA + " TEXT PRIMARY KEY , " +
Should this be unique and not primary key?
The system at the moment recognizes its causing a duplicate and ends the program, I want it to catch the error and stop it being inserted into the database if its a duplicate.
Thanks!
Declare KEY_CONCATA TEXT NOT NULL UNIQUE and when insert use insertWithOnConflict with SQLiteDatabase.CONFLICT_IGNORE flag
Related
try {
db.insertOrThrow("savedreports", null, cv);
} catch (SQLException e) {
Log.e(TAG, e.getMessage(), e);
}
If the code above is executed trying to insert duplicate record (same primary key and fields values as well) what error message will the exception give?
"PRIMARY KEY must be unique"
or
"UNIQUE constraint failed"
or
???
My program will grab some records from a server and then informs the server to delete those records. However sometimes the http request to delete the records fails due to bad connection, hence I would get the same records when I rerequest for the latest records.
I currently insert these records using insertOrThrow, hence when the duplicate records occur, I would like to know exactly the exception thrown it's exactly due to duplicate records (and not due to null column constraint, database connection close or etc).
These constraints are designed to detect programming errors, not runtime errors. Therefore, there is no guaranteed error message.
If you want to check that the key is not a duplicate, you have to do the check yourself.
But if this is the only constraint that can fail, you can INSERT OR IGNORE instead (insertWithOnConflict() in Android).
I have created one Member_Master table whose primary key is member_id in sqlite manager for android and creating foreign key in child table e.g Event_Master for member_id from Member_Master. I was getting no such table found:Member_Master error
But now I am getting the following error :
SQLiteManager: CREATE TABLE "main"."Events_Master" ("event_id" NUMERIC PRIMARY KEY NOT
NULL ,"event_name" TEXT NOT NULL ,"event_details" TEXT NOT NULL ,"event_start_date"
DATETIME NOT NULL ,"event_end_date" DATETIME NOT NULL ,"event_time" DATETIME NOT NULL
,"event_venue" TEXT NOT NULL ,"min_level" NUMERIC,"status_id" NUMERIC,"member_id"
NUMERIC, foriegn key(member_id) references Member_Master(member_id)ON DELETE CASCADE ON
UPDATE CASCADE) [ near "member_id": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[mozIStorageConnection.createStatement]
Please give me the solution. Since Member_Master table is already created and field name is member_id only.
If you are getting no such table exception, the reason for this is that the table is not created.There can be few reasons like
1 ) The command you have used to create the table is is wrong (and could be a typing error also).
2 ) You are adding a table in an already existing database
When Database is created with specific number of tables you can not add more tables to it .
If you want to add more tables, you need to uninstall the application from your emulator and install the application again only then it shall create the new tables by re creating the database.
Or upgrade the database version , it shall drop all the tables once and then recreate the database.
Try uninstalling the app and reinstall it again. it works most of the times.
If you are getting no such table exception, then It is sure that the tables are not getting created.Have You written create Table query in your DatabaseAdapter class or simply adding a table from SqliteManager.
If you are creating tables dynamically, please write CREATE TABLE query and execute Query.
I'm developing an Android 2.2+ app with SQLite db support. I'm trying to define referential integrity constraints while creating the tables. I've also enabled the foreign key support by executing db.execSQL( "PRAGMA foreign_keys=ON;" ); in onCreate and onOpen methods of SQLiteOpenHelper. Afteer setting up my tables and proper foreign key references, it still allows me to insert rows in tables where reference records are missing. For e.g. I've following structure
CREATE TABLE Questions(_id integer primary key,question text not null);
CREATE TABLE Ques_Ans(_id integer primary key autoincrement,qid integer not null,
aid integer not null,is_correct integer default 0,
FOREIGN KEY (qid) REFERENCES Questions(_id));
and following my data in the table
INSERT INTO Questions VALUES(1, 'Some text');
INSERT INTO Ques_Ans(qid, aid, is_correct) VALUES(20, 1, 0);
If foreign key is properly set on Ques_Ans table, 2nd insert should have failed as there is no record in Questions table with id 20, but somehow my app does not thrown any error and inserts the 2nd insert statement. Can anybody tell me whats wrong over here or am I missing any configuration over here?
Update [03-Mar-2012]: After discussion thread with #Catcall
Using sqlite3 tool, switching on PRAGMA foreign_keys=ON; foreign key works as expected
Same thing if used via app on Emulator or on Phone does not work
Insert statements executed using insert() or execSQL(). None of them throw foreign key constraint failed error
PRAGMA integrity_check returns ok. So database is not corrupt.
Your foreign key is on the column "qid", not on the column "_id".
This INSERT statement
INSERT INTO Ques_Ans VALUES(20, 1, 0);
should have thrown the error
Error: table Ques_Ans has 4 columns but 3 values were supplied
This INSERT should succeed.
INSERT INTO Ques_Ans VALUES(20, 1, 0, 0);
This one should fail.
INSERT INTO Ques_Ans VALUES(21, 2, 0, 0);
Error: foreign key constraint failed
Later . . .
Since it works in sqlite, but not in your emulator, the problem is probably in either the emulator or your code.
This is the idiom for database transactions.
db.beginTransaction();
try {
...
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
Android docs suggest using insert() instead of execSQL(). Make sure you're trapping, catching, or checking for errors in every function that can return an error. So, for example, if you switch to insert(), check its return value for -1.
If that doesn't help, you might try tagging this question with "Java" (for example), or asking a new question that focuses on your code and on catching errors.
IN SQLite Foreign key constraints are disabled by default (for backwards compatibility). You have to enable it explicitly using
PRAGMA foreign_keys = 1
after you establishing your connection with the database. Here's the link to the official docs that explains it in more depth. http://sqlite.org/foreignkeys.html Please navigate to enabling foreign key support in the above link.
I have a common problem, and have looked at several solutions but haven't seen one that fits this case.
I have a temporary table that is defined as follows:
public static final String GROUP_TABLE_CREATE =
"CREATE TEMP TABLE "+GROUP_TABLE_NAME+" ("
+GROUP_ID_COLUMN_NAME+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+GROUP_GROUP_ID_COLUMN_NAME+" INTEGER NOT NULL UNIQUE, "
+GROUP_COLUMN_NAME+" VARCHAR(64) NOT NULL)";
The table is created without any problems. I then download some data to insert into it and use the following query to insert it:
ContentValues contentValues = new ContentValues();
contentValues.put(WhereWolfOpenHelper.GROUP_GROUP_ID_COLUMN_NAME, groupID);
contentValues.put(WhereWolfOpenHelper.GROUP_COLUMN_NAME, groupName);
db.insert(WhereWolfOpenHelper.GROUP_TABLE_NAME, null, contentValues);
And then I get the following error:
08-05 08:52:37.791: ERROR/Database(847): Error inserting group_name=Friends group_id=2
08-05 08:52:37.791: ERROR/Database(847): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
The error appears twice, and the only data in the database is the group named Friends (there should be two more entries).
I have another table with very similar code that works without any problems, so I'm guessing it's just some silly mistake that I haven't spotted. Anyone got any ideas?
If "constraint failed" than something in table scheme should tell us what is wrong.
I see what it show data what it want to insert, this means what NOT NULL constraint is OK.
In this case I've only one assumption what you already inserted some GROUP_GROUP_ID_COLUMN_NAME with value 2 and UNIQUE constraint is failed because of that.
Are you using a temporary table on purpose? A temporary table dies once the DB connection is closed. If this is the case, the table will get created and the insert will not correspond to any table. So the constraint 19 can correspond to 'Table not found'
You shouldn't pass null values in insert method. Pass empty strings.
I want to force a foreign key constarint on a table in an Android application.
I've searched that this can be done by using triggers:
I did it like this:
db.execSQL("CREATE TRIGGER dept_id_trigger22+" +
" AFTER INSERT "+
" OF EmployeeName ON Employees"+
" BEGIN"+
//Condition
" RAISE(ABORT,'error') END;");
but no error was raised and the illegal values are inserted.
what is wrong with this ?
Ok I got it
Android supports SQLite triggers.
The correct syntax is
db.execSQL("CREATE TRIGGER dept_id_trigger22" +
" AFTER INSERT "+
"ON Employees"+
" BEGIN"+
//Condition
" SELECT RAISE(ABORT,'error'); END;");
I forgot to add semicolon after the raise statement.
This does not execute the statement but it does not throw an exception.
still will search for how to throw exceptions
thanks
Foreign keys are only supported on Android on Froyo (2.2) or newer, for previous versions you can include them but SQLite ignores them. All Android versions of SQLite support triggers to produce the same effect though.
Newer versions of SQLite (for your PC) has a command called "genfkey" that will analyze your SQLite database (which has foreign keys in it) and produce the equivalent triggers. This way you can design your tables with foreign key constraints while also supporting all versions of the OS.
On Windows, open the SQLite command line tool with your database file as a parameter:
sqlite3 mydatabase.db
.genfkey --exec
This will generate triggers for all of your key constraints.
I don't expect any votes for this answer, just to let you know:
You could use another database, for example the H2 database. Disclaimer: I'm the main author of H2.
There are some disadvantages: some (not all) operations are slower, for example opening and closing a database. The jar file is relatively big (about 1 MB). You would have to use the JDBC API.
But the advantage is: H2 supports using triggers, constraints, and so on.
To delete Last 50 rows when count is greater than 100
sqliteDB.execSQL("CREATE TRIGGER IF NOT EXISTS delete_trigger
AFTER INSERT ON table1
WHEN (SELECT COUNT(*) FROM table1) > 50 " +
BEGIN
delete From table1 where id not in(select id from table1 order by id desc limit 100;
END;"
);
I discovered that the SQLite version used does not support foreign keys - so I expect that triggers are not supported, too.