I am working on Android 2.3 Simulator.
I have some 5 unique categories...
I need to add these categories in my table....
I added it using primary key like this
Create table if not exists test (category integer primary key, value text) ;
But if i execute this query twice
insert into test(category, value) values(1,"0");
Then it is adding this twice
Even if i use unique like this
Create table if not exists test (category integer unique, value text) ;
Then also it is adding the row twice...
How to make a row unique?
dont add record manually ie for primary key, you can assign primary key as auto increment, let system will add record.
Related
I have the following tables in my sqlite android database:
CREATE TABLE MAIN (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
NAME TEXT,
.....
CURRENT_CARD INTEGER
FOREIGN KEY (CURRENT_CARD) REFERENCES CARD (_ID));
CREATE TABLE CARD (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
.....
Now I want to delete records from my CARD table in the following way:
DELETE FROM CARD WHERE _ID IN (10,11,12,...);
Everything works ok until I have the record in Card table being linked to Main table, ex. let's admit that Card with id 11 has the corresponding record in Main table. When deleting Card 11 I would like to set the corresponding CURRENT_CARD field for record ex. 1 in MAIN as null (I don't want to delete the whole record in MAIN, just to null one column).
How to do that in the easiest way? If possible as one sql statement...
Use ON DELETE SET NULL action with the FOREIGN KEY.
Remember to enable foreign key support with pragma foreign_keys='ON'.
I have a table Details in which _id is the primary key by default. Now while inserting i have to make multiple inserts to different tables. for each record I have to insert one row in Details table and one record in Notifications table this happens one after the other for each record.
Now I am assigning a foreign key to the second table that is Notifications, this is the primary key of details table(Which is auto increment integer) is this the right approach? I wonder how do I extract this primary key of details table as soon as insertion in Details table is made and then supply this _id as the foreign key to my next query which will add row in the notification table.
The second approach I think of would be to assign a different primary key in Details (Which I will supply to the query explicitly), keep a record of this key and use it as a foreign key in Notifications table.
SELECT last_insert_rowid()
will allow you to pick up the _id used in any previous insertion.
Just make sure this is executed after a Details table insert.
I have a database in which i have created one table with name "SectionDetails".
In this table i have set "id" as a primary key with AUTOINCREMENT property.And i am inserting my data into this table.
However, i came to the scenario where i need to check if record i am inserting is already present or not(if record is present then replace it with same values or skip it,And if record is not present then insert new one ).
But when i tried to insert record with same column values, it increases the primary key and insert the same row again instead of replacing.
So, my question is-
Does sqlite "insert or replace" works with AUTOINCREMENT primary key?
I am using following query:
insert or replace into SectionDetails(Name,Month,Title,Url)values('Deepak',"August","None","www.dd619.com")
here column "id" is not appearing because its a primary key with AUTOINCREMENT property.
You will need to add some unique constraints to your other columns to make this work and even then you will have your IDs change.
insert or replace is really an insert with on conflict replace conflict resolution strategy. That is, when the insert would violate some constraint, all conflicting rows are first deleted and the insert takes place only then. The autoincrement mechanism will then generate a new ID value for you.
For more information: http://www.sqlite.org/lang_conflict.html
Short answer: no it does not.
The "insert or replace" only works well when you also specify the primary key. With the autoincrement in a setup that you describe, this cannot work.
Probably the quickest way to do that is by creating a custom insert/update method for your use case.
I want to use constraints in SQLite.
Scenario: Stop parent delete if child is present.
How can I achieve that?
Corrected: SQLite supports foreign key constraints, but they're off by default. You have to enable them at run time with this statement. (That means you can't set this once, and expect it to stick. If you forget to set it just once, you can delete rows from the parent and leave orphans in the child table.)
sqlite> PRAGMA foreign_keys = ON;
Details are at Enabling Foreign Key Support
SQLite can be compiled with foreign key support entirely disabled. That's something to keep in mind.
Lets say you have table A (ID, NAME) and table B (ID, A_ID, NICK_NAME)
When you create table A you'd do
create table A (
ID INTEGER PRIMARY KEY,
NAME TEXT NOT NULL UNIQUE
);
create table B (
ID INTEGER PRIMARY KEY,
A_ID INTEGER NOT NULL,
NICK_NAME TEXT,
CONSTRAINT A_FK FOREIGN KEY (A_ID) REFERENCES A (ID) ON DELETE RESTRICT
);
That should cause an error if you try to delete content from table A that is referenced by column B.
EDIT: as noted in the original poster's comment and my own following it, this apparently does not work. Anyone else shed light on this?
I have a table with a composite primary key and I am having trouble inserting. The code used to create the table is:
CREATE TABLE ClassEvent (
EventName varchar(10) NOT NULL,
CourseId varchar(10) NOT NULL,
EventType varchar(20),
EventWeight number(3),
DueDate DATE NOT NULL,
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId),
PRIMARY KEY (CourseId, EventName));
The problem I am having is when I want to insert records that have values that may not be unique for the columns CourseId or EventName, but are a unique combination of the 2.
for example, if I try to run the following 2 inserts:
INSERT INTO ClassEvent VALUES('Assignment 1','60-415','Assignment',10,'12/10/2010');
INSERT INTO ClassEvent VALUES('Project 1','60-415','Project',15,'5/12/2010');
I get the following error:
Error: columns CourseId, EventName are not unique.
and the second insert does not make it into the DB. Why does this error out? I thought that a composite primary key requires that the combination of both values are unique. In my above inserts, the values for the EventName column are different even though the values for CourseId are the same. Shouldn't this be seen as 2 unique combinations and thus 2 different primary keys?
My table needs to be able to hold several different events for each CourseId, but each Event must be unique for each Course. I need to be able to insert values into the table like:
EventName CourseId
Assignment 1 60-415
Project 1 60-415
Assignment2 60-415
Project 2 60-415
Assignment 1 60-367
Project 1 60-367
and so on. Can anyone tell me how I can get this to work? Why are these composite PK's not being seen as unique entries? Any help would be much appreciated.
Here is the java function I am using for the insert:
public void addNewClassEvent(ContentValues values) {
SQLiteDatabase db = openConnection();
db.insert("ClassEvent", null, values);
db.close();
}
Could this be causing the problem?
You can have a composite primary key in SQLite, but you
have to create the key when you create the table:
CREATE TABLE example1(
field1 FLOAT,
field2 TEXT,
PRIMARY KEY(field1, field2)
);
You cannot create the primary key after the fact using ALTER TABLE.
On the other hand, you can create a UNIQUE INDEX after the fact
which has essentially the same effect as a PRIMARY KEY:
CREATE UNIQUE INDEX pk_index ON "table1"("field1","field2");
I am not sure how you have created, the tables, and if you have added the primary index later, but grab the database to your desktop, and check out how works in a desktop environment.
You can't make combinations like that, but you don't need them. What is stopping you from just having a truly id column ?