SQL LITE : one row constraint - sqlite

I am creating a table in SQLite Database which will only store a single data, else if data exists in the table, it should be deleted before inserting a new data.
This is the syntax i used to create the table
CREATE TABLE my_login_info (
id BOOLEAN PRIMARY KEY
DEFAULT True
CONSTRAINT one_row_only CHECK (id)
NOT NULL,
username STRING,
password STRING
);
however when i am trying to insert the data to the table while the table is empty, it gave an error.
Error while committing new row: CHECK constraint failed: one_row_only
Any idea what is the caused the problem?

You can use INSERT OR REPLACE to insert a new record or update the existing one that matches the key field, eg:
INSERT OR REPLACE INTO my_login_info (id,username,hash)
VALUES (true,someuser,somehash)
Just make sure you don't store the password as cleartext. Only store a strong hash. If you don't need the hash for authentication, you could get away with storing a short hash, ie only part of the full hash.
That's what git does for example, when it displays a short hash for each file version instead of the full hash

Related

How do I fix the error: "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints"

When I set up my SQLite DB and try to get UIPath to read it, I get this error: "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints"
What can I do to get UIPath to read this?
UIPath may produce this error when you try to run a query
“Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints”
The query may work in the command line of the database but not in UIPath but this is deceptive as this is NOT a UIPath issue. It is an issue with the database table itself. The table elements must be a type that UIPath can use.
For instance, one of the issues documented:
SQLite will allow you to create the database table like this
 create table clients (name text null, email text null, goodstanding text null);
While this is allowable in the database and it will work, UIPath will not be able to read these rows. Instead, create the table like this:
 create table clients (name string null, email string null, goodstanding string null);
UIPath will not know how to handle a variable type “text”
Once you make the table of type string (or other accepted varialbes int, etc) you will be able to use this in UIPath

Phonegap SQLite id unique is null

I'm not sure that I'm getting an id primary key in my sqlite table.
When I create my table I use
CREATE TABLE IF NOT EXISTS HABITS (id unique, uid, text, orglvl, habitstatus)
The other properties are fine, and I can retrieve results.rows.item(i).text, but results.rows.item(i).id is NULL
Also upon Insert, when I try to get results.insertId, I get an error of "undefined", which started this whole wondering for me.
What am I doing wrong - how come I have no id's when I have created them in my table?
To get an autoincrementing column, you must use INTEGER PRIMARY KEY.

How to check that email address isn't already in use when updating it

In my website, I have allowed my users to update their email address. However, I require that each email address be unique. How can I ensure that the email address which is entered on the update form is unique before updating it in the database?
Have a unique constraint on the column that stores your email address.
You can use UNIQUE constraints to make sure that no duplicate values
are entered in specific columns that do not participate in a primary
key.
Blindly inserting in a table with unique constraints will throw an exception. To avoid this, run a select query checking for this email address in the where clause.
If you have a large user base, be sure to index your email column.
Assuming you have some key on the table, and your initial statement was:
UPDATE dbo.foo SET Email = #Email WHERE Key = #key;
Then you can check for values first:
IF EXISTS (SELECT 1 FROM dbo.foo WHERE Email = #Email AND Key <> #Key)
BEGIN
RAISERROR('Sorry, that e-mail is already in use.', 11, 1);
END
ELSE
BEGIN
UPDATE dbo.foo SET Email = #Email WHERE Key = #key;
END
Now, you should have a constraint on the table anyway, e.g.
ALTER TABLE dbo.foo ADD CONSTRAINT UQ_Email
UNIQUE(Email);
But it can still be beneficial to check for violations first, even with a constraint in place, as I demonstrated here.
My first recommendation would be defining this as a unique column in the table, see this. Alternatively, and not in my opinion a very good way to do this (but maybe for some reason you don't control the tables) you could check this in your insert statement. If you can provide more information (table structure / insert statement) I will be able to provide a better answer for you.

How to merge N SQLite database files into one if db has the primary field?

I have a bunch of SQLite db files, and I need to merge them into one big db files.
How can I do that?
Added
Based on this, I guess those three commands should merge two db into one.
attach './abc2.db' as toMerge;
insert into test select * from toMerge.test
detach database toMerge
The problem is the db has PRIMARY KEY field, and I got this message - "Error: PRIMARY KEY must be unique".
This is the test table for the db.
CREATE TABLE test (id integer PRIMARY KEY AUTOINCREMENT,value text,goody text)
I'm just thinking off my head here... (and probably after everybody else has moved on, too).
Mapping the primary key to "NULL" should yield the wanted result (no good if you use it as foreign key somewhere else, since the key probably exists, but has different contents)
attach './abc2.db' as toMerge;
insert into test select NULL, value, goody from toMerge.test;
detach database toMerge;
actual test:
sqlite> insert into test select * from toMerge.test;
Error: PRIMARY KEY must be unique
sqlite> insert into test select NULL, value, goody from toMerge.test;
sqlite> detach database toMerge;
I'm not 100% sure, but it seems that I should read all the elements and insert the element (except the PRIMARY KEY) one by one into the new data base.

SQLITE: Unable to remove an unnamed primary key

I have a sqlite table that was originally created with:
PRIMARY KEY (`column`);
I now need to remove that primary key and create a new one. Creating a new one is easy, but removing the original seems to be the hard part. If I do
.indices tablename
I don't get the primary key. Some programs show the primary key as
Indexes: 1
[] PRIMARY
The index name is typically in the [].
Any ideas?
You can't.
PRAGMA INDEX_LIST('MyTable');
will give you a list of indices. This will include the automatically generated index for the primary key which will be called something like 'sqlite_autoindex_MyTable_1'.
But unfortunately you cannot drop this index...
sqlite> drop index sqlite_autoindex_MyTable_1;
SQL error: index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped
All you can do is re-create the table without the primary key.
I the database glossary; a primary-key is a type of index where the index order is typically results in the physical ordering of the raw database records. That said any database engine that allows the primary key to be changed is likely reordering the database... so most do not and the operation is up to the programmer to create a script to rename the table and create a new one. So if you want to change the PK there is no magic SQL.
select * from sqlite_master;
table|x|x|2|CREATE TABLE x (a text, b text, primary key (`a`))
index|sqlite_autoindex_x_1|x|3|
You'll see that the second row returned from my quick hack has the index name in the second column, and the table name in the third. Try seeing if that name is anything useful.

Resources