How to delete a field or row in Spatialite GUI - sql

I am trying to find an easy way to remove columns/fields from an existing QGIS Spatialite database file. I am new to both Spatialite GUI and SQL, but I want to get the said job done. I right-clicked on a layer (for-China) and chose 'Show columns' from the context menu. Then I got an error message:
SQL error: "near "-": syntax error"
so I tried executing the statement:
PRAGMA table_info('for-China');
alter table 'for-China'
delete row 'note';
and the table showed up, but the NOTE row wasn't deleted:
I tried using COLUMN instead of ROW and also tried using DROP instead of DELETE but NOTE is still left untouched. I am confused on what to do to delete the NOTE row.

I assume that spatialite uses the same escape characters as SQLite. Hence, try double quotes:
PRAGMA table_info("for-China");
alter table "for-China" drop column note;
You should only need this for identifiers that are keywords or use characters other than alpha numeric, underscore (and perhaps a few others).
SQLite also recognizes backticks and square braces, as explained in the documentation.

Related

SQL CHECK statement error: A comma or a closing bracket was expected. (near "CHECK" )

I have made relational database model using MagicDraw tool. Every cashier has their workTime type saved in enumerator:
Then I generate DDL code and try to paste it into phpMyAdmin to generate DB but I get many errors. I tried to fix them by hand but errors are still here. So how should that check statement be written?
Errors:
A check constraint isn't part of a column's definition, it's its own clause in the table definition. You need a comma between the worktime char(13) not null and the check.
I think I have found a solution.
A book that I was following to create DDL from MagicDraw project suggested to pick "Check Constraints" as an "Enumeration transformation strategy" when transforming model. It didnt worked that way so I have chosen "Lookup tables" instead and DDL code seems to be working now.
However it still had some erorrs. I had to add semicolons after all INSERT statements.
And I also had to delete word "precision" after every 'double' type:
It works now.

Access Reserved Symbols Workaround not working as expected

I would like to add a field with:
ALTER TABLE Table_name ADD COLUMN ABCD012345(en) Memo;
Here is described what to do in that case:
To work arond this problem, do not use special characters. If you must use special characters in query expressions, enclose the special characters in brackets ([]). For example, if you want to use the greater than sign (>), use [>].
As I understand it I would Need to Change it to:
ALTER TABLE Table_name ADD COLUMN ABCD012345[(]en[)] Memo;
But this still leads to the same error: Syntax error in field Definition. If you Need a little more context: I do this with a ADODB.Recordset in a VBA macro.
Where do I go wrong? How can I solve it?
You need to surround the entire name with brackets, not just the special characters:
ALTER TABLE Table_name ADD COLUMN [ABCD012345(en)] Memo;
-- Here --------------------------^--------------^

table with "." in its name

