VTK/Examples/Cxx/Broken/Databases/ODBCDatabase
From KitwarePublic
Jump to navigationJump to search“Could not execute statement" error.
Contents
ODBCDatabase.cxx
//This is the format for using the Microsoft Access ODBC Driver connection string.
//DRIVER={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;
//The keyword DRIVER must be in all caps for vtkODBCDatabase to work with any data source with the word "Driver" in it.
//Many odbc examples online have it in lower case.
//Create Database object
vtkODBCDatabase* db = vtkODBCDatabase::New();
//Set the data source name. Do not include the user id or password in this string. They are set with separate functions.
db->SetDataSourceName("DRIVER={Microsoft Access Driver (*.mdb)};Dbq=C:\\mydatabase.mdb;");
db->SetUserName("Admin");
db->SetPassword("password");
//Open the database
bool status = db->Open(NULL);
//Check if we successfully opened the database
if(!status)
{
const char* err = db->GetLastErrorText();
return;
}
//Retrieve the usable query object from the database instance
vtkSQLQuery* query = db->GetQueryInstance();
//Set the query text. This can be any valid SQL statement that the database supports
query->SetQuery("Select CenterLon, CenterLat FROM Sites WHERE SiteID = 'KUEX'");
//Execute the query. There are several options for retreiving data (fill tables, get rows, ...)
if (query->Execute())
{
while(query->NextRow())
{
latlon[0] = query->DataValue(0).ToDouble();
latlon[1] = query->DataValue(1).ToDouble();
}
}
else
{
const char* err = query->GetLastErrorText();
}
db->Close();
query->Delete();
db->Delete();
Please try the new VTKExamples website.
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
PROJECT(ODBCDatabase)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(ODBCDatabase MACOSX_BUNDLE ODBCDatabase.cxx)
if(VTK_LIBRARIES)
target_link_libraries(ODBCDatabase ${VTK_LIBRARIES})
else()
target_link_libraries(ODBCDatabase vtkHybrid vtkWidgets)
endif()
Download and Build ODBCDatabase
Click here to download ODBCDatabase. and its CMakeLists.txt file.
Once the tarball ODBCDatabase.tar has been downloaded and extracted,
cd ODBCDatabase/build
- If VTK is installed:
cmake ..
- If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./ODBCDatabase
WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.