SQLite, Update Statement using DateTime - sqlite

I have been trying to update a datetime column using the following SQL Statement:
UPDATE HistoricData SET RecordDate=DATETIME(RecordDate,'60 minutes') WHERE DataStreamID=1 AND TimeFrameID=5
However, I keep getting the following error message:
NOT NULL Constraint Failed: HistoricData.RecordDate
If you could recommend the appropriate change to get this working, I would very much appreciate it.
I will attempt to attach a sample schema and data:
Table Data
Table Schema

After inspecting your DML, my only remaining concern was the datetime format you have in your table. I tried updating such a value as you did, and guess what, it returns NULL: http://sqlfiddle.com/#!7/f4651/10 Why? Because your strings (do notation) are not valid ISO-8601 strings. You probably need to simply replace the dots with dashes before updating (http://sqlfiddle.com/#!7/f4651/11).

Related

Whats the correct syntax for on conflict do update sqlyte?

I am trying to insert clients on a table(importing them from a csv), the email column needs to be unique(in the csv the a client can appear more than one time, the last instance has the correct info) and the ids are created with an autoincrementing value (this is why i cant use select or replace), im trying to use the syntax found in the sqlite guide(and in every question about ON CONFLICT DO UPDATE found here) but sqlite throws a
SQL logic error near "ON" :syntax error
the query goes like this(using VB)
sqlQuery = "INSERT INTO clients(name1,name2,address1,address2,plz,city,country,phoneNumber1,phoneNumber2,cellPhoneNumber,fax,email) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(email) DO UPDATE SET name1=excluded.name1,name2=excluded.name2,address1=excluded.address1,address2=excluded.address2,plz=excluded.plz,city=excluded.city,country=excluded.country,phoneNumber1=excluded.phoneNumber1,phoneNumber2=excluded.phoneNumber2,cellPhoneNumber=excluded.cellPhoneNumber,fax=excluded.fax,email=excluded.email;

Reading dates from Sqlite with Delphi / Firedac

When reading date fields from Sqlite into Firedac, I get conversion errors. The fields are called dates but with string entries (yyyy-mm-dd). I set the option for Datetime Format = string, but I've discovered that while null values are handled OK, empty values (= '') produce an error which I can't figure out how to handle.
You can enable StrsEmpty2Null option, which will automatically convert all empty strings to NULL state. But it's for all values and parameters handled by the data component. So it's not the cure.
I'm not sure what you're doing, but in general, NULL is a state and you cannot convert NULL state to a value because it's a state indicating no value. So as you cannot convert empty string to date.
So try to describe more about your value to string conversion, so we can suggest a proper way to deal with it. For SQLite, I'd suggest using DATE pseudo data type and convert values through the built-in formatting expressions.

Delphi - ClientDataSet SQL calculated field causing "Invalid field type" error at runtime [duplicate]

Using Delphi 10.2, SQLite and Teecharts. My SQLite database has two fields, created with:
CREATE TABLE HistoryRuntime ('DayTime' DateTime, Device1 INTEGER DEFAULT (0));
I access the table using a TFDQuery called qryGrpahRuntime with the following SQL:
SELECT DayTime AS TheDate, Sum(Device1) As DeviceTotal
FROM HistoryRuntime
WHERE (DayTime >= "2017-06-01") and (DayTime <= "2017-06-26")
Group by Date(DayTime)
Using the Field Editor in the Delphi IDE, I can add two persistent fields, getting TheDate as a TDateTimeField and DeviceTotal as a TLargeIntField.
I run this query in a program to create a TeeChart, which I created at design time. As long as the query returns some records, all this works. However, if there are no records for the requested dates, I get an EDatabaseError exception with the message:
qryGrpahRuntime: Type mismatch for field 'DeviceTotal', expecting: LargeInt actual: Widestring
I have done plenty of searching for solutions on the web on how to prevent this error on an empty query, but have had not luck with anything I found. From what I can tell, SQLite defaults to the wide string field when no data is returned. I have tried using CAST in the query and it did not seem to make any difference.
If I remove the persistent fields, the query will open without problems on an empty return set. However, in order to use the TeeChart editor in the IDE, it appears I need persistent fields.
Is there a way I can make this work with persistent fields, or am I going to have to throw out the persistent fields and then add the TeeChart Series at runtime?
This behavior is described in Adjusting FireDAC Mapping chapter of the FireDAC's SQLite manual:
For an expression in a SELECT list, SQLite avoids type name
information. When the result set is not empty, FireDAC uses the value
data types from the first record. When empty, FireDAC describes those
columns as dtWideString. To explicitly specify the column data type,
append ::<type name> to the column alias:
SELECT count(*) as "cnt::INT" FROM mytab
So modify your command e.g. this way (I used BIGINT, but you can use any pseudo data type that maps to a 64-bit signed integer data type and is not auto incrementing, which corresponds to your persistent TLargeIntField field):
SELECT
DayTime AS "TheDate",
Sum(Device1) AS "DeviceTotal::BIGINT"
FROM
HistoryRuntime
WHERE
DayTime BETWEEN {d 2017-06-01} AND {d 2017-06-26}
GROUP BY
Date(DayTime)
P.S. I did a small optimization by using BETWEEN operator (which evaluates the column value only once), and used an escape sequence for date constants (which, in real you replace by parameter, I guess; so just for curiosity).
This data type hinting is parsed by the FDSQLiteTypeName2ADDataType procedure that takes and parses column name in format <column name>::<type name> in its AColName parameter.

