Insert boolean value into sqlite table - sqlite

I created a table MYTABLE
CREATE TABLE "MYTABLE" (
"surname" VARCHAR,
"name" VARCHAR,
"id" INTEGER PRIMARY KEY NOT NULL ,
"flag" BOOL);
when I insert a record with:
INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", true);
I get an error message, that no such column: true. If I use this:
INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", "true");
I don't get any error, but when i read that record with rs.getBoolean("flag") I get false.
Finally, i tried this
INSERT INTO "MYTABLE" VALUES ("Super","Mario","94", 1);
the rs.getBoolean("flag") returns true. So the lesson here is that the boolean values in Sqlite are inserted with 0/1 ?

SQLite does not have a separate Boolean storage class.Boolean values are stored as integers 0 and 1.
source

Yes, the BOOL type is synonymous to a BIT in many databases, including SQLite and SQL Server. Other databases, like Oracle, do not even have a boolean type and a NUMBER(1) field is used to store boolean values by convention.

Related

why content_rowid must be integer in FTS?

Why content_rowid must be integer in FTS?
And what if content_rowid be a not primary integer key?
I create a VIRTUAL TABLE and TRIGGER width content_rowid equal to a not primary integer key, it works, has there any hidden risk?
CREATE TABLE tbl (a, createTime INTEGER NOT NULL);
CREATE VIRTUAL TABLE fts USING fts5(a, content=tbl, content_rowid=createTime);
CREATE TRIGGER IF NOT EXISTS tbl_ai AFTER INSERT ON tbl
BEGIN
INSERT INTO fts (rowid, a)
VALUES (new.createTime, new.a);
END;
Is there anywayt to set content_rowid to a text column?I have tried,throw error dataType mismatch
CREATE TABLE tbl (a, guid text NOT NULL PRIMARY KEY);
CREATE VIRTUAL TABLE fts USING fts5(a, content=tbl, content_rowid=guid);
CREATE TRIGGER IF NOT EXISTS tbl_ai AFTER INSERT ON tbl
BEGIN
INSERT INTO fts (rowid, a)
VALUES (new.guid, new.a);
END;
Your virtual table explicitly references tbl.createTime, which is an integer. The resulting virtual column is of the same type, necessarily.

sqlite integer primary key not null constraint failed