I was trying to use sqlFetch. The fetch works perfectly when I change the name of my table to have underlines instead of periods. So if I use the command
sqlFetch(conn, "HelloWorld_40")
It works fine. Unfortunately, my friends are all using the real name of the table
sqlFetch(conn, "HelloWorld.40")
But then it crashes and it tells me that
Error in sqlColumns(conn, "HelloWorld.40") :
'HelloWorld.40': table not found on channel
I'm guessing the period "." is illegal name for a table. But I don't want my friends to change it because it's a lot of people who would be affected. Is there a way I can call the table, or do I have to secretly go to their database, change the name while I use it and then change it back to a period (risking that I will forget, someone will read, blah blah).
Thanks.
put the table name in square brackets:
[HelloWorld.40]
It is a problem with sqlFetch which parse table name. Unfortunately it did not handle table quotes, so it's search for table 40 in schema HelloWorld. You need to directly call sqlQuery (with quoted table name, brackets for MS SQL Server):
sqlQuery(dbhandle, "SELECT * FROM [HelloWorld.40]")
Side note: you should specify which database you are using.
The best delimiter is double quotes -- that should work in most underlying databases:
"HelloWorld.40"
In MySQL, you can also use back ticks (`):
`HelloWorld.40`
In SQL Server, Access, and I think Sybase, you can also use square braces:
[HelloWorld.40]

How to configure PL/SQL Developer IDE to leave the columns names as they are?

I have created a table with some columns using PL/SQL Develper to access this Oracle database.
After clicking the "Apply" button that will create the table, all my column names suddenly become upper case (which can hardly be read), including the table name!
Would that be possible to configure PL/SQL Developer IDE to leave the columns names and table names as they are?!
Oracle DBMS ignores case and translates everything to uppercase unless you use quotes. But the quotes have the liability that you always have to use them.
If you do not use the quotes you will get an error message ORA-00942: table or view does not exist
You can give column name in double quote "". Then those column name will be in as it is.

Informix: Update Statement Error In WebSphere

I am trying to run this update statement but informix doesn't allow me to.
I have a table, named ITEMS and below I have selected some records from it.
SELECT SHORT_SKU, ITEMS."STYLE" FROM ITEMS;
SHORT_SKU STYLE
--------- -----
01846173 null
01811752 null
01811748 null
Trying to run the below UPDATE statement, informix says syntax error.
UPDATE ITEMS SET ITEMS."STYLE" = 'M' WHERE SHORT_SKU = '01846173';
^ syntax error here
Then I changed (as below) and got "Column (style) not found in any table in the query (or SLV is undefined)."
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
How do I update the "STYLE" field?
UPDATE 1
I did a change to one of WAS data source's custom properties, ifxDELIMIDENT. Originally it was blank. So, I changed it to true. Restarted WAS. And I couldn't login to our application. SQLExceptions were thrown by WAS but was not able to see the stack trace because WAS has truncated the last few lines. After changing the property back to blank, I was able to login to our application.
I tried another approach, which was to write a Java class that updates the ITEMMST.STYLE column. I executed this from a shell script. In the shell script, I defined and exported the variable DELIMIDENT with the value 'Y'. But I am still getting 'Syntax error'.
UPDATE 2
I managed to update the column. This is done by adding the 'DELIMIDENT=Y' property at the end of the connection string which will be passed to the DriverManager object when opening the database connection.
But, this won't work for our web application because it uses the WebSphere data source to create the db connection. It would be super if there's a way to set this property in the Informix environment itself.
Try:
UPDATE ITEMS SET "STYLE" = 'M' WHERE SHORT_SKU = '01846173';
It must be that STYLE is a reserved word so you must double-quote it to refer to the column. But standard UPDATE syntax doesn't allow you to prefix column names with the table name in the SET clause (since you can only be updating the columns of one table: the table mentioned in the UPDATE).
The right Syntax would be
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
As stated on IBM Documentation but as STYLE is a reserved word i guess your getting problems, Read IBM Recommendation on this.
Anyway you may find a work arround oat this link, otherwise you may consider changing the column name.
I am not aware of STYLE being a keyword in Informix (but I haven't gone to look for it). However, you can usually use keywords as column names etc without much problem.
If you must quote it, you need to set the DELIMIDENT environment variable - the value doesn't matter, but use DELIMIDENT=1 for concreteness. This enables SQL standard 'delimited identifiers', where double quotes surround identifiers (column names, table names, etc) and single quotes surround strings. (Normally, you can use either single quotes or double quotes around strings.)
One other point: if you use delimited identifiers, they also become case-sensitive (whereas normally, identifiers are case-insensitive). So you need to know how the STYLE column is stored in the system catalog. In most databases, they will be in lower-case. There's an outside chance that in a MODE ANSI database, they are stored in upper-case (but it is a while since I looked to make sure).
Use this query:
UPDATE ITEMS SET ITEMS.STYLE = 'M' WHERE SHORT_SKU = '01846173';
I think double quotes not required for column name.
Updated Answer 1:
Error Description-
-217 Column column-name not found in any table in the query
(or SLV is undefined).
The name appears in the select list or WHERE clause of this query but is
not defined in a table and does not appear as a statement local variable
(SLV) definition. Check that the column name or SLV name and the names of
the selected tables are spelled as you intended.
If all names are spelled correctly, you are not using the right tables,
the database has been changed, or you have not defined the SLV. If the
name not found is a reference to a column, that column might have been
renamed or dropped. If the name not found represents an SLV and you
defined the SLV in the statement, make sure that the SLV definition
appears before all other references to that SLV name.
This error message can also appear during the execution of an ALTER TABLE
statement when the engine tries to update views that depend on the table.
More info link
Updated Answer 2:
If not possible to change column name then get more information about SLV.
You can refer following links for description and use of SLV:
link1
link2
link3
There are 2 solutions for this.
Set the Informix JDBC data source 'ifxDELIMIDENT' property to 'true'
Rename the affected table columns
For the 1st option, we had a problem after setting the data source to 'true'. Suddenly all our queries did not work. After much troubleshooting, we found out that by setting the 'ifxDELIMIDENT' property to 'true', it also changed Informix to be case sensitive. In our Java code, we have all the column names in uppercase, and in Informix (Example: resultSet.getString("STYLE")), but the table column names are lowercase (Example: 'style'). This was why after changing this property, we were not able to login to our application Unfortunately this behavior was not documented anywhere in IBM's Info Centre nor in the internet.
We opted for the 2nd option which involved changing the affected column names to another column name (Example: Changed 'STYLE' to 'ITEM_STYLE').