QSqlTableModel notify on rows inserted - mysql

How do I get notified if a record has been inserted into mysql table by another database client, is this possible?

void QSqlTableModel::beforeInsert(QSqlRecord &record)
This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active database table. The values that are about to be inserted are stored in record and can be modified before they will be inserted.

QSqlDriver supports notifications , however not for Mysql

Related

Do something i.e insert into another table when mysql table becomes empty

I am processing some bulk sms that are stored in the messageout table and once a message is sent,its deleted from the table so there is a time the messageout table is totally empty.
Is there a mechanism in mysql that can be used to notify when the table falls empty i.e triggers or something similar?.
Thanks.

Is it possible to get LAST_INSERT_ID() from different database?

Suppose, that we have 2 databases: a and b, and tables a.test1 and b.test2.
If I need to insert a row into table a.test1, and return LAST_INSERT_ID() to insert into b.test2, will LAST_INSERT_ID() return value from another database? Is it reliable?
I didn't find anything in the manual, but ##IDENTITY depends on client session, so it should be portable between two databases. Isn't it?
LAST_INSERT_ID() always gives you the id of the row inserted by the last INSERT statement you executed on the current connection, irrespective of what table (and what database!) that row went into.
From Mysql documentation:The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
Inshort Both *LAST_INSERT_ID()* and *mysql_insert_id()* work as advertised i.e.: they will retrieve the last id inserted into any table during the current session/connection.

AUTO_INCREMENT and LAST_INSERT_ID

I'm using AUTO_INCREMENT and I would like to get that ID of inserted row so that I could update another table using ID as common field between the 2 tables.
I understood that LAST_INSERT_ID will get last ID. However, my concern is that, the database is accessed at same time by many users. Hence, there might be another process accessed the table and also inserted a new row at same time.
Does LAST_INSERT_ID return just the last ID regardless of the connection used, or only return last ID for the connection that I'm using?
Notice, I'm accessing MySQL database using connection pool in Tomcat server.
In summary, I need to insert a row in table A with auto increment, than I need to insert row in table B, which need to be linked to table A using the AUTO_INCREMENT value.
SELECT max(employeeid) FROM Employee;
The above query returns the value of employeeid of last inserted record in Employee table because employeeid is an auto increment column. This seems to be OK, but suppose two threads are executing insert operation simultaneously, there is a chance that you get wrong id of last inserted record!
Don’t worry, MySQL provides a function which returns the value of auto increment column of last inserted record.
SELECT LAST_INSERT_ID();
LAST_INSERT_ID() is always connection specific, this means even if insert operation is carried out simultaneously from different connections, it always returns the value of current connection specific operation.
So you have to first insert record in Employee table, run the above query to get the id value and use this to insert in second table.
LAST_INSERT_ID() work in context, it should be in transactions or inside user defined stored procedures or user defined functions.
LAST_INSERT_ID is connection specific. That's true, but you should be careful if you use connection pooling. This may be problematic when you perform successive INSERT IGNORE statements in a loop and the pool gives you the same connection at each iteration.
For example; Assume that you receive the same (open) connection for each of the below:
INSERT IGNORE ... some-new-id >>> LAST_INSERT_ID returns 100
INSERT IGNORE ... some-existing-id >>> LAST_INSERT_ID still returns 100 (result of the previous operation)
It is always good to check whether the INSERT operation has in fact inserted any rows before calling LAST_INSERT_ID.
LAST_INSERT_ID return the last insert id for the current session.
As long as you don't insert more than one entry with your current connection, it is valid.
Further information here: https://dba.stackexchange.com/questions/21181/is-mysqls-last-insert-id-function-guaranteed-to-be-correct
(i would link to mysql.com, but it'S currently down for me)

how to update the value of one table when the value is inserted in another table using mysql?

hi i have two tables namely
sms(Message,sms_index...columns)
c_paid_bribe(c_addi_info,....)
what i want to do is to insert the values of Message column of sms table into c_addi_info column of c_paid_bribe table automatically whenever a new value is inserted into sms table.
I tried this
$query=mysql_query("insert into bd_paid_bribe(c_addi_info) select Message from sms");
But when a new value is inserted and i run the .php file the already existed values are also inserting into the table again.....
Q: what i want to do is to insert the values of Message column of sms table into c_addi_info column of c_paid_bribe table automatically whenever a new value is inserted into sms table.
A: If you want to update one table when another is changed (and, for whatever reason, you can't simply do this in your application), then use "triggers":
http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/
Q: But when a new value is inserted and i run the .php file the already existed values are also inserting into the table again.....
A: You want an "upsert". For example:
How do I update if exists, insert if not (AKA "upsert" or "merge") in MySQL?
What you want to do is simply to make two queries that are comitted in one transaction. This is what transactions are for. See here for examples on how to do it:
PHP + MySQL transactions examples

Filemaker Pro 10: How to get the unique ID of last insert on mySQL tables (ODBC)

I have a filemaker script that inserts a new entry on several imported mySQL tables. I need to get the unique id of these entries as they are created (before or after, either way) so I can refer to them later in the script. This is easy to do in SQL with LAST_INSERT_ID(), but I can't seem to find how in filemaker.
What I have tried that didn't work:
GetNextSerialValue ( Get ( FileName )&".fp7" ; "jos_users::id" ) #returns nothing, does not work properly on ODBC tables
Get(RecordID) #Local value does not correspond to the mySQL unique ID
Thanks in advance! Any suggestions are appreciated.
How are you creating the records in your MySQL table? Are you using FileMaker's ESS and native "New Record" command to insert the new record or are you using the Execute SQL[] script step? Or something different?
The next serial value and recordID values are FMP-specific and don't apply here (unless your MySQL primary key is a serial value managed by FMP).
With "New Record" FileMaker sets your record context to the new record. So if your "jos_users" table occurrence is an alias to the MySQL table, calling "New Record" will place you in the context of that record. You can then set a variable to the contents in the "id" column.
Go To Layout ["jos_users" (jos_users)]
New Record/Request
Commit Records/Requests[No dialog]
Set Variable [$lastInsertID; jos_users::id]
This presumes that your MySQL record is given a proper ID at record insertion time and that the record can be committed passing any validation checks you've applied.
If you're creating the records using some other process, let us know and we can advise you on how to get the value.