Microsoft Access ODBC Driver Manager Function sequence error - c++

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.

Related

CRecordset fails on adding a new row using the free ODBC driver for SQLite

I have an C++ application using MFC CRecordset to add rows to a table of an SQLite database, using the free ODBC driver by Ch. Werner. For this, I use the usual sequence of rs.Open(), rs.AddNew(), set values, and finally rs.Update().
This works on a small example, but with my actual database rs.Update() fails with error -1 and the following error message: unrecognized token: ""RedFaktorFly" (1). The 'token' is a truncated name of column 14 of the table, whose full name is "RedFaktorFlyt".
In some runs, it appends seemingly random characters, so the message becomes for ex. unrecognized token: ""RedFaktorFlyH" (1).
Interestingly, when I add "LongNames=true" to the ODBC connection string, which prepends table names to the column names and therefore makes the SQL query longer, the error becomes (for ex.) unrecognized token: ""K_Noder.MaxKompresjox" (1) - where "MaxKompresjonsFaktor" is the name of column 10 of the table.
This seems to suggest that there is a limit on the length of a SQL query accepted by the driver - but it seems strange that such a limit would be so small that it would fail already with 14 columns.
I do not think that the limit is in the C++ part, since the same code works fine both with the (commercial) SQLite driver from Devart and with Microsoft's ODBC driver for Access.
I tried adding a TraceFile option to the ODBC connection string, but it does not seem to do anything, so I do not know what exactly gets sent to the ODBC driver.
I see the same behaviour both with 32- and 64-bit builds, using Visual Studio 2015 on Windows 10.
Any suggestions what to try next?
I'll give you a solution to solve your issue completely. I don't know if it's suitable for you, but definitely it works. I successfully use it on my own.
For any operation except listing records, use CDatabase, not CRecordset. So, to insert rows to any table, to update records, to delete records, use CDatabase. To retrieve records from SQLLite database, use CRecordset. I can give you examples if you need.

wrong character showing when insert persian string to sql server using poco ODBC

I am trying to insert Persian string like "سلام" to SQL server 12 database with Poco ODBC. But in the database I see Characters like this "ط³ظ„ط§ظ…". the column data type is varchar(I try it with nvarchar too) and I test it with different Collation like Arabic_CI_AS and Persian_100_CI_AS.
there is no problem with data stored in the database. it is what I inserted into the database.
but when I try to see my database with Microsoft SQL Server Management Studio and another application with Qt interface, both of them show me "ط³ظ„ط§ظ…".
Does anyone have any idea how to fix it?
std::string updated = "سلام";
Statement select(session);
select << "INSERT INTO Employee VALUES( ?)",
use(updated),now;
Please change 'سلام' to N'سلام'

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

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.

ODBC SQL Server Unicode Bug?

Background:
We have an application that uses the ODBC API to interact with Access and SQL Server (dynamically, depending on user's configuration).
I have discovered a bug which might be in the ODBC SQL driver, or may be a misconfiguration issue with the ODBC DSN we create, or may be a bug somehow in our code.
When a document is edited and saved, we query the database to see if this file has a corresponding record in the database - if so, we update the record with the updated data from the document; if not, we do an insert to create the necessary record for it.
We use the filename as the unique primary key on our table, and this works fine normally.
The bug is that if the filename contains characters outside of the current ANSI code page, then the select indicates no matches:
SQL: SELECT * FROM "My Designs" WHERE "PATHNAME" = '\\FILE-SERVER\Home Folders\User Files\狭すぎて丸め処理が出来ません!!.foo' [# matches = 0]
However, when the insert is attempted, we get a unique key violation (of course) - since there already is a record with that filename.
Database error: Violation of PRIMARY KEY constraint 'PK__My Desig__1B3D5B4BF643706B'. Cannot insert duplicate key in object 'dbo.My Designs'. The duplicate key value is (\\FILE-SERVER\Home Folders\User Files\狭すぎて丸め処理が出来ません!!.foo).
The statement has been terminated.
I've been over the code with a fine-tooth comb, and I can see nothing wrong. :(
The SQL statement that is being generated produces the correct Unicode output of the filename. Our application is compiled for Unicode. The column is SQL_WVARCHAR in ODBC speak.
I've tried adding AutoTranslate=no to the DSN configuration string, but that appears to have no effect.
I've tried logging the database connection from ODBC control panel. Sadly, that interface produces an ANSI log file - so I cannot verify UNICODE / ANSI issues using that tool.
Questions:
Is there a tool I can use to verify that these statements are being
created / issued correctly by the ODBC driver to the SQL Server
database?
Is there a better way to use ODBC so that the driver doesn't get canoodled by a simple UNICODE string in a SELECT query vs. an INSERT request?
Any other ideas for how to approach this problem (short of replacing our technology)
In the select statement, make sure you enclose the where clause string with a N to tell SQL it's unicode:
..."PATHNAME" = N'\\FILE-SERVER\Home Folders\User Files\狭すぎて丸め処理が出来ません!!.foo'
Also, MFC converts the data to MCBS or UNICODE depending on your configuration. Make sure you use CStringT in recordset.

QSqlQuery UPDATE/INSERT DateTime with server's time (eg CURRENT_TIMESTAMP)

I am using QSqlQuery to insert data into a MySQL database. Currently all I care about is getting this to work with MySQL, but ideally I'd like to keep this as platform-independent as possible.
What I'm after, in the context of MySQL, is to end up with code that effectively executes something like the following query:
UPDATE table SET time_field=CURRENT_TIMESTAMP() WHERE id='5'
The following code is what I have attempted, but it fails:
QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=? WHERE id=?");
query.addBindValue("CURRENT_TIMESTAMP()");
query.addBindValue(5);
query.exec();
The error I get is: Incorrect datetime value: 'CURRENT_TIMESTAMP()' for column 'time_field' at row 1 QMYSQL3: Unable to execute statement. I am not surprised as I assume Qt is doing some type checking when it binds values.
I have dug through the Qt documentation as well as I know how, but I can't find anything in the API designed specifically for supporting MySQL's CURRENT_TIMESTAMP() function, or that of any other DBMS.
Any suggestions?
I have no SQL server to test here, but addBindValue() binds Qt's data types. You should put the timestamp function direct into the query
QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=CURRENT_TIMESTAMP() WHERE id=?");
query.addBindValue(5);
query.exec();
should do the trick.
I don't see a way to get the database time command. I think it would be in QSqlDriver as it is specific to the database.
The only way I can think of :
QString timeCommand("CURRENT_TIMESTAMP()");
query.prepare(QString("INSERT INTO table SET time_field=%1 WHERE id=?").arg(timeCommand));
Edit : In fact, I'm not sure CURRENT_TIMESTAMP() is DB specific ...