#include #include #include using namespace std; bool configureTable(QSqlDatabase& db) { QString sqlCommand = "CREATE DATABASE IF NOT EXISTS test_point_db;"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return false; } } sqlCommand = "USE test_point_db;"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return false; } } sqlCommand = "CREATE TABLE `test_point_tbl` (`lonlat_point` POINT NULL) ENGINE = InnoDB;"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return false; } } sqlCommand = "INSERT INTO test_point_tbl(lonlat_point) VALUES(ST_GeomFromText('POINT(1 1)'));"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return false; } } return true; } bool cleanupTable(QSqlDatabase& db) { // cleanup QString sqlCommand = "DROP DATABASE test_point_db;"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return false; } } return true; } int main(int argc, char *argv[]) { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setUserName("root"); db.setPassword("ROOT_PASSWORD_HERE"); if (!db.open()) { cout << QObject::tr("could not open QSqlDatabase: %1").arg(db.lastError().text()).toLatin1().data(); return false; } if (!configureTable(db)) { return -1; } QVariant::Type t; QString sqlCommand = "SELECT * FROM test_point_tbl;"; { QSqlQuery sqlQuery(db); if (!sqlQuery.exec(sqlCommand)) { cout << QObject::tr("SQL query failed: %1\nSQL:%2").arg(sqlQuery.lastError().text()).arg(sqlCommand).toLatin1().data(); return -1; } t = sqlQuery.record().field(0).type(); } if (!cleanupTable(db)) { return -1; } // BUG: t is coming in as a string. It should probably come in as a binary. assert((t == QVariant::Type::ByteArray) || (t == QVariant::Type::Point)); }