'rowid' column is not autoincrementing [duplicate] - sqlite

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);

Related

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

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

How to filter rows by using datetimes present in the row and datetime('now') [duplicate]

This question already has answers here:
SQLite DateTime comparison
(14 answers)
Closed 3 months ago.
Given the table Use, defined as
CREATE TABLE Use(date TEXT NOT NULL, id TEXT PRIMARY KEY);
I want to make a query to find "All uses before today", however SQLite is not behaving as I expect and return the wrong value.
I tried to query using my table as such:
SELECT * FROM Use WHERE date <= datetime('now')
The problem here is that date might not be in the same format that datetime returns.
You need first to convert the date to use the same format, by using the datetime function on it as well, and then you can compare both correctly:
SELECT * FROM Use where datetime(date) <= datetime('now')

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.

convert rows to columns ... how do you do this? [duplicate]

This question already has an answer here:
How to convert column values into rows in Sqlite?
(1 answer)
Closed 8 years ago.
Suppose you have a three-column table named scoreTotals. It has the weekly points totals for three players.
If you ran this query on scoreTotals:
select *
from scoreTotals;
You would get this:
Jones Smith Mills
50 70 60
How do you reconfigure the output to the end user so it's this way:
player points
Jones 50
Smith 70
Mills 60
The trick is to get the column titles to appear on the left hand side as actual data fields, rather than the titles of the columns.
I saw some things on StackOverflow relating to how to turn columns into rows, but none addressed this exact question, and my attempts to adjust the other ideas to my circumstance did not work.
It needs to work in sqlite, which means the pivot and unpivot keywords won't work. I'm looking to do this without storing a table to the database and then deleting it afterward.
The following code will generate the table I am trying to operate on:
create table scoreTotals(Jones int, Smith int, Mills int);
insert into scoreTotals values (50, 70, 60);
I had a similar problem and my solution depends on which programming language you might be using to process sqlite commands. In my case I am using python to connect to sqlite. After I do a select to return records, I store the result set into a "list of lists" (aka table) which I can then transpose (aka unpivot) with the following single line of code in python:
result = [[row[idx] for row in table] for idx in xrange(len(table[0]))] # transpose logic using list comprehension
SQLite does not have an unpivot command, but this solution by bluefeet for MySQL, may also work for SQLite:
MySQL - turn table into different table

Resources