Force Delete on a UNIQUE rows SQLite FMDB iOS - ios

I have an app which uses SQLite, i have some problems deleting a row because one of its value is unique.
I get the error :
Unknown error calling sqlite3_step (19: column myField is not unique)
DB Query: DELETE FROM "****" WHERE "myField" = '72645DA9-E95F-452A-94B8-C93A20D9A41C'
Is there a way to force the delete of this unique row ? Or can simply disable the unique constraint when i need to delete.
Of course this behavior should not appear, but it does so please do not tell to re-create my database :)
Thank you very much.

Related

How to handle update_all when empty data returned

I am updating some of my database details using update all. When the returned data is empty update all fails ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column
I know this can be fixed for example, but this looks like a redundant use, because in my code base I am doing many changes as follows
Product.where(local_product: true).update_all(tax: 0) if Product.where(local_product: true).present?
I wish if update_all had handled this by itself, or is there any other way?
Just do it in one line you dont need to check if there are any records.
Product.where(local_product: true).update_all(tax: 0) will generates next sql query:
UPDATE "products" SET "tax" = true WHERE "products"."local_product" = true
Database filters records for you, you dont need to check it twice, if there are no data for update it will do nothing.
Try this to avoid redundant
#products = Product.where(local_product: true)
#products.update_all(tax: 0) if #products.present?

Cannot delete a mnesia table that I know exists

I have a mnesia table that I am trying to delete. However, when I try to run :mnesia.delete(TableName) I get this error back {:aborted, {:no_exists, TableName}}
When I try to create the same table by running :mnesia.create_table(TableName, [attributes: [:id, :att1, :att2], disc_copies: [Node.self()]]) I get this back {:aborted, {:already_exists, TableName}}
I can still see the .DCD file for the table after a delete, what causes this and how can I fix it?
Note: The code is in an Elixir codebase.
Edit: When my application starts, I try to delete and recreate that table, even if it exists.
:mnesia.delete/1 looks for a key to delete in the given table (and takes a tuple {Table, Key}).
You probably want :mnesia.delete_table/1 which will delete the table itself.
Docs for more: http://erlang.org/doc/man/mnesia.html#delete_table-1

Can't view newly created records after database sequence reset

So I had some problems with my database. I have an import functionality which I used to import a few thousand records. Now I was developing locally (SQLite) and everything worked fine. I pushed my updates to heroku where I have a PostgreSQL database. I ran my import there and it appeared to work well too. Now, I'm trying to manually create an other record in that table and I kept getting the error that the unique ID was a duplicate. So, after some online searching I created this extension to active record:
def self.reset_pk_sequence
case ActiveRecord::Base.connection.adapter_name
when 'SQLite'
new_max = maximum(primary_key) || 0
update_seq_sql = "update sqlite_sequence set seq = #{new_max} where name = '#{table_name}';"
ActiveRecord::Base.connection.execute(update_seq_sql)
when 'PostgreSQL'
ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
else
raise "Task not implemented for this DB adapter"
end
end
I ran it on my table, and the ID is now counting up from where it should (3000 something instead of 1,2,3, etc).
My problem now is that I am able to create new records in my table, and I see them in my index page. However, when I try to open that specific record, I get this error:
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
The link from my index is just to the ID of the record. When I check if this ID exists in the console, I'm perfectly able to find the record. What's going wrong here?
I figured it out. I had some empty associations that I was trying to display in the view. The error threw me off since it appeared like something was wrong with my record.

SQL Insert Update Issue in Procedure

This is my first post to this website.
Here is my question: I want to use the same stored procedure for Insert and Update.
For Insert there is no problem. But at the time of update O want to update it based on my primary key, not on any unique column value.
Here is my current Procedure.
IF EXISTS(SELECT MemberID FROM MEMBER WHERE MEMBERSHIPNO=#MEMBERSHIPNO)
WHERE MEMBERSHIPNO=#Membershipno
I want to use WHERE MemberID=#MemberID //Memberid is primary key
Please Help.
I'm not sure if I understand you're question quite clearly, but from what I understand you're trying to do a IF clause that would UPDATE if said credentials exists.
Something like this would do the trick, just fill in the logic and it should all work out.
IF EXISTS (SELECT MemberID FROM MEMBER WHERE MEMBERSHIPNO=#MEMBERSHIPNO)
UPDATE [ADD UPDATE LOGIC HERE] WHERE MemberID=#MemberID
ELSE
INSERT [ADD INSERT LOGIC HERE]
Hope this helps.

SQLite - Foreign Keys Contraints - IOs 5

it seems that Foreign Keys Constraints are supported since version 3.6.x in SQLite. The version of SQLite on IOS5.0 is 3.7.7 (found in sqlite3.h).
But when I try to insert a row in a table that has a constraint, my row is correctly inserted even if the related foreign key is not existing. I have no error.
Doing the same insert statement using apps like Navicat gives me a "Constraint violation error"
Do you know if foreign keys are supported on IOs 5 ?
Here is the Database Schema:
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
)
CREATE TABLE "track" (
"trackid" INTEGER PRIMARY KEY AUTOINCREMENT,
"trackname" TEXT,
"trackartist" INTEGER,
CONSTRAINT "trackartist" FOREIGN KEY ("trackartist") REFERENCES "artist" ("artistid") ON DELETE CASCADE ON UPDATE CASCADE)
Really simple, isn't it ?
Thanks
Emmanuel
Foreign keys are disabled by default. You have to enable them separately for each connection. The setting isn't "sticky". You have to do this every time you connect to a SQLite database.
PRAGMA foreign_keys = ON;
Odds are good that Navicat takes care of that for you. In your own code, it's your job.
I've finally found the solution .... and catcall you were right.
By default the sqlite database is opened with foreign keys option disabled, even if the tables have been built with foreign keys constraints !
So just make this simple request :
PRAGMA foreign_keys=ON;
just after opening the database, and outside a transaction (if you are using fmdatabase and transactions functionality)
Hope this will help somebody else.
Emmanuel
This is how I do it using FMDB:
[database executeUpdate:#"PRAGMA foreign_keys=ON"];
It is on the doc:
SQLite Doc
and when you open the connection:
[database executeUpdate:#"PRAGMA foreign_keys=ON"];

Resources