SQLite CREATE TRIGGER ... SQLite error 1 with statement: incomplete input - sqlite

trying to create a trigger in Xcode / Swift5 / GRDB. the trigger in the codeblock below fails silently, so I move it and see the error in the title [I have removed the text of the SQL statement from the title]. the weird part is that DB Browser will run the SQL and CREATE the trigger just fine. and the app responds as expected with the externally created trigger. and the UPDATE within seems to work fine outside of the trigger.
opened an issue with the GRDB github repo...just passing the raw SQL thru. check several validators online. all either said there was nothing wrong, or failed on CREATE TABLE in the block below...so no faith in any of the validators.
remembered I have DraftCode on the ipad, with phpLiteAdmin included. send the SQL over and get the same incomplete input error. move back to the desktop...update PHP, and get phpLiteAdmin...same error.
so...it's either a SQLite bug like https://github.com/sqlitebrowser/sqlitebrowser/issues/1470? or more likely a syntax error on my part. I've never played with triggers before...so I may be the dumbass overlooking the obvious. if so, please help me rectify.
one more bit of interesting... in phpLiteAdmin, using the trigger builder, everything works just fine. but trying to run the resulting create SQL statement, copied/delete trigger/and pasted back in, generates the same error
SQLite version: 3.31.1
PHP version: 7.3.14
phpLiteAdmin version: 1.9.8.2
any help with creating the trigger in either GRDB or phpLiteAdmin as a SQL statement would be appreciated. thnx.
CREATE TABLE CartItem (
id INTEGER PRIMARY KEY AUTOINCREMENT,
transactionID INTEGER DEFAULT NULL,
wasReceived BOOLEAN,
sku VARCHAR,
label VARCHAR,
description VARCHAR,
unitLabel VARCHAR,
cost DOUBLE,
wholesale DOUBLE,
retail DOUBLE,
tax DOUBLE,
cartQty FLOAT,
modified REAL
);
-- because 'transaction' is reserved
CREATE TABLE TransXion (
id INTEGER PRIMARY KEY AUTOINCREMENT,
transactionNum VARCHAR,
shippingSubtotal DOUBLE,
taxableSubtotal DOUBLE,
nontaxableSubtotal DOUBLE,
subtotal DOUBLE,
total DOUBLE,
paymentsSubtotal DOUBLE,
balance DOUBLE,
itemsWereRcvd BOOLEAN,
locked BOOLEAN,
modified REAL
);
CREATE TRIGGER insert_TransXion_Dollar_Values_trigger
AFTER INSERT ON CartItem
WHEN transactionID = new.transactionID
BEGIN
UPDATE TransXion
SET total = (
SELECT SUM(ci.retail) AS retail_total
FROM CartItem ci
WHERE ci.transactionID = TransXion.id
)
WHERE id IN (
SELECT transactionID
FROM CartItem ci
WHERE ci.transactionID = TransXion.id
);
END;

In phpLiteAdmin's SQL tab, statements are separated by the delimiter that you can configure below the input tab. By default, this is ;.
In your trigger definition you use ;, but splitting what you enter at this point results in those two queries:
First query:
CREATE TRIGGER insert_TransXion_Dollar_Values_trigger
AFTER INSERT ON CartItem
WHEN transactionID = new.transactionID
BEGIN
UPDATE TransXion
SET total = (
SELECT SUM(ci.retail) AS retail_total
FROM CartItem ci
WHERE ci.transactionID = TransXion.id
)
WHERE id IN (
SELECT transactionID
FROM CartItem ci
WHERE ci.transactionID = TransXion.id
);
second query:
END;
So this obviously fails. You can simply configure another delimiter like $ to make sure it executes as one query.

Related

using an expression as table name in sqlite

