I have a problem with the Insert into command [duplicate] - sqlite

This question already has answers here:
SQLite3 UNIQUE constraint failed error
(2 answers)
Closed 6 days ago.
Unfortunately I get an error when I wanted to put the values ​​for my attributes. I don't know exactly what the problem is. For Info : I have previously created 8 tables successfully and i use SQLite Online.Here is the picture
I hope that someone can solve my problem.

If a table has a UNIQUE constraint, it means the value of a column (or set of columns) must be unique, i.e. cannot be repeated for different rows in the table.
You're trying to insert a new record that breaks that constraint, i.e. the new row contains values in the constraint columns that already exist in the table with the same combination of values.
For example:
CREATE TABLE t1 (id INT, firstname, surname TEXT, UNIQUE (id, surname));
INSERT INTO t1 VALUES (1, 'John', 'Doe');
INSERT INTO t1 VALUES (1, 'Bob', 'Doe'); -- UNIQUE constraint failed: t1.id, t1.surname

Related

Pass array to Redshift parametrized insertion

I have the following Redshift query executed via ODBC / C++:
INSERT INTO _tmp_limit_0(val) VALUES(?);
_tmp_limit_0 is a temporary table.
I want to bind an string array to the ? parameter with SQLBindParameter to insert multiple rows in one statement.
The problem is if I query the rows in the table, it seems, that only 1 row has been inserted:
SELECT COUNT(*) AS cnt FROM _tmp_limit_0;
Can anybody help me, what did I wrong? How can I insert multiple rows in one query using SQL (the solution should work across multiple relational DB)
Thank you
Each row's data needs to be within parentheses separated with commas. Like this:
INSERT INTO table(val) VALUES (1), (2), (3);
If you want to insert multiple columns of data in multiple rows then the column information is comma separated within the parentheses. Like:
INSERT INTO table(val1, val2) VALUES (1, 9), (2, 8), (3, 7);
See - https://docs.aws.amazon.com/redshift/latest/dg/r_INSERT_30.html

How to set all values in a column to empty (null)? [duplicate]

This question already has an answer here:
How to update an entire column in sqlite? [duplicate]
(1 answer)
Closed 1 year ago.
How to set all values in a column to empty (null)?. To reset the value of a nullable column for every row in a table.
Just ignore the WHERE clause would do the trick. How to change a value in a column on a SQLite database?
UPDATE table_name SET column_name = NULL

Sqlite UNION ALL Query with Different Columns

Using UNION ALL with Queries with different number of column returns the following error sqlite3.OperationalError: SELECTs to the left and right of UNION ALL do not have the same number of result columns.
I tried
this answer but I think that is now outdated and does not work. I tried to find something in the documentation but I couldn't find it.
Both UNION and UNION ALL do not work.
This answer is a bit complex for me to understand.
What would be the workaround to achieve this? A Column with Null - how do I do that ?
Update:
Also, I don't know the no. or name of tables as in my program I allow the user to create and manipulate data.
To find out the queries in the database, I Use:
SELECT name FROM sqlite_master WHERE type='table';
and to find out the columns I use this:
[i[0] for i in cursor.description]
Simple, the column size and type for each side of a union must be identical.
You can make them identical by casting columns to the correct type, or setting missing columns to NULL.
I think you just need to compensate for the missing columns with adding 'empty' columns.
CREATE TABLE test_table1(k INTEGER, v INTEGER);
CREATE TABLE test_table2(k INTEGER);
INSERT INTO test_table1(k,v) VALUES(4, 5);
INSERT INTO test_table2(k) VALUES(4);
SELECT * FROM test_table1 UNION ALL SELECT *,'N/A' FROM test_table2;
4|5
4|N/A
Here I've added another pseudo-column with 'N/A', so the test_table2 has two columns before the UNION ALL happens.

Copy SEVERAL Column Values from One table into Another Matching IDs - SQLite

I'm elaborating on this question (Copy Column Value from One table into Another Matching IDs - SQLite) by adding an extra challenge.
The idea is to copy the value of several columns from one table to another when an id is matching. The aforementioned question addresses how to copy the content of one column when a matching id is found. Here the code as posted by #scaisEdge:
UPDATE t1
SET value = (
SELECT value
FROM t2
WHERE t1.ID = t2.ID)
But what if we want to update several columns from that same row where t1.ID = t2.ID? Of course one could run it several times once for each column to be updated, however, that's extraordinarily inefficient as I have to update millions of rows. I guess that the less amount of logical comparisons that the query has to do the faster it will be. Any other ways of optimizing this task are welcome, the IDs are unique, both tables have the same number of rows, and the exact same values of ID are found in both tables. So sorting the tables is not out of the question.
If your version of SQLite is 3.15.0+ you can do it with Row Values:
update t1 set
(col1, col2) = (
select col1, col2
from t2
where t2.id = t1.id
)
See the demo.

'rowid' column is not autoincrementing [duplicate]

This question already has answers here:
ROWID INTEGER PRIMARY KEY AUTOINCREMENT - How to insert values?
(5 answers)
Closed 6 years ago.
A similar question has been asked before, but the query is embedded in java and perhaps less clear to a total novice who would be asking this type of question.
I think the 'rowid' column is not auto-populating/auto-incrementing. Table, insert statement, and error code as follows:
CREATE TABLE T (rowid INTEGER PRIMARY KEY, c1 REAL, c2 REAL);
insert into T values (8,9);
Error code in my DB Browser is:
table T has 3 columns but 2 values were supplied: insert into T values (1,2);
I have other code that is very similar but not producing this error. Any help is greatly appreciated! Thanks!!
This isn't a SQLite problem, it's a problem with your DB browser enforcing that values for all three columns must always be present.
One option to get around this would be to insert NULL for the rowid column. This should force SQLite to assign the next logical value, which is the behavior you want anyway:
INSERT INTO T values (NULL, 1, 2);

Resources