I am using sqlite as my database. It is connected to the livecode project.
The Contacts table has the following data (address and contact number are omitted for security)
ID Name Address Contact No.
1 John ...Philippines 0999999999
2 Kim ...Philippines 0999999999
When I executed this command...
SELECT Name from Contacts ORDER BY ID DESC LIMIT 1
It will return
Kim
In Livecode, I want to store that value to the variable and display it as a Message Box.
How to do that?
You can use any of LiveCodes database functions. First you need to open the database via:
revOpenDatabase("sqlite",filepath[,sqliteOptions])
Then you can query the database via one of the query commands:
revQueryDatabase(databaseID,SQLQuery[,{variablesList | arrayName}])
There is also a function called revDataFromQuery([columnDelim],[rowDelim],databaseID,SQLQuery[,varsList]) that you might use for your query.
Look them up in your dictionary and you may also have a look at the "Book Database" provided via the start center.
So using the last function you can use:
put revOpenDatabase("sqlite","/path/to/your/database") into tDB
revDataFromQuery(,,tDB,"SELECT Name from Contacts ORDER BY ID DESC LIMIT 1", tResult)
answer tResult
(Using empty row and column delimiter as you only select one field in one post.)
Related
I am trying to select data based on a status which is a string. What I want is that status 'draft' comes first, so I tried this:
SELECT *
FROM c
ORDER BY c.status = "draft" ? 0:1
I get an error:
Unsupported ORDER BY clause. ORDER BY item expression could not be mapped to a document path
I checked Microsoft site and I see this:
The ORDER BY clause requires that the indexing policy include an index for the fields being sorted. The Azure Cosmos DB query runtime supports sorting against a property name and not against computed properties.
Which I guess makes what I want to do impossible with queries... How could I achieve this? Using a stored procedure?
Edit:
About stored procedure: actually, I am just thinking about this, that would mean, I need to retrieve all data before ordering, that would be bad as I take max 100 value from my database... IS there any way I can do it so I don t have to retrieve all data first? Thanks
Thanks!
ORDER BY item expression could not be mapped to a document path.
Basically, we are told we can only sort with properties of document, not derived values. c.status = "draft" ? 0:1 is derived value.
My idea:
Two parts of query sql: The first one select c.* from c where c.status ='draft',second one select c.* from c where c.status <> 'draft' order by c.status. Finally, combine them.
Or you could try to use stored procedure you mentioned in your question to process the data from the result of select * from c order by c.status. Put draft data in front of others by if-else condition.
I'm using Oracle APEX 4.2.6 and Oracle DB 11gR2
I've an interactive report showing the list of clients.
The end user can modify the Name of the client.
My issue is that I have to find a way to allow the end user to find the modified client by seraching it with his old name.
For example, the end user modify the name of client from OLD NAME to NEW NAME
In the serach engine of the interactive report, the end users must be able to find the client by serching it by its old name OLD NAME
Is there a way to manage this situation on the APEX side or Database side.
This is very much a database issue, not an APEX issue. When the user modifies the client name, you will need to record the old name somewhere: this could simply be an OLD_NAME column on the CLIENTS table (which would only support knowing the previous name for a single name change), or it could be a CLIENT_NAME_HISTORY table to which a row is added every time a client name is changed.
Having done that, your interactive report's SQL can then be modified to search both old and new names to find the client - for example:
select ...
from clients
where (name like :P1_NAME or old_name like :P1_NAME)
or
select ...
from clients c
where (c.name like :P1_NAME or exists (select null
from client_name_history h
where h.client_id = c.client_id
and h.name like :P1_NAME)
Note that I think you will need to create a page item for the name filter, because the built-in filter of the IR can only search data that is displayed in the report, which previous names will not be (presumably).
Having additional columns might not be a "scalable" solution. What if another user changes the name again? And again? And again?
A better approach to store this data would be in rows that are uniquely identified by a combination of the primary key of the client along with an object version identifier - this could be a number or a time stamp or a date range. This is an approach that Oracle themselves use in many of its enterprise application.
Example of the data would look like below.
1.) Using Object Version Number
Client Id | Client Name | Object Version Number
1 | Bob | 1
1 | Sam | 2
1 | Ed | 3
Here, every time a user changes the name an additional row is created maintaining the same client_id value but incrementing the object version number by 1. The highest ovn represents the latest value. You could also have a column called "latest_record" and insert a value of Y when creating a new record to show that this is the latest record (resetting the value in the previous latest record to N). Similarly, instead of a number, you can simply store the timestamp and use that to determine the latest record.
Using date range
Client Id | Client Name | Start Date | End Date
1 | Bob | 01-Jan-2017 | 31-Jan-2017
1 | Sam | 02-Feb-2017 | 02-Mar-2017
1 | Ed | 03-Mar-2017 |
In this approach, you are specifying the period of time for which the name was valid. A use case would be an individual taking the adopting the surname of their partner after marriage. In such a case, one name was valid from the time of birth to the date of marriage and another name was valid from the date of marriage onwards.
Once you prepare your datastructure in this format, in the apex report you just need to query on the single name column. I feel additional tables and columns are an unnecessary overhead in this case.
Regards,
SJ
So I currently have a database that keeps tracks of projects, project updates, and the update dates. I have a form that with a subform that displays the project name and the most recent update made to said project. It was brought to my attention however, that the most recent update to a project does not display correctly. Ex: shows the update date of 4/6/2017 but the actual update text is from 3/16/2017.
Doing some spot research, I then learned that Access does not store records in any particular order, and that the Last function does not actually give you the last record.
I am currently scouring google to find a solution but to no avail as of yet and have turned here in hopes of a solution or idea. Thank you for any insight you can provide in advance!
Other details:
tblProjects has fields
ID
Owner
Category_ID
Project_Name
Description
Resolution_Date
Priority
Resolution_Category_ID
tblUpdates has these fields:
ID
Project_ID
Update_Date
Update
there is no built-in Last function that I am aware of in Access or VBA, where exactly are you seeing that used?
if your sub-form is bound directly to tblUpdates, then you ought to be able to just sort the sub-form in descending order based on either ID or Update_date.
if you have query joining the two tables, and are only trying to get a single row returned from tblUpdates, then this would do that, assuming the ID column in tblUpdates is an autonumber. if not, just replace ORDER BY ID with ORDER BY Update_Date Desc
SELECT a.*,
(SELECT TOP 1 Update FROM tblUpdates b WHERE a.ID = b.PROJECT_ID ORDER BY ID DESC ) AS last_update
FROM tblProjects AS a;
I've seen enough answers to know you can't easily check for columns in SQLITE before adding. I'm trying to make a lazy person's node in Node-Red where you pass a message to SQLITE which is the query. Adding a table if it does not exist is easy.
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY);'; node.send(msg);
it occurred to me that adding a table which had the names of the fields would be easy - and if the field name is not in the table.... then add the field. BUT you can't add multiple fields at once - so I can't do this...
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
The problem with THAT is that I can't add this in later, there's no way to check before adding a field it the table exists!
This is what I WANT
msg.topic='create table IF NOT EXISTS fred (id PRIMARY KEY, myfields TEXT);'; node.send(msg);
msg.topic='if not (select address from myfields) alter table fred add column address text';
I just cannot think of any way to do this - any ideas anyone (the idea is that the node-red node would input a table, field and value and if the table didn't exist it would be created, if the field didn't exist it would be created, all before trying to add in the value).
You won't be able to make the ALTER TABLE conditional in the SQL itself. You'll need to handle that from your calling script.
Alternately, simply attempt to add the column to the table and accept failure as an outcome. Assuming the table exists, the only failure reason you could encounter is that the column already exists.
If you'd like to do something more graceful, you can check if the column exists in advance, then conditionally run the SQL from your calling application.
I am unsure on how to do this 'best practice' wise.
I have a web application (asp.net VB) that connects to an MS SQL server 2012. Currently when the page loads the app connects to a DB table and gets the last ID and adds 1 to it and displays this to the user. When the user submits the form the new ID is saved to the DB.
The problem being the app may be opened by 2 users at the same time and therefore they will get assigned the same ref number which will cause problems when the data is saved.
How can I assign different numbers to different users if the app is opened at the same time without saving unnecessary data?
You have multiple solutions for this, I'll try to outline a few approaches. (I'll assume that you need to insert things into a DB that I'll call "orders".)
First of all, you can move the ID-generation to the moment when the order is actually inserted, and not at the moment when the user start to enter the data. That way, you do not generate an ID for a user that never completes the form. Also this scenario is easy to accomplish using autoincrementing values in sql server. You can, for example do:
-- create a table with an identity column
create table Orders (
ID int identity(1,1) not null,
Description nvarchar(max) not null
);
-- insert values, without specifying the ID column
insert into Orders (Description) values ()
-- select the row back
-- returns 1, 'My First Order'
select * from Orders;
Another way to do this is to use SQL Server Sequences. These are things that do nothing except generate numbers in a row. They guarantee that the numbers won't be repeated, and always keep count of the current value, i.e.
-- create a sequence
create sequence OrderIdSequence
start with 1
increment by 1;
-- get the next sequence value
select next value for OrderIdSequence