0

I'm connecting to an MS SQL table (from Linux) using the Microsoft ODBC driver. Note that if "MyTable" points to a table with no spaces in the columns, everything is fine. For the other table (the one I need to be reading from) things go pear-shaped, around the time it gets to the first column with a space. The error I get out of lastError().text() is something like:

 ERROR: [Microsoft][ODBC Driver 11 for SQL Server][SQL  "U Max", "U Max_Inst", "U  [Microsoft][ODBC Driver 11 for SQL Serve][SQL  "U Max_I2", "U Max_I3 QODBC3: Unable to execute statement

I've definitely been given better errors in my life, but it's a least a hint.

Code:

SqlTableModel::SqlTableModel( QObject* parent, QSqlDatabase db ) : QSqlTableModel( parent, db )
{

}

auto SqlTableModel::initialize() -> void
{
    if ( !database().open() ) {
        QSqlError error = database().lastError();
        std::cout << "ERROR: Couldn't open database: " << error.text().toStdString() << std::endl;
        return;
    }

    getRows();
}

auto SqlTableModel::getRows() -> void
{
    setTable( "MyTable" );
    if ( select() ) {
        std::cout << "selected returned true." << std::endl;
    } else {
        std::cout << "failed to select" << std::endl;
        std::cout << "ERROR: " << lastError().text().toStdString() << std::endl;
    }
}

If I wanted to try to work around this and manually select only certain columns from the table (IE, with a query) do I need to do so with a QSqlQueryModel and then call setData() for each adjacent record?

Mike Gardner
  • 6,611
  • 5
  • 24
  • 34
kiss-o-matic
  • 1,111
  • 16
  • 32
  • MySQL has nothing to do with MS SQL. Please remember to pay attention when tagging your questions. – Mike Gardner Nov 03 '15 at 15:08
  • 1
    In SQL Server, when object names have spaces in the names, they need to be referenced with surrounding square braces like this `[Table Name]` – Tab Alleman Nov 03 '15 at 16:04
  • @Michael - indeed. For what it's worth, it was a suggested tag. I know that doesn't make it okay though. – kiss-o-matic Nov 03 '15 at 16:40
  • @Tab, the table name has no spaces. The columns, however, do. I'm not sure what's going on under the sheets... I'm simply calling select(). – kiss-o-matic Nov 03 '15 at 16:42
  • Yes, same rule applies to column names. I also don't know what's going on behind the scenes though. I have no expertise with QSql. Do you define column names somewhere in this model? – Tab Alleman Nov 03 '15 at 16:48

0 Answers0