avg() with sqlite-jdbc - sqlitejdbc

I have a query which relies on avg() using sqlite-jdbc to connect to a sqlite database. However when my query runs I get an answer of 0.0 for the average, but when I run the same query elsewhere (portable database viewer) I get the expected (non-zero) value. Why?
Column being averaged is a list of integers.
SELECT COUNT(col) as count, AVG(col) as average FROM table WHERE <many-clauses-here>
The count returns correctly.
Values being averaged:
"54", "56", "66", "53"
The file format is sqlite, but the way it is styled is in the spec given by ddf spec though this should not be relevant

After I tried using a regular statement instead of a PreparedStatement, I found it worked as intended. I was using JDBC incorrectly as described here - I'm newish to JDBC (haven't used it in ages) so I made a rookie mistake, but it manifested itself so oddly.

Related

How to implement INSERT where not exists for ORACLE in Mule4

I am trying to implement a use-case in Mule4 where a tour needs to be assigned to a user if it has not already been assigned.
I was hoping that I could implement it using Mule db:insert component and using INSERT WHERE NOT EXISTS SQL script as below.
INSERT INTO TL_MAPPING_TOUR(TOURNO,TLID,SYSTEM) select :tourno,:tlid,:system from DUAL
where not exists(select * from TL_MAPPING_TOUR where (TOURNO=:tourno and TLID=:tlid and SYSTEM=:system))
However, this is resulting in Mule Exception
Message : ORA-01722: invalid number
Error type : DB:BAD_SQL_SYNTAX
TL_MAPPING_TOUR table has an id column (Primary Key), but that is auto-generated by a sequence.
The same script, modified for running directly in SQL developer, as shown below, is working fine.
INSERT into TL_MAPPING_TOUR(TOURNO,TLID,SYSTEM)
select 'CLLO001474','123456789','AS400'
from DUAL
where not exists(select * from TL_MAPPING_TOUR where (TOURNO='CLLO001474' and TLID='123456789' and SYSTEM='AS400'));
Clearly Mule db:insert component doesn't like the syntax, but it's not very clear to me what is wrong here. I can't find any INSERT WHERE NOT EXISTS example implementation for the Mule4 Database component either.
stackoverflow page https://stackoverflow.com/questions/54910330/insert-record-into-sql-server-when-it-does-not-already-exist-using-mule directs to page not found.
Any idea what is wrong here and how to implement this in Mule4 without using another Mule4 db:select component before db:insert?
I don't know "mule4", but this:
Message : ORA-01722: invalid number
doesn't mean that syntax is wrong (as you already tested it - the same statement works OK in another tool).
Cause: You executed a SQL statement that tried to convert a string to a number, but it was unsuccessful.
Resolution:
The option(s) to resolve this Oracle error are:
Option #1: Only numeric fields or character fields that contain numeric values can be used in arithmetic operations. Make sure that all expressions evaluate to numbers.
Option #2: If you are adding or subtracting from dates, make sure that you added/substracted a numeric value from the date.
In other words, it seems that one of columns is declared as NUMBER, while you passed something that is a string. Oracle performed implicit conversion when you tested the statement in SQL Developer, but it seems that mule4 didn't and hence the error.
The most obvious cause (based on what you posted) is putting '123456789' into TLID as other values are obviously strings. Therefore, pass 123456789 (a number, no single quotes around it) and see what happens. Should work.
SQL Developer is too forgiving. It will convert string to numbers and vise versa automatically when it can. And it can a lot.
Mulesoft DB connector tries the same but it is not as succefule as native tools. Pretty often it fails to convert, especially on dates but this is not your case.
In short - do not trust too much data sense of Mulesoft. If it works - great! Otherwise try to eliminate any intelligence from it and do all conversions in the query and better from the string. Usually number works fine but if doesn't - use to_number function to mark properly that this is the number.
More about this is here https://simpleflatservice.com/mule4/AvoidCoversionsOrMakeThemNative.html

SQLite Prepared Statement for Create Table error

