Using C++ SQL to Add Relationship to Microsoft Access Table - c++

I'm trying to add a CONSTRAINT/RELATIONSHIP between two Microsoft Access Tables using C++ (MFC-based) (through ODBC) with the following SQL Command:
ALTER TABLE [JobSettingsReporting]
ADD CONSTRAINT FK_PerEntry
FOREIGN KEY (JobSetID)
REFERENCES JobSettingsEntry(JobSetID)
ON UPDATE CASCADE
ON DELETE CASCADE
I tried to do it with this:
CDatabase db;
db.OpenEx(App().GetDBConnectString());
db.ExecuteSQL(strSQL);
db.Close();
where strSQL is the Query above. I'm getting the error:
Syntax error in CONSTRAINT clause.
It seems the problem is with the "ON UPDATE CASCADE" and "ON DELETE CASCADE" components. I get the same error when I use the Query Design tool (or in VB) within Microsoft Access.
However, when I do the following in VB in Microsoft Access, it works:
Dim cn As ADODB.Connection 'For action queries
Set cn = CurrentProject.Connection
cn.Execute "ALTER TABLE [JobSettingsReporting]
ADD CONSTRAINT FK_PerEntry
FOREIGN KEY (JobSetID)
REFERENCES JobSettingsEntry(JobSetID)
ON UPDATE CASCADE
ON DELETE CASCADE"
This command also seems to work in SQL Server Express when directly using the Query Window.
It is my understanding that I need to use Jet/ADO to issue this command in C++ for it to work but I'm having trouble finding the syntax. I need to add this relationship programmatically so that it will update the databases on all the deployed machines.
Any help would be appreciated.

Related

Microsoft Access ODBC Driver Manager Function sequence error

I am trying to use Qt to query a table in an MS Access database with a QSqlQuery. I am able to query all tables, except for one. The one table returns the error:
[Microsoft][ODBC Driver Manager] Function sequence error
Here is the code I use to query the table.
QSqlQueryModel *tempModel = new QSqlQueryModel();
QSqlQuery *qry = new QSqlQuery();
qry->prepare("SELECT * FROM table_name;");
qry->exec();
tempModel->setQuery(*qry);
while(tempModel->canFetchMore())
{
tempModel->fetchMore();
}
I've tried the answer from this SO question, but no change.
QSqlQuery causing ODBC Function sequence error
I too encountered issue with DAO connector to MySql backend. My passthrough queries were working, but attempt to read from the table using DAO were receiving ODBC function sequence error. The recordset connector was fine - no problem with move.first, move.last, record count, numerating the field names. Program failed when attempting to read record data -- but again, no problem was happening with my passthrough queries.
Issue was easy to resolve. I forgot to refresh my ODBC link after making a table schema changes. Refreshed the link.. and now everything working normal again.
To simplify my life, I added a program link for end-users to automatically refresh the ODBC links.
The issue seemed to be with the Date/Time datatype of one of the columns.
One of my columns had a data type of "Date/Time" with a property of "IME Sentence Mode" set to "Phrase Predict".
Changing this from "Phrase Predict" to "None" allowed me to query the MS Access table from my Qt application.

Doctrine ORM how to save an UPDATE of my object to the database (equivalent of SQL UPDATE query)

I'm new to Doctrine and I can't seem to find an answer to this.
To save a newly created object to the database (equivalent of SQL INSERT query), I'm using
$this->getEntityManager()->persist($category);
$this->getEntityManager()->flush($category);
And to delete one (equivalent of SQL DELETE query), I'm using
$this->getEntityManager()->remove($category);
$this->getEntityManager()->flush($category);
Now, how do I save updates to my $category object to the database? I'm looking for the Doctrine equivalent of an UPDATE query.
Surely Doctrine ORM must have a simple method to do this, without having to use DQL?
Well there is no need for specific update query.
With doctrine you can update objects like this. Let's suppose you have category object with field name. So we will try to update it.
$newName = 'hello world';
$category->setName($newName);
$this->em->flush(); // without passing the object
This will update you entity.
So following up from this, we have learnt, that you can update entities, by just using setters and setting new values, and after that you flush. It will handle things automatically.

