Game Development Community

Updated ODBC support for TGE 1.4, TGB 1.1 and TSE ms4

by Jonathon Stevens · 02/01/2006 (5:01 pm) · 216 comments

Download Code File

In this section I'll show you how to install the TGE Mac/Lin/Win MySQL ODBC resource and updating it to include support for Remote MySQL, SQL Server 2000/2005, and Oracle. Thanks to OneST8 for the original resource found here: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7890. Additional credits are in the source code files. YOU DO NOT complete the aformentioned resource. This is meant to replace it.


*********************************************************************************
UPDATE:: I also have two .sql files included: MySQL.sql and SQLServer2005.sql. You can run them from their respective database to get a basic database layout to test with.

UPDATE 2-3-06: I have now updated this resource to include stored procedure support. Simply copy the new utils.cs file over the one you have to update.

UPDATE 3-2-06: I have added the ability to clear the results set as well as a row counter. I've also added stored procedure support. I have NOT tested sprocs on MySQL, only on SQL Server 2005. If someone could test that'd be great.

UPDATE 7-8-06: I have updated the resource to account for the CTD issue when closing the game as well as random other issues (int issue) and some bug fixing. This now also works on TGE 1.4 and TSE ms3.5.

UPDATE 7-8-06b: Few more small fixes and updated to work on Mac OS now!

UPDATE 7-9-06: Added more steps to cover things such as the SQL libs and MySQL ODBC Drivers, etc.

UPDATE 7-28-06: Changed all global results variables to local. Thanks Dreamer!

UPDATE 9-1-06: Few small fixes and addition of prepared statements thanks to David Michael! Updated the test script to allow testing of both prepared and non-prepared statements. Set $db_test_prepared to true to test prepared statements.

UPDATE 10-17-06: A few more bug fixes.

This is confirmed working for Linux, Mac OS X and Windows. DB wise it has been confirmed working on dedicated and local instances with MySQL 5.0+ and on MS SQL Server 2005 express and pro.

I still need some testers for other DB systems to verify functionality, please email me or post here if you have any results to add.
*********************************************************************************



#1 - (download zip) Download the attached zip file. Extract the following files into your engine/console folder:
databaseConnection.cc
databaseConnection.h
databaseResult.cc
databaseResult.h
databasePrepared.cc
databasePrepared.h

#1.2 - Copy the 'db' directory into your 'starter.rpg/server/scripts/' folder.

#1.3 - Make sure you have the ODBC library linked in your project Windows users should already have this on their computer. Linux users just do a search for it on the web and download from there (someone wanna post a link for this?)

#2 - (Edit Scripts) Open up 'server/scripts/game.cs' and below the exec("./aiPlayer.cs"); line add exec("./db/init.cs");

#2.1 - Open up 'server/defaults.cs' and at the bottom add:

//-----------------------------------------------------------------------------
// ODBC Database connection prefs
//-----------------------------------------------------------------------------

$Pref::Server::DB::Software = 0; // 0 - MySQL Local, 1 - MySQL Remote, 2 - SQL Serv 2000, 3 - SQL Serv 2005, 4 - Oracle
$Pref::Server::DB::Port = ""; // DB Port number
$Pref::Server::DB::Server = "localhost"; // Server's url or ip
$Pref::Server::DB::Database = "starter_rpg"; // Database name
$Pref::Server::DB::User = "TESTER"; // username
$Pref::Server::DB::Password = "password"; // password

//check platform for which driver to use
if ($platform $= "windows")
{
//check database they are using
switch($Pref::Server::DB::Software)
{
case 0:
$Pref::Server::DB::Driver = "MySQL ODBC 3.51 Driver";
case 1:
$Pref::Server::DB::Driver = "MySQL ODBC 3.51 Driver";
case 2:
$Pref::Server::DB::Driver = "SQL Server";
case 3:
$Pref::Server::DB::Driver = "SQL Native Client";
case 4:
$Pref::Server::DB::Driver = "Microsoft ODBC for Oracle";
default:
$Pref::Server::DB::Driver = "ERROR: UNKNOWN DATABASE:" SPC $Pref::Server::DB::Database;
}
}
else
{
//check database they are using
switch($Pref::Server::DB::Software)
{
case 0:
$Pref::Server::DB::Driver = "MySQL";
case 1:
$Pref::Server::DB::Driver = "MySQL";
case 2:
$Pref::Server::DB::Driver = "ERROR: SQL SERVER NOT SUPPORTED BY LINUX";
case 3:
$Pref::Server::DB::Driver = "ERROR: SQL SERVER NOT SUPPORTED BY LINUX";
case 4:
$Pref::Server::DB::Driver = "Oracle";
default:
$Pref::Server::DB::Driver = "ERROR: UNKNOWN DATABASE:" SPC $Pref::Server::DB::Database;
}
}

#2.2 - Make sure you install ODBC drivers for the database you are going to use. MySQL for all platforms can be found at: dev.mysql.com/downloads/connector/odbc/3.51.html. If you are going to use MS SQL Server, the drivers will already be installed on your machine.

FOR LINUX ONLY:

