QTableView does not update after ALTER TABLE query using PySide6 - qtableview

I am using the QSqlTableModel and the QTableView. I want to be able to add and drop columns via the menubar in the GUI. When I trigger the SQL query, the db is altered as expected but the view won't update. I have to close and rerun the application to see the altered version of the db.
Here are the methods that are called when clicking on the respective entries in the menu:
def add_db_column(self):
query = QSqlQuery()
query.exec("ALTER TABLE Names ADD NewColumn varchar(255)")
query.finish()
def drop_column(self):
query = QSqlQuery()
query.exec("ALTER TABLE Names DROP NewColumn")
query.finish
I tried to include self.update(), self.model.layoutChanged.emit() and self.select() after the query in those methods, but the view just won't update.
What am I doing wrong here?

Related

Using SHOW CREATE TABLE in a subquery

I want to get the script of existing table and assign to variable and save it in db.
I have used this script
SELECT `CreateTable` FROM (show create table tablename);
But it is not working
I want createtable column from the result plz review the result image:
if there is other way to get script of table also mention it.

Update\Insert data from grafana to mysql

Is is possible to update data or insert data from grafana to mysql. I have a requirement to insert/update information in mysql using a UI. Now I am already using grafana so wanted to know if there is any way we can use grafana to update or insert information
There is no input panel for taking user input and inserting that data to MySQL or any other. (Grafana v7.0)
For very minimalist input data you can use grafana variables as a hack.
Create example table for storing input in MySQL
CREATE TABLE `grafana_variable` (`variable` VARCHAR(64)) ENGINE = InnoDb;
In Grafana dashboard click settings icon:
then click:
add variable:
return to dashboard and you should see at the top:
create a new visualization panel or add another query in any already existing panel and add SQL query:
INSERT INTO `grafana_variable` VALUES ('${myvar}');
now every time you type into that field
and click away from it or use ie tab key the data will be inserted to the MySQL database.
mysql> select * from grafana_variable;
+---------------------+
| variable |
+---------------------+
| this is just a test |
+---------------------+
1 row in set (0.08 sec)
The downside of this hack is that the visualization tile will have in its upper left corner error icon:
with information (when you hover over it):
Found no column named time or time_sec
because the sql query of the visualization tile is inserting data instead of selecting it from database therefore there is no time or time_sec data.
Grafana also does not handle multiple MySQL statements so you cannot repair that by adding before or after the INSERT INTO ... statement second one with SELECT FROM. Maybe this could be somehow patched by using sub queries or something similar but I did not investigate that further.
Because the error as above can be due to multiple reasons not related to the hack it will be better to use the hack query in separate visualization tile - the one that you will remember (or even give descriptive panel tile) so you know that the error there is nothing unusual.
See grafana varialbes for more info about variables.
There is also useful list of already existing variables you can use same way as myvar I created. See section MACROS here For eg:
$__timeFrom()
and
$__timeTo()
have the start and the end of displayed time range.
Permissions and Security
The whole hack works because the MySQL user that grafana uses is allowed to execute INSERT statement, but if grafana is able to execute ANY statement then make sure that MySQL user that grafana uses is not allowed for example to execute statements like DROP ... or any other that is not related to INSERT data into the grafana_variable table as in example.
If you use MySQL as datasource for displaying data the grafana user should also be able to execute SELECT statements.
But nothing else than that.
Read about MySQL user premissions
Must hide Update as temporary table
I am working with postgres and Grafana. Probably it is quite similar.
With postgres you have to hide the update like this:
WITH UPDAT_TEMP AS (UPDATE MyTable SET MY_ROW='MY_TEXT' WHERE ID=99)
SELECT 1;
If you want to update various rows, you have to put various temporary tables.
The
Update my_table (rows) values(...)
does not work for me under Grafana.
Therefore:
WITH UPDAT_TEMP AS (UPDATE MyTable SET MY_ROW='MY_TEXT' WHERE ID=99),
WITH UPDAT_TEMP2 AS (UPDATE MyTable SET MY_ROW2='MY_TEXT2' WHERE ID=99)
SELECT 1;

Split Mysql table on date column and store result in different tables with dynamic name

My job looks like the first image where I am trying to read a Mysql table,
and split the table based on the FROM_DATE field. The steps in the job are as follows :
Use tMap to create a column StringFromDate -> TalendDate.formatDate("yyyy-MM-dd",row1.FROM_DATE)
Connect tMapOutput to tFlowToIterate
Connect tFlowToIterate to tFixedFlowInput. The tFixedFlowInput component configuration is shown in the second image below
Connect tFixedFlowInput to tLogRow
Connect tLogRow to tMysqlOutput. The tMysqlOutput component setting is as shown in the third image.
The problem is that the tables are generating with only 1 row of data. When I try to collect the data in a csv file, this setting works fine with an append option in tFileOutputDelimited.
Please, immediate help required.
The problm is caused by the tMysqlOutput setting drop table if exists and create, which effectively recreates your table at each iteration. Thus the one line you end up with in your table is the one corresponding to the last iteration.
Try with the setting create table if not exists.
Also, based on your followup question, you need to add OnComponentOk -- tMysqlCommit (uncheck close connection) after your tMysqlOutput in order to commit the inserts.

mySQL create view returns no rows, but create table works

I can't figure out the issue here and have done some testing, but I'm new to mySQL queries. Here's the issue. If I run this:
create view services_view as
SELECT `innovation_name`,`link`,`category`,`brief_description` FROM `innovation_db`
WHERE `category`= "Service"
I get a new view with all the correct columns, but the rows are empty. If I just run the query without the create view, it returns the data correctly. If I do this instead:
create table services_view
SELECT `innovation_name`,`link`,`category`,`brief_description` FROM `innovation_db`
WHERE `category`= "Service"
I get a new table with the correct columns AND the rows filled with all the correct data. Why won't the create view work if clearly the create table does?
Thanks

I broke my Mysql View query. How do I fix it?

A few months ago I created a VIEW that gives me last month's transactions. I use that view within other queries. The problem is, I changed some table names of tables that are referenced inside the VIEW and have broken the VIEW query in the process. Now my other queries don't execute anymore.
Is there any way to see what the VIEW statement is and edit it to reflect the new table names? I'm using phpmyadmin, but everytime I try to go to the view table, it gives me an error saying it is invalid, but it won't show me what it is...
Providing you know the name of the view you can run this and it'll show you the query that creates the view. If phpMyAdmin doesn't let you run it, then you will probably have to do it at the command line.
SHOW CREATE VIEW view_name
You should also that it says something like:
SQL SECURITY DEFINER VIEW `myViewName` AS
Directly after that you will find the query that creates this view. Copy and paste it into a text editor and you can alter it so it works with your new table names.
Run it as a normal query to ensure it works, then when you're ready to replace it with the broken original you can run this query (beware this will delete the original view!):
DROP VIEW IF EXISTS myViewName;
CREATE VIEW myViewName AS ...(your new query here)...
Hope that helps!
References:
http://dev.mysql.com/doc/refman/5.1/en/show-create-view.html