ibm db2 multiple alter table statements

I am trying to run multiple alter table statements for adding foreign keys on my database
I am using RazorSQL
this are my sql statement
ALTER TABLE SPO999.AVTVRSTEPLACILAPOD
ADD CONSTRAINT SQL100419145030510 FOREIGN KEY
(AVP_VRSTEPLACILA)
REFERENCES SPO999.VRSTEPLACILA
(VP_ID_VP)
ON DELETE NO ACTION
--ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
-- DDL Statements for foreign keys on Table SPO999.AVTVRSTEPLACILAVRPL
ALTER TABLE SPO999.AVTVRSTEPLACILAVRPL
ADD CONSTRAINT SQL100419145030630 FOREIGN KEY
(AVV_VRSTEPLACILA)
REFERENCES SPO999.VRSTEPLACILA
(VP_ID_VP)
ON DELETE NO ACTION
--ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
If I run one at a time it works, while if trying to run both at I get an SQL Error -104 a token,character or clause is invalid or missing.
I can not find a problem/solution
any suggestions?
thank you
I think it is likely that you can't have these two foreign key definitions referencing the same column within a single transaction.
Try adding commit; between the statements.
(Your SQL editor, depending on its settings, may automatically send a commit after each chunk of statements that you execute. That would explain the difference between the two cases).

borland builder c++ oracle question

I have a Borland builder c++ 6 application calling Oracle 10g database. Operating over a LAN. When the application in question makes a simple db select e.g.
select table_name from element_tablenames where element_id = 10023842
the following is recorded as happening in Oracle (from the performance logs)
select table_name
from element_tablenames
where element_id = 10023842
then immediately (and not from C++ source code but perhaps deeper)
select table_name, element_tablenames.ROWID
from element_tablenames
where element_id = 10023842
The select statement is only called once in the TADODbQuery object, yet two queries are being performed - one to parse and the other adds the ROWID for executon.
Over a WAN and many, many queries this is obviously a problem to the user.
Does anyone know why this might be happening, can someone suggest a solution?
Agree with Robert.
The ROWID uniquely identifies a row in a table so that the returned record can be applied back to the database with any changes (or as a DELETE).
Is there a way to identify a particular column (or set of columns) as a primary key so that it can be used to identify a row without using a ROWID.
I don't know exactly where the RowID is coming from, it could be either the TAdoQuery implementation or the Oracle Driver. But I am sure I found the reason.
From the Oracle docs:
If the database table does not contain a primary key, the ROWID must be selected explicitly when populating DataTable.
So I suspect your Table does not have a primary key, either add one or add the rowid.
Either way this will solve the duplicate query problem.
Since you are concerned about performance. In general
Using TAdoQuery you can set the CursorType to optimize different behaviors for performance. This article covers this from a TAdoQuery perspective. MSDN also has an article that covers it from from a general ADO Perspective. Finally the specifications from the Oracle Driver can be useful.
I would recommend setting the Cursor to either as they are the only supported by Oracle
ctStatic - Bi-directional query produced.
ctOpenForwardOnly - Unidirectional query produced, fastest but can't call Prior
You can also play with CursorLocation to see how it effects your speed.

Drop constraints only if it exists in mysql server 5.0

i want to know how to drop a constraint only if it exists. is there any single line statement present in mysql server which will allow me to do this.
i have tried the following command but unable to get the desire output
alter table airlines
drop foreign key if exits FK_airlines;
any help to this really help me to go forward in mysql
I do not believe this is possible in a single line, unless you are willing to detect the error and move on (not a bad thing).
The INFORMATION_SCHEMA database contains the info you need to tell if the foreign key exists, so you could implement it in a 2 step process.
http://dev.mysql.com/doc/refman/5.1/en/table-constraints-table.html
yep not possible the if exist is available only for database table and view :
http://dev.mysql.com/doc/refman/5.0/en/replication-features-drop-if-exists.html
yep 2 step process is a good way like gahooa said