#2.3 - Open engine/targets.torque.mk in the section for dedicated server build replace:
$(DIR.OBJ)/$(EXE_DEDICATED_NAME)$(EXT.EXE): LINK.LIBS.GENERAL = \
with
$(DIR.OBJ)/$(EXE_DEDICATED_NAME)$(EXT.EXE): LINK.LIBS.GENERAL = -lodbc \

END LINUX ONLY BIT

#3 - (compile!) In solution explorer browse to Torque Demo->console and right-click and select add->existing item. Browse to your engine/console folder and select the four database files we extracted in step 1. Once done, compile the solution.

That's it! Now we're ready to set the database up. On the next tutorial I'll show you how to set up our database structure for MySQL and SQL Server.

About the author

With a few casual games under his belt as CEO of Last Straw Productions, Jonathon created the increasingly popular Indie MMO Game Developers Conference which. Following the success of IMGDC a new MMOG fan event called LFG Expo will debut in June of 2010.

Page«First 5 6 7 8 9 10 11 Next»
#201
04/03/2008 (10:06 am)
Hi all,
I am getting this error when i run torque, any help would be appreciated

*******************************************************************************
***** Initializing the database...
***** Using ODBC Driver: MySQL ODBC 3.51 Driver
PlayWorld/server/db/init.cs (44): Unable to instantiate non-conobject class DatabaseConnection.
***** software: 0
****** driver: MySQL ODBC 3.51 Driver connectionstring server=localhost;database=starter_rpg;user=TESTER;password=password;option=3;
PlayWorld/server/db/init.cs (66): Unable to find object: '0' attempting to call function 'ConnectDriver'
***** Database error received while trying to connect to database.
***** End of database initialization.
*******************************************************************************

I couldn't find the Torque Demo->console in the last step i think this might be causing the error, any hep would be appreciated.
#202
07/18/2008 (6:35 pm)
Wow! Painless installation, perfect results on the first attempt. Bravo, Jonathon!
#203
09/04/2008 (1:04 pm)
Heyas guys!

I had a recent problem, where a HD made me reinstall the PC (which dropped to the floor). So at this new one, I had to install MsVC++2005E, and a lot of other stuff. Trying to compile the project gave me that 'missing sql.h file' error ppl mention.

Doing some research and going in deep with the 'include and link the MS SDK' I did the includes mentioned here and now it compiles as a charm.

(Just in case others has/have/gets the problem) ;)
#204
06/16/2009 (12:10 am)
How can I do MS SQL 2008 connect string?
My Database isn't SystemDatabase=>master=>dbo.XXX
My error message...
*******************************************************************************
***** Initializing the database...
***** Using ODBC Driver: SQL Server Native Client 10.0
***** software: 7
***** %connectionString = server=localhost;database=XXXX;
****** driver: SQL Server Native Client 10.0 connectionstring server=localhost;database=XXXX;uid=username;pwd=password;
DatabaseConnection::Connect - SQL Error - state(S1000):errRow(1):native(1045) - [MySQL][ODBC 5.1 Driver]Access denied for user 'username'@'localhost' (using password: YES)
DatabaseConnection::Connect - SQL Error - state(S1000):errRow(2):native(1045)
Call $db.ConnectDriver($Pref::Server::DB::Driver, %connectionString)
$Pref::Server::DB::Driver=SQL Server Native Client 10.0
%connectionString=server=localhost;database=XXXX;uid=username;pwd=password;
***** Database error received while trying to connect to database.
***** End of database initialization.
*******************************************************************************
#205
06/17/2009 (10:49 pm)
I tried to use this API.
ret = SQLDriverConnectW(mDBC, GetDesktopWindow(), (SQLWCHAR*)pszConnStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
And popup ODBC manager.Connected! But I think this is not a good idea.
#206
06/29/2009 (1:51 pm)
Hi Everyone,

I managed to get this to compile and it connects to the database. However, the only table it will actually work with is the gameAccounts table. I imported the table example in the download db to one of my network SQL servers and i can manipulate the data there and it works with the gameAccounts table below so I know its connecting to the DB.

This works
%query="Select password FROM gameAccounts WHERE userid=1;";
%results = $db.Execute( %query );
%passw=%results.password;

This does not
%query="Select itemname FROM idtA WHERE ItemID=1001;";
%results = $db.Execute( %query );
%tn=%results.itemname;

Actually, it crashes with Torque Memory Error - Error Allocating Memory. Shutting down. Depending on the table i try to select it either crashes or returns a result but nothing is in the result even though looking at the table it should.

I'm sure this is probably something simple that i'm just not seeing but I've been unsuccessful in figuring out whats going on. Anybody else had anything similar that they could share?

Thanks everyone.
#207
07/23/2009 (1:31 pm)
ok, so I got the above issue figured out. Apparently, when your table is created you need to SET ANSI_PADDING ON. I found it in the SQL for the table. It was not setting by default so if your not getting results from your query try checking your Databases Table SQL. So, for example, the default gameAccounts database looks similar to this:

USE [yourdatabasename]
GO

/****** Object: Table [dbo].[gameAccounts] Script Date: 07/23/2009 13:22:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[gameAccounts](
[userID] [int] IDENTITY(1,1) NOT NULL,
etc ...

SET ANSI_PADDING OFF
GO

It seems that ANSI_PADDING needed to be turned on in order for me to grab some of the column data. Hopefully this helps someone else.
#208
09/25/2009 (3:00 pm)
Where is all the producers we can do with this resource? I see them nowhere listed.
#209
09/26/2009 (3:56 am)
%query="Select password FROM gameAccounts WHERE userid=1;";
%results = $db.Execute( %query );


this is for directly executing what is found to the client correct?
#210
10/07/2009 (11:12 pm)
hmm... time to create an ODBC resource for T3D
#211
10/18/2009 (2:55 am)
Hi, I am using VS 2008 and TGEA 1.8.1. And when I start compiling. These errors appears. Any help? Thanks..

Warning 4 warning C4101: 'tempFloat' : unreferenced local variable e:\desktop\thesis game\engine\source\console\databaseresult.cpp 116
Warning 5 warning C4101: 'valSize' : unreferenced local variable e:\desktop\thesis game\engine\source\console\databaseresult.cpp 119
Warning 6 warning C4101: 'strValue' : unreferenced local variable e:\desktop\thesis game\engine\source\console\databaseresult.cpp 108
Warning 7 warning C4101: 'tempDouble' : unreferenced local variable e:\desktop\thesis game\engine\source\console\databaseresult.cpp 117
Warning 8 warning C4101: 'tempInt' : unreferenced local variable e:\desktop\thesis game\engine\source\console\databaseresult.cpp 115
Warning 9 warning C4189: 'err' : local variable is initialized but not referenced e:\desktop\thesis game\engine\source\console\databaseresult.cpp 495
Warning 10 warning C4189: 'err' : local variable is initialized but not referenced e:\desktop\thesis game\engine\source\console\databaseresult.cpp 502
Warning 11 warning C4189: 'err' : local variable is initialized but not referenced e:\desktop\thesis game\engine\source\console\databaseresult.cpp 509
Warning 12 warning C4189: 'err' : local variable is initialized but not referenced e:\desktop\thesis game\engine\source\console\databaseresult.cpp 516
Warning 13 warning C4701: potentially uninitialized local variable 'ret' used e:\desktop\thesis game\engine\source\console\databaseresult.cpp 477
#212
10/18/2009 (1:04 pm)
those aren't errors, but warnings and can be ignored.
#213
10/20/2009 (8:50 am)
Hi, I'm using this resource in T3D 1.0 and it works correctly, but I have some questions about "where" to use.

Which is the best side to do querys? Client-Side or Server-Side? I think that is Server-Side but I don't know how to send the results to the client to do something because if Server send the results with 'commandtoclient' function to Client, this client "don't know" what is %Results.nextRow(), for example.

Anybody knows what is the best way to do this?

Thanks!
#214
12/18/2009 (3:35 am)
Please someone help me eliminate the warnings. Even though they can be ignored. I want to eliminate these warnings. This are the local variable and I am not good in C++.

tempFloat
valSize
strValue
tempDouble
tempInt
err
ret

Detailed info about it are in comment #211. Thanks in advance.
#215
07/17/2010 (9:53 am)
Works perfectly in TGEA 1.8.1, but LOTS of errors in TGE 1.5.2. According to the first post, it should work in TGE!!

This is only a small collection of the errors:
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(137) : error C2143: syntax error : missing ')' before ';'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(163) : error C2061: syntax error : identifier '__out_xcount_opt'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(168) : error C2059: syntax error : ')'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(168) : error C2143: syntax error : missing ')' before ';'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(198) : error C2061: syntax error : identifier '__out_xcount_opt'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(203) : error C2059: syntax error : ')'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(203) : error C2143: syntax error : missing ')' before ';'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(554) : error C2061: syntax error : identifier '__out_xcount_opt'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(558) : error C2059: syntax error : ')'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(558) : error C2143: syntax error : missing ')' before ';'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(573) : error C2061: syntax error : identifier '__out_xcount_opt'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(577) : error C2059: syntax error : ')'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(577) : error C2143: syntax error : missing ')' before ';'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(605) : error C2061: syntax error : identifier '__out_xcount_opt'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(609) : error C2059: syntax error : ')'
c:program filesmicrosoft sdkswindowsv7.0includesqlucode.h(609) : error C2143: syntax error : missing ')' before ';'
..engineconsoledatabaseConnection.cc(194) : error C2660: 'SQLSetStmtAttr' : function does not take 4 arguments
..engineconsoledatabaseConnection.cc(200) : error C2660: 'SQLSetStmtAttr' : function does not take 4 arguments

Also:
Quote:
Browse to your engine/console folder and select the four database files we extracted in step 1

There are 6. ;)
#216
01/26/2011 (11:48 am)
I know this is a long shot... but does this "ODBC support for TGE 1.4, TGB 1.1 and TSE ms4" work with Torque 3D beta 3+?

-al
Page«First 5 6 7 8 9 10 11 Next»