Retrieving Data From Returned Oracle Timestamp Column

We have a ColdFusion 8 (Linux) application that uses an Oracle timestamp. We just converted to Oracle 11g from 10g and we're now using Oracle's thin client on the data sources. We're getting an error in the application where a timestamp column is selected.
It seems as though an object of class oracle.sql.TIMESTAMP is being returned. I verified this by dumping the contents of the column. Sure enough, it gives me a break down of the object's methods and their return types. But, I can't seem to be able to interface with this object directly:
<cfquery name="getstuff" ...>
SELECT timestampfld ...
FROM myTable
</cfquery>
getstuff.timestampfld contains an object. But, doing this:
<cfoutput query="getstuff">
#timestampfld.stringValue()#
#timestampfld.dateValue()#
</cfoutput>
produces the error that says those methods can't be found. How can I get at the data held in that object?
Update from comments:
When I take column value and apply the DateFormat( timestampfld, "dd.mm.yyyy" ) function to it. The CF error is
"The value class oracle.sql.timestamp cannot be converted to a date".
When I perform <cfoutput>, I get the class definition.
In the retrieved column, I seem to be getting an object instead of a string. When I cfdump the column, I get OBJECT OF oracle.sql.TIMESTAMP. The dump lays out the methods and fields available. When I cfoutput that same variable, a string is displayed. When I try to perform a DataFormat() on the variable, it complains that its not a date.
I just happened to stumble over this error during my development. I had it in the past and long forgotten since.
I know of two ways to mitigate:
The first is according to an answer of a question regarding the same error message in a different context.
One would add the following to the jvm.config file
-Doracle.jdbc.J2EE13Compliant=true
The second is not to return an Oracle TIMESTAMP column but CAST it do DATE, first, like
<cfquery name="getstuff" ...>
SELECT CAST( timestampfld as DATE ) timestampfld
FROM myTable
</cfquery>
I'm not satisfied with either, to be honest. When the JVM argument is forgotten, the software crashes. The CAST, on the other hand, may influence how the SQL works.

Copied database from live server doesn't allow me to perform inserts

After I copied the SQL Server database from the server and changed the connection string, the database appears to be read-only, but I try inserting a new node it gives me this error:
Cannot insert the value NULL into column 'ID', table
'Theshuk.dbo.Banners'; column does not allow nulls. INSERT fails. The
statement has been terminated.
This does not occur when I use the live server's database.
Any ideas?
Thanks
i have seen some weird things like that in the identity not sticking around in a database migration. not sure the cause, but it sounds like you are probably just missing an identity that was there in the other location. check all your tables to see that you have identities specified where you expect them.
The error just means: you cannot insert the values that you're trying to insert. As the error clearly states:
Cannot insert the value NULL into column 'ID', table
'Theshuk.dbo.Banners'; column does not allow nulls.
So it seems you're trynig to insert something into that table, and the table requires a value for ID and you're not supplying one.
That's all there is - add that column and a value for it to your INSERT statement and you should be fine.

Resources