I am trying to check if a table exists prior to send a SELECT query on that table.
The table name is composed with a trailing 2 letters language code and when I get the full table name with the user's language in it, I don't know if the user language is actually supported by my database and if the table for that language really exists.
SELECT name FROM sqlite_master WHERE name = 'mytable_zz' OR name = 'mytable_en' ORDER BY ( name = 'mytable_zz' ) DESC LIMIT 1;
and then
SELECT * FROM table_name_returned_by_first_query;
I could have a first query to check the existence of the table like the one above, which returns mytable_zz if that table exists or mytable_en if it doesn't, and then make a second query using the result of the first as table name.
But I would rather have it all in one single query that would return the expected results from either the user's language table or the english one in case his language is not supported, without throwing a "table mytable_zz doesn't exist" error.
Anyone knows how I could handle this ?
Is there a way to use the result of the first query as a table name in the 2nd ?
edit : I don't have the hand of the database itself which is generated automatically, I don't want to get involved in a complex process of manually updating any new database that I get. Plus this query is called multiple times and having to retrieve the result of a first query before launching a second one is too long. I use plain text queries that I send through a SQLite wrapper. I guess the simplest would rather be to check if the user's language is supported once for all in my program and store a string with either the language code of the user or "en" if not supported, and use that string to compose my table name(s). I am going to pick that solution unless someone has a better idea
Here is a simple MRE :
CREATE TABLE IF NOT EXISTS `lng_en` ( key TEXT, value TEXT );
CREATE TABLE IF NOT EXISTS `lng_fr` ( key TEXT, value TEXT );
INSERT INTO `lng_en` ( key , value ) VALUES ( 'question1', 'What is your name ?');
INSERT INTO `lng_fr` ( key , value ) VALUES ( 'question1', 'Quel est votre nom ?');
SELECT `value` FROM lng_%s WHERE `key` = 'question1';
where %s is to be replaced by the 2 letters language code. This example will work if the provided code is 'en' or 'fr' but will throw an error if the code is 'zh', in this case I would like to have the same result returned as with 'en' ....
Not in SQL, without executing it dynamically.. But if this is your front end that is running this SQL then it doesn't matter so much. Because your table name came out of the DB there isn't really any opportunity for SQL injection hacking with it:
var tabName = db.ExecuteScalar("SELECT name FROM sqlite_master WHERE name = 'mytable_zz' OR name = 'mytable_en' ORDER BY ( name = 'mytable_zz' ) DESC LIMIT 1;")
var results = db.ExecuteQuery("SELECT * FROM " + tabName);
Yunnosch's comment is quite pertinent; you're essentially storing in a table name information that really should be in a column.. You could consider making a single table and then a bunch of views like mytable_zz the definition of which is SELECT * FROM mytable WHERE lang = 'zz' etc, and make instead-of triggers if you want to cater for a legacy app that you cannot change; the legacy app would select from / insert into the views thinking they are tables, but in reality your data is single table and easier to manage

SQLite error: cannot start a transaction within a transaction with very basic tables

I am brand new to SQL, and I am learning on an SQLite editor. So I create a couple of very simple tables. This code is straight from Linkedin learning "SQL essential training", and I am using the recommended SQLite editor.
CREATE TABLE widgetInventory(
id INTEGER PRIMARY KEY,
description TEXT,
onhand INTEGER NOT NULL);
CREATE TABLE widgetSales(
id INTEGER PRIMARY KEY,
inv_id INTEGER,
quan INTEGER,
price INTEGER);
Then I update widgetInventory with some data:
INSERT INTO widgetInventory (description, onhand) VALUES ('rock', 25);
INSERT INTO widgetInventory (description, onhand) VALUES ('paper', 25);
INSERT INTO widgetInventory (description, onhand) VALUES ('scissors', 25);
Next, I want to update the widgetSales table with a sale, and update the widgetInventory table to record the reduction of onhand.
BEGIN TRANSACTION;
INSERT INTO widgetSales (inv_id, quan, price) VALUES (1,5,500);
UPDATE widgetInventory SET onhand = (onhand-5) WHERE id = 1;
END TRANSACTION;
I am not understanding why this gives me an error when I run it, as it is exactly as it is in the lesson.
[06:18:04] Error while executing SQL query on database 'test': cannot start a transaction within a transaction
But, I can run the INSERT and UPDATE lines separately, and they do what I want them to do.
Apparently, running - END TRANSACTION; - before running the entire transaction appears to work.
I think that somehow, SQL thinks that a transaction is already occurring. Though, I'm not sure where exactly. So to stop it, you have to end the transaction first before proceeding with the course.
In the SQLite Editor, you may have to delete or comment out all of the code before and after these two transactions.
BEGIN TRANSACTION;
INSERT INTO widgetSales ( inv_id, quan, price ) VALUES ( 1, 5, 500 );
UPDATE widgetInventory SET onhand = ( onhand - 5 ) WHERE id = 1;
END TRANSACTION;
BEGIN TRANSACTION;
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'toy', 25 );
ROLLBACK;
Otherwise it won't execute the transaction.
Other than that, there is probably an error written in somewhere. Copying and pasting in the .txt file didn't give me that transaction error and could execute the transaction normally.
Just had this same error and my issue was I only highlighted the first line so SQLLite started the transaction but didn't run it fully. All I did was run end transaction, highlight the whole block of code and run that and it worked fine. Must be some syntax issue in Lite that doesn't run the full block itself.
while executing SQL query on database 'test': cannot start a
transaction within a transaction
means a transaction already exists. It may happen if someone forgets to select the END TRANSACTION; statement.
If you face this issue just select END TRANSACTION once and run. With this it will end the active transaction and then you can run any of the existing transaction.
For the particular case of following the Linkedin learning "SQL essential training" course, I have figured out to fix it by running (f9) the "BEGIN TRANSACTION", "...TRANSACTION CONTENTS..." and "END TRANSACTION" statements separately, not all the statements at the same time.
So,
First select the "BEGIN TRANSACTION;" and run it by pressing f9.
Then select the contents of the transactions (I think you can include also the "END TRANSACTION;" part) and run it.