I'm running into an error trying to use a SQLite prepared statement:
create table RawRecord (?, ?, ?);
Calling sqlite3_prepare16_v2 gives me this error: SQLITE_ERROR: SQLITE_ERROR[1]: near "?": syntax error
I don't run into problems with prepared statements anywhere else (and have been using SQLite for many years). I have tried to find whether prepared statements are simply not allowed for CREATE TABLE, but haven't found anyone saying that's the case.
If I build the create string manually and embed my column names, it works. I prefer to use prepared statements simply because it makes things like quotes cleaner, and in this case the column names come from user data, so I don't know what they will be.
I can certainly work around this, but was hoping to understand why this is an error.
Help?

UPDATE statement not working in sqlite

I'm using the following command to update a field in my database:
UPDATE Movies SET 'From'=2 WHERE 'Name'="foo";
I'm using sqlite3.exe in windows (command prompt). Although no error message is produced, nothing changes in the table. I examined the database with a couple of gui tools and I'm sure UPDATE does nothing.
'From' is of type integer and 'Name' is text.
The problem you've got is that you're getting your quoting wrong. SQLite follows the SQL standard here, and that specifies what quote characters to use: '…' is for strings, and "…" is for tokens (like special names used as column or table names). Sometimes it manages to guess what you mean anyway and compensate for getting it wrong, but it can't with the WHERE clause because that is syntactically correct (if decidedly unhelpful):
Swapping the quoting appears to work:
UPDATE Movies SET "From"=2 WHERE "Name"='foo';
Those aren't good column names. Both are keywords, best avoided, and not self-explanatory at all.
Do a SELECT to see how many rows match the WHERE clause in the UPDATE. If none come back, you have your answer.
Did you commit the UPDATE? Is auto commit turned on?

nHibernate named query returns not all results

I have a few simple named queries with only joins, ant couple subselects. All of them ar working perfectly except one. The problem is, that when i run SQL code in Management Studio, i get 177 results, and when i run named query with the same SQL code, i get 20 results. I can't figure out why is that. I Call named query the as all other:
public IList<InstitutionIndexDTO> GetInstitutionIndexByWorkTimeSearch(int time, int institutionType)
{
IQuery query = GetCurrentSession()
.GetNamedQuery("GetInstitutionsListByTime")
.SetInt32("Type", institutionType)
.SetInt32("TimeUntilClose", time)
.SetResultTransformer(Transformers.AliasToBeanConstructor(typeof(InstitutionIndexDTO).GetConstructors()[0]));
return query.List<InstitutionIndexDTO>();
}
Even when i harcoded parameters in SQL, i still got the same result. I tried checking with Profiler, but generated SQL is perfect and in management studio returns all 177 results.
InstitutionIndexDTO is working correctly, because i use it with other named queries.
I have a working named query, and the one causing problems was made from that one, adding additional INNER JOIN and changing WHERE clause. Both queries returns same columns.
Maybe somebody has an idea, what i could have done wrong?
I tested my code and realised that this line is causing the problem:
(CASE WHEN (DATEPART(dw, GETDATE())) = 1 THEN 7 ELSE (DATEPART(dw, GETDATE()) - 1) END)
If i understand well, the problem is in DATEPART function. Has anybody encountered this problem?

Verifying sqlite FTS (Full Text Search) syntax, and how to do a single term NOT search

Is there a way to determine if a MATCH query sent to an fts3 table in sqlite is valid? Currently, I don't find out if the expression is invalid until I try to run it, which makes it a little tricky. I'd like to report to the user that the syntax is invalid before even trying to run it.
I'm using sqlite with the C api.
Additionally, doing a search with the expression "NOT " will fail with a "SQLlite logic/database error". Googling seems to indicate that such a query is invalid. Is there a correct syntax to do the operation? I'm essentially trying to find entries that do NOT contain that term. Or do I have to drop back down to using LIKE and do a sequential scan and not use FTS?
Looks like the simplest way right now is to create a separate FTS table with no rows, and execute the query against it. It will be immediate since there's no data in the table, and will report and error if the query syntax is incorrect.

Resources