insert data in the first table the same data is automatically inserted in the second identical table

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

insert data in the first table the same data is automatically inserted in the second identical table

Post by facos79 » 2019-04-23 13:09

Hello,
I have 2 identical tables. I would like that when I insert data in the first table the same data is automatically inserted in the second identical table. In the second table, however, I will only show a single read-only one to a user (but I think this is not a problem).
How should I do? use sql statement ("UPDATE` stable_name` .....?

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by sjohn » 2019-04-23 14:22

Could you not make the second table as a view of the first table.
This way all data is automatically updated.
You could first create both tables in AppGini.
Then upload and let the tables be created. Then delete the "mirror"-table and recreate it as a view of the first table.
Hope you understand what I mean - and that I have understood you.

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by facos79 » 2019-04-23 14:32

hello, can I do it even if in the second table I have to add a column with a value multiplied by a percentage? In a few words I server that the second table is identical in all respects to the first but with the addition of a column that server to calculate a percentage value. The second table will then be visible to a user who can only view it and not change the values. I would like the user to see only a few columns and the last one containing the percentage calculation.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by sjohn » 2019-04-24 08:29

Short answer - yes.

The following example defines a view that selects two columns from another table as well as an expression calculated from those columns:


mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+

Here is created a table with two columns : quantity and price.
Thereafter is created a view with the quantity and price column AND a calculated field that shows the total.
You can calculate with/on existing data and with constants.

Example can be seen here : https://dev.mysql.com/doc/refman/8.0/en ... -view.html

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by facos79 » 2019-04-24 21:38

thank you very much for the help.
But I am not very familiar with mysql and I don't know how to integrate that code into apps. :?
I was hoping to be able to use the hooks to duplicate the table by taking only the columns I want to show. I mean, when the second table is opened, the data updated from the first one is taken.
Thanks so much for the support.

sjohn
Veteran Member
Posts: 86
Joined: 2018-05-23 09:32

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by sjohn » 2019-04-25 09:14

There is no code that need to be integrated.
The example was only to show how to make a view with a calculated row.

The view only has to be created once. But you have to create it manually.
Once it is created it can be used almost like a "normal" table.

All you have to do is :

In AppGini you create the definition of your tables. Also create the table you need as view.
Upload project.
Delete the table ( the table you want as view ) on the server, and create the table as a view by : CREATE VIEW nameofviewtable AS SELECT field1, field2,....... AS value FROM name oforiginaltable;

As long as you don't make changes in the definition of the two tables ( source and viewtable ) in AppGini, then you can use the "view" table in AppGini as the other tables you have there.

I hope I explain this in a clear way. Maybe someone else here on the forum can give a better explanation.

facos79
Veteran Member
Posts: 115
Joined: 2014-10-29 12:31

Re: insert data in the first table the same data is automatically inserted in the second identical table

Post by facos79 » 2019-04-26 16:39

Delete the table ( the table you want as view ) on the server, and create the table as a view by : CREATE VIEW nameofviewtable AS SELECT field1, field2,....... AS value FROM name of original table;
sorry, it might be just what I'm looking for but I can't figure out how to create the table as a view. I have to do it from phpmyadmin, do I have to create a new page? I didn't understand this passage ...

Post Reply