How to write sqlite transaction that rolls back on any error

I have searched extensively on this and I have found a lot of people asking the question but no answers that included code examples to help me understand.
I'd like to write a transaction (in sql using the command line sqlite3 interface) that performs several update statements, and if any of them fail for any reason, rolls back the transaction. The default behaviour appears to be to roll-back the statement that failed but commit the others.
This tutorial appears to advise that it's sufficient to add begin; and rollback; before and after the statements, but that's not true because I've tried it with deliberate errors and the non-error statements were definitely committed (which I don't want).
This example really confuses me because the two interlocutors seem to give conflicting advice at the end - one says that you need to write error handling (without giving any examples) whereas the other says that no error handling is needed.
My MWE is as follows:
create table if not exists accounts (
id integer primary key not null,
balance decimal not null default 0
);
insert into accounts (id, balance) values (1,200),(2,300);
begin transaction;
update accounts set balance = field1 - 100 where id = 1;
update accounts set balance = field1 + 100 where id = 2;
update accounts set foo = 23; //Deliberate error
commit;
The idea is that none of these changes should be committed.
The sqlite3 command-line shell is intended to be used interactively, so it allows you to continue after an error.
To abort on the first error instead, use the -bail option:
sqlite3 -bail my.db < mwe.sql
If you are executing line by line, then the idea is that you first run these commands:
create table if not exists accounts (
id integer primary key not null,
balance decimal not null default 0
);
insert into accounts (id, balance) values (1,200),(2,300);
begin transaction;
update accounts set balance = field1 - 100 where id = 1;
update accounts set balance = field1 + 100 where id = 2;
update accounts set foo = 23; //Deliberate error
At this point, if you have no errors, you run the commit:
commit;
All the updates should be visible if you open a second connection and query the table.
On another hand if you got an error, instead of committing you rollback:
rollback;
All the updates should be rolled back;
If you are doing it programatically in java you would enclose the updates in a try - catch block, and commit at the end of the try, or rollback inside the catch.

SphinxSE with MariaDB not returning result

I am using MariaDB 10.1.19 and Sphinx 2.2.10
I am trying to query the Sphinx server using SphinxSE plugin available in MariaDB but I'm getting "OK" as response instead of result (rows).
Query : SELECT * FROM from_sphinx_documents Where query ="eml";
Screenshot of Database Query
I validated Sphinx server by querying it using test jar file and I'm able to get the result there.
Query from command Line
Below is the structure of my Proxy Table :
CREATE TABLE from_sphinx_documents(
id BIGINT UNSIGNED NOT NULL,
weight INTEGER NOT NULL,
query VARCHAR(3072) NOT NULL,
group_id INTEGER,
_sph_count INTEGER,
CASE_ID INTEGER,
FOLDER_ID INTEGER,
DOC_ID INTEGER,
INDEX(query)
) ENGINE=SPHINX CONNECTION ="sphinx://10.0.10.150:9675/documents"
Please can anybody shed a light on it. Is it a version mismatch issue ?

ASP.NET/SQL Server: Conversion failed when converting from a character string to uniqueidentifier

I am using a paramterized query for this. I am passing a comma-delimited string of unique identifiers to this query's parameters which are split. It works fine using a couple of unique identifiers but I think I am hitting a max value around 2000 characters.
I have around 150 areaID's and selecting a few the query works fine. Selecting all of the areas the query fails and returns
Conversion failed when converting from a character string to
uniqueidentifier.
Any suggestions for getting more values into query without running into this issue?
Area.AreaID IN (SELECT CONVERT(UNIQUEIDENTIFIER, Value) FROM dbo.Split(#AreaIDs,','))
/
USE [Triton2]
GO
/****** Object: UserDefinedFunction [dbo].[Split] Script Date: 12/11/2012 11:39:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Split]
(
#List nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table
(
Id int identity(1,1),
Value uniqueidentifier
)
AS
BEGIN
While (Charindex(#SplitOn,#List)>0)
Begin
Insert Into #RtnValue (value)
Select
Value = ltrim(rtrim(Substring(#List,1,Charindex(#SplitOn,#List)-1)))
Set #List = Substring(#List,Charindex(#SplitOn,#List)+len(#SplitOn),len(#List))
End
Insert Into #RtnValue (Value)
Select Value = ltrim(rtrim(#List))
Return
END
As mentioned by #DigitalID it seems like you should be using #List nvarchar(max), and I'd say do so in this case regardless of performance characteristics. It's fairly certain that your application is simply sending more than 2000 characters now, and it would seem to be timebomb coding to assume you won't be sending more than 4k at some point in the not-too-distant future. You know you app best, though.
Generally, the performance issue is because of storage of BLOBs, which SQL Server will usually only do if they go above the 8kB limit. You don't seem to be storing this data, at least in this function. There may also be some performance degradation in terms of plan caching as well, but again, if you need max, then use it.

Resources