#include #include #include #include #include #include #include static const QString dbName("Test"); int exec() { QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", dbName); db.setDatabaseName("Driver={SQL Server Native Client 11.0};Server=WIN-Q93798GT690;Database=QtTest;"); if (!db.open("sa", "sapwd")) return 1; if (!db.tables().contains("TestTable")) { QSqlQuery qry = db.exec("CREATE TABLE TestTable (id int IDENTITY(1,1) NOT NULL, smallcol VARCHAR(1))"); if (qry.lastError().type() != QSqlError::NoError) { qDebug("Error creating table: %s", qPrintable(qry.lastError().databaseText())); return 2; } } QSqlField smallCol("smallcol"); smallCol.setValue(QVariant(QString(QChar('F')))); QSqlRecord record; record.append(smallCol); // Prepare query QSqlDriver *d = db.driver(); Q_ASSERT(d); QString queryString = d->sqlStatement(QSqlDriver::InsertStatement, "TestTable", record, true); QSqlQuery qry(db); if (!qry.prepare(queryString)) return 2; qry.bindValue(0, smallCol.value()); if (!qry.exec()) { qWarning("Error inserting record: %s", qPrintable(qry.lastError().driverText())); return 3; } qDebug("Record inserted, ok"); return 0; } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); int ret = exec(); QSqlDatabase::removeDatabase(dbName); return ret; }