Dynamic Way to Insert Data into SQLite Table When Column Counts Change - sql

I am working on a script using SQLite where there is a flux in the number of columns that are available to be inserted into a table I am creating to later do a join on.
The table I am created to insert the data into has 97 columns, the data coming in from my feed can range from around 80 all the way up to that 97th column.
The error I get is SQLITE_ERROR: table allPositionsTable has 97 columns but 80 values were supplied and is the one I am trying to avoid by figuring out a way where this doesn't happen.
Are there any workarounds or tricks I can use to have SQLite function so that it will always include the columns where there is no data for them or dynamically not include them so the error goes away?

The error I get is SQLITE_ERROR: table allPositionsTable has 97
columns but 80 values were supplied and is the one I am trying to
avoid by figuring out a way where this doesn't happen.
This happens because you are using the default column list (i.e. by not specifying the columns into which the values are to be placed)
That is you would be coding the equivalent of INSERT INTO your_table VALUES(.......
so in the absence of a list of columns you are saying that you will provide a value for all columns in the table and hence the message when a value or values are not present.
What you want to do is use INSERT INTO your_table_name (your_comma_separated_list_of_columns_to_be_inserted) VALUES(.......
where your_table_name and your_comma_separated_list_of_columns_to_be_inserted would be replaced with the appropriate values.
See the highlighted section of the INSERT syntax that can be found at SQL As Understood By SQLite - INSERT
and the respective section from the above link is :-
The first form (with the "VALUES" keyword) creates one or more new
rows in an existing table.
If the column-name list after table-name is
omitted then the number of values inserted into each row must be the
same as the number of columns in the table.
In this case the result of
evaluating the left-most expression from each term of the VALUES list
is inserted into the left-most column of each new row, and so forth
for each subsequent expression.
If a column-name list is specified,
then the number of values in each term of the VALUE list must match
the number of specified columns.
Each of the named columns of the new
row is populated with the results of evaluating the corresponding
VALUES expression.
Table columns that do not appear in the column list
are populated with the default column value (specified as part of the
CREATE TABLE statement), or with NULL if no default value is
specified.

Related

Add column to table with existing data in SQL Server

I have a table Rates with data in it and I need to add new column to the table however I get the error:
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'CreatedOn' cannot be added to non-empty table 'RateIncreases' because it does not satisfy these conditions
How can I do this, I have disabled prevent saving changes that required table re-creation
What part of the error do you not understand? When a column is added to an existing table, there are rows. The column is assigned to each of those rows. SQL Server has to give those rows a value. How does it do this?
It can assign the default value for the column.
It can assign NULL.
In your case, you have defined the column as NOT NULL but not provided a default value. Hence, the database does not know what to do, so it returns an error.
The simplest solution is to remove the NOT NULL constraint in the definition. Very close behind is assigning a default value.

Insertion SQL and NOT NULL values

I've created a table schema and specified that for some attributes, values cannot be null. For one column of this table, values are to be imported from a column of some another table but the problem i am facing is that when i use insert statement to copy values from that column of another table to the column of this newly created table, the attributes of this new column start screaming because they kind of have a constraint on them that while insertion their values cannot be NULL!
How do i cope with this?
One solution is that for other attributes, just for time being, i can state that null values can be accommodated so that i can successfully import values from column of other table and then later on put condition on the remaining attributes that values are not be NULL. But how do i do do this?
You need to convert NULL to some DEFAULT values while importing.
I am not sure which DB engine you are using, in mysql:
Use something like IFNULL(column_name, "").
Reference
You may simply be looking for the default clause. When you define a column, you can specify;
intcol int not null default 0
If the column is not specified for an insert, then it will default to 0. In some databases, if a NULL value is supplied, it will also get the default value.

Copy Contents of One Column Of a Table To another of a different Table SQL

I want to copy the content of one column in table A and replace the contents (not insert into it - the number of rows will be the same) of another column in another table.
I can't a where condition, the table has only just been created at this point with one empty timestamp column. it will be populated via pyodbc class after the timestamps have been added - this query will fill the timestamps for me
What is the SQL command for this?
Thanks!
After discussion, this is the query needed : INSERT INTO OCAT_test_table (DateTimeStamp) SELECT DateTimeStamp FROM DunbarGen

Empty string default values for many columns

I am working in SQL Server 2008. I have a table with many columns that will not have values (at least, for the given situation). So, they will have a NULL value when I query each of them. I would like to instead make these NULL values be empty strings (i.e., ''). What is the best way to achieve this? My current idea is to set a DEFAULT value of '' on each them at the time that the table is created. However, since there are so many of them, this will be very tedious.
You have 2 options:
As you said, give it a default value of empty string for columns you don't want to be null when you create table/add new columns.
When you select nullable columns from the table you can use IsNull(ColumnName,'') which means if ColumnName is null it'll return empty string ('').
Create a table with the same structure as your current table, with a different name, and the default value as ''.
Insert into that table from your original table.
Delete the original table.
Change the name of the new table to the original table name.

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.