How to work around missing insertId in WebSQL - javascript

I want to obtain the ID of the just inserted record into a WebSQL database. WebSQL has the InsertID Property for this task.
However this property is not supported in the Cordova WebSQL Plugin.
The Cordova WebSQL Plugin adds WebSQL to Windows8 Cordova Apps
https://github.com/msopentech/cordova-plugin-websql/
This plugin however doesn't support the insertId Property - see quirks section
How to efficiently obtain the ID of a newly inserted record without using the insertId?

I am suggesting a workaround for SQLITE db and assuming that the implementation of WebSQL is similar to SQLLite db.
I feel that you would at least have to make two passes (two SQL) statements for getting the last insert ID. The easiest option is trying to get the maximum id that has been inserted in the table e.g if id is the column set to autoincrement then use the below sql.
select max(id) from table
and then using the max(id)+1 from above as the insertId that would get inserted into the table.
2nd Option:
In SQLite when you create a table with an autoincrement column an entry is made in the sqlite_sequence table. It holds the value of the last id that has been applied to the column. You need to select the id for the table as shown below.
You need to find out what is a similar table WebSQL.
select seq from sqlite_sequence where table = <table_name>
Increment the seq from above query to 1 to get the sequence that will be used by the autoincrement for the desired table as the last insertId.

Related

How to limit array size in supabase?

I am making an application which involves users storing their favorite content series (which each have a unique ID).
The problem is : I want to check if those IDs exists in the database and also to limit the size of this list to a floor (~100 items).
So far, I have tried to implement a RLS policy but checking the content of the posted data is too complex especially for arrays.
I am considering the use of a sort of gateway API that makes a bridge between the user and the database of their profile to insure more checks but before working on that I want to be sure there is no way to implement this by relying on Supabase only.
You can use a database constraint to limit the size of the list to a floor of 100 items. Additionally, you can use a SQL query to check if the IDs exist in the database.
To limit the size of the list, you can use a check constraint on the table that stores the user's favorite content series. The check constraint can be defined to ensure that the size of the array is not greater than 100.
To check if the IDs exist in the database, you can use a SQL query with the "IN" operator. The query can be written to check if each ID in the user's list exists in the database.
These solutions can be implemented using Supabase only, without the need for a gateway API.

If the field doesnt exist in the document how do i handle the firebase query?

I am ordering the objects from firebase by timestamp and timestamp was included in last build. How do i handle a case where there is no timestamp it should just push the object to last?
Basically sorting the stuff using timestamp and want to push the queries with no timestamp in the end.
If you perform a query that filters and sorts a collection using a specific field, and that field does not exist in a document, that document will not appear in the query results at all. In that case, there's nothing you can do in the query to include it to the results. Firestore indexes can't find documents that don't have a field - indexing only works with fields that are present.
If you're querying a collection for all documents, and you want push a document without some field to the bottom of the results, you will have to apply a sort in your client code to reorder the documents as you require. Firestore will not do that for you.

JsGrid - Auto Increment ID

I recently started using JsGrid and I really like it. It is simple to interact with and well documented.
I though face a small issue:
Whenever I insert a new row in the JsGrid application I get asked an ID. If this ID happens to be the same as an already existing ID, it will give my an error on my MySql database (unique key).
Is there a way to fill in the ID for the user and make sure he can not alter it while inserting?
Thanks!
Usually ID is generated on the DB level. So user should not provide the ID for new items. Remove ID field from the grid or just make it readonly, removing the type attribute.
It's implemented in the sample project, showing how to use jsGrid with PHP + MySQL RESTful backend https://github.com/tabalinas/jsgrid-php.

Populating multiple webpage output with one global database query?

How can one make one global database query (with PHP) and then use all the output (multiple rows) on various places on a webpage?
I read that javascripts can be called for each specific field on a webpage that needs data, but this is inefficient with regards to performance.
The webpage would contact a sort of table-of-contents with version numbers next to each of them. Those version numbers are stored inside the database and calling the database 20 times for the 20 different fields would be inefficient.
Any suggestions on how to run say a PHP query globally when the page loads and then later in the page use the different output at different locations on the page?
QUESTION UPDATE WITH EXAMPLE OF DESIRED OUTPUT:
The webpage should show the following output:
Document Name Document Version
DEPT A DOCS:
Doc ABC 1.2
- Description of doc
Doc another doc 2.3
- Description of doc
DEPT B DOCS:
Yet another doc 0.9
- Description of doc
Doc XYZ 3.0
- Description of doc
Each of the documents have its own version associated with it. Each document has its own table inside the database with its associated version and this can be queried from a Postgres function or View. I wish to query this function or view only once and then display the results in a sort of 'table-of-contents' style (or table sort of view) on the webpage.
Thank you
P.S. This is my first post here.
Make the query in a separate PHP page that is included in all the pages you want to use the information on.
In the beginning of your page make one database query to get data for all versions.
Using PHP split it into associative array having version number as key.
Then during in different section of your page just apply to that array with version number. output the data the way you need. and it will be the data of this version

Selecting SQL Query from Javascript Selections

https://docs.google.com/file/d/0B9urXGIYtvx-YzdYNlllbUxFeHM/edit?usp=drive_web
Attached is a picture of what I have done so far. This is just the front end and I need help with the backend.
I have a question about using SQL to bring up a table based on these selections. I created these dropdowns and selections using javascript but I want to be able to pass them on to a sql to search the database based on selections and return anything that fits the criteria.
To explain everything:
I want to lists at the bottom to be apart of the Select (ex. Select OrganizationID, OrganizationName, OrganizationType, EventID, EventName)
Then I would like the two selections at the top to be the FROM (ex. FROM Events, Organization) and then of course join these with a WHERE.
Then I would like the include or exclude to be apart of the WHERE (ex. WHERE EventID=32211)
I know that I will have to use PHP for this as well and that's perfectly fine I just need help starting it to find out how to pass these selections.
the easiest way is to wrap everything in a FORM tag with an action property. The action property will specify the page you'll be sending the data to (via an http post by default).
so you could put action="searchresults.php", for example.
Your server will receive the values from all of the inputs on your page inside of the $_POST variable. The $_POST variable is an associative array where the key is the name attribute of the input elements.
so to get the values the people entered you'd do something like this:
if (isset($_POST)){
$table=$_POST["Table"]; //where Table is the name of your first option box, it should get the value assigned to the option element.
}
After you get the values, you need to build out your query. The ONLY ways you should EVER consider doing this is through either a prepared statement or a stored procedure.
here's the php documentation on MSSQL prepared statements: http://us2.php.net/sqlsrv_prepare
the long and the short of it:
//$conn is the database connection, $params is an array of the parameters to fill in the ?.
$sql = "SELECT FROM table WHERE id=?";
$stmt=sqlsrv_prepare ($conn, $sql , $params);
NEVER build a query by concatenating user inputs, this leaves you open to SQL injection attacks.
If you'd like some more specifics, you're going to have to provide more information, a database schema, the HTML on your form, some expected results.

Categories

Resources