According to the SQLite documentation / FAQ a column declared INTEGER PRIMARY KEY will automatically get a value of +1 the highest of the column if omitted.
Using SQLite version 3.22.0 2018-01-22 18:45:57
Creating a table as follows:
CREATE TABLE test (
demo_id INTEGER PRIMARY KEY NOT NULL,
ttt VARCHAR(40) NOT NULL,
basic VARCHAR(25) NOT NULL,
name VARCHAR(255) NOT NULL,
UNIQUE(ttt, basic) ON CONFLICT ROLLBACK
) WITHOUT ROWID;
Then inserting like this:
INSERT INTO test (ttt, basic, name) VALUES ('foo', 'bar', 'This is
a test');
gives:
Error: NOT NULL constraint failed: test.demo_id
sqlite>
When it is expected to create a record with a demo_id value of 1. Even if the table already contains values, it'll fail inserting the row without explicitly specifying the id with the same error.
What am I doing wrong?
The documentation says that you get autoincrementing values for the rowid. But you specified WITHOUT ROWID.

sqlite with boolean values?

i am getting some text and boolean values from server i need to save them in database.
this is my table . I defined boolean values as INTEGER couse in sqlite there is no boolean.
db.execSQL("CREATE TABLE outcomesStore(id INTEGER PRIMARY KEY AUTOINCREMENT , allowgo INTEGER,cod TEXT,youdidComments INTEGER, youwent INTEGER,ByDate INTEGER ," +
"OnCompletion INTEGER,yourtext TEXT , yourGroup TEXT, yourConsultation INTEGER )");
and i am getitng these values from server.
Store[] Storedata = Configuration.getStore();
booleanvalues[0] = Store[0].isallowgo ();
and inserting like this
helperdatabase = new DatabaseHelperInurseBusiness(this);
db = helperdatabase.getWritableDatabase();
ContentValues insertOutcomes = new ContentValues();
insertOutcomes.put(helperdatabase.ALLOW_GO,booleanvalues[0]);
db.insert("outcomesStore", helperdatabase.ALLOW_GO,insertOutcomes);
Its not working even not giving any error.
Actually, SQLite does support BOOLEAN type, but may be not exactly in the way you expect.
You can create column of BOOLEAN type using standard CREATE TABLE, and then populate it:
CREATE TABLE mytable (name VARCHAR(10), flag BOOLEAN);
INSERT INTO mytable (name, flag) VALUES ('Alice', 0);
INSERT INTO mytable (name, flag) VALUES ('Bob', 1);
Then you can get your data back, and use standard BOOLEAN logic while doing so:
SELECT * FROM mytable WHERE flag
or using different BOOLEAN expressions:
SELECT * FROM mytable WHERE NOT flag
and so on. (Obligatory SQLFiddle)
In other words, it all works great, the only catch is that you must use 0 instead of FALSE and 1 instead of TRUE (this includes trying to set values from client software). Note that this is somewhat similar to other SQL engines (For example, PostgreSQL supports using '0'/'1', 'f'/'t' and false/true for setting FALSE/TRUE values by client software).
Also, if you were to use this BOOLEAN field in numeric context (like adding or multiplying) it will behave as number 0 or 1, while in other SQL engines adding BOOLEAN and INTEGER may cause an exception because of incompatible types.
i got the solution.
Thanks Yaqub Ahamad.
insertOutcomes.put(DatabaseHelperInurseBusiness.ALLOW_GO,storedata.isAllowGo()== true ? 1:0);

How to autogenerate the username with specific string?

I am using asp.net2008 and MY SQL.
I want to auto-generate the value for the field username with the format as
"SISI001", "SISI002",
etc. in SQL whenever the new record is going to inserted.
How can i do it?
What can be the SQL query ?
Thanks.
Add a column with auto increment integer data type
Then get the maximum value of that column in the table using "Max()" function and assign the value to a integer variable (let the variable be 'x').
After that
string userid = "SISI";
x=x+1;
string count = new string('0',6-x.ToString().length);
userid=userid+count+x.ToString();
Use userid as your username
Hope It Helps. Good Luck.
PLAN A>
You need to keep a table (keys) that contains the last numeric ID generated for various entities. This case the entity is "user". So the table will contain two cols viz. entity varchar(100) and lastid int.
You can then have a function written that will receive the entity name and return the incremented ID. Use this ID concatenated with the string component "SISI" to be passed to MySQL for insertion to the database.
Following is the MySQL Table tblkeys:
CREATE TABLE `tblkeys` (
`entity` varchar(100) NOT NULL,
`lastid` int(11) NOT NULL,
PRIMARY KEY (`entity`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The MySQL Function:
DELIMITER $$
CREATE FUNCTION `getkey`( ps_entity VARCHAR(100)) RETURNS INT(11)
BEGIN
DECLARE ll_lastid INT;
UPDATE tblkeys SET lastid = lastid+1 WHERE tblkeys.entity = ps_entity;
SELECT tblkeys.lastid INTO ll_lastid FROM tblkeys WHERE tblkeys.entity = ps_entity;
RETURN ll_lastid;
END$$
DELIMITER ;
The sample function call:
SELECT getkey('user')
Sample Insert command:
insert into users(username, password) values ('SISI'+getkey('user'), '$password')
Plan B>
This way the ID will be a bit larger but will not require any extra table. Use the following SQL to get a new unique ID:
SELECT ROUND(NOW() + 0)
You can pass it as part of the insert command and concatenate it with the string component of "SISI".
I am not an asp.net developer but i can help you
You can do something like this...
create a sequence in your mysql database as-
CREATE SEQUENCE "Database_name"."SEQUENCE1" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 001 START WITH 21 CACHE 20 NOORDER NOCYCLE ;
and then while inserting use this query-----
insert into testing (userName) values(concat('SISI', sequence1.nextval))
may it help you in your doubt...
Try this:
CREATE TABLE Users (
IDs int NOT NULL IDENTITY (1, 1),
USERNAME AS 'SISI' + RIGHT('000000000' + CAST(IDs as varchar(10)), 4), --//getting uniqueness of IDs field
Address varchar(150)
)
(not tested)

counting rows of sqlite INSERT SELECT

I have two sqlite tables, where one table has a foreign key of the other.
CREATE TABLE a (id INTEGER PRIMARY KEY NOT NULL, value TEXT UNIQUE NOT NULL);
CREATE TABLE b (id INTEGER PRIMARY KEY NOT NULL, a INTEGER REFERENCES a (id) NOT NULL, value TEXT NOT NULL);
I am doing an INSERT with a SELECT into b.
INSERT INTO b (a, value) SELECT ?value, a.id FROM a WHERE a.value == ?a;
How do I know weather a row was inserted into b or not? Doing a SELECT for the just inserted values and checking weather they exist, seems rather inefficient.
I hope the changes() function can help you.
The changes() function returns the number of database rows that were
changed or inserted or deleted by the most recently completed INSERT,
DELETE, or UPDATE statement, exclusive of statements in lower-level
triggers. The changes() SQL function is a wrapper around the
sqlite3_changes() C/C++ function and hence follows the same rules for
counting changes.
So changes() returns 1 if a row was inserted and 0 otherwise.

Resources