MySQL and VC++ 
Author Message
 MySQL and VC++

Hello,
    I know nothing about sql or how to put
it in my programs. I read over the referance
manual that came with MySQL, but it wasn't
thorough enough to get things going.
    I went on this endevour because my friends
tell me that I can use SQL in my C++ programs
to keep track of data in a multiplayer game I am
writing with an ongoing world.
    I am also going to take a class next semestre
on "Oracle concepts and developement" will using
MySQL coincide with what I am to learn? i.e.
is one database the same as another?
    Everything I see on the web uses
"MFC ODBC classes" I can't use MFC in my
game programs, it's not efficient in that context.
I much prefer the Win32 API.

    My questons are:
Can I use sql in my C++ program without MFC?

Are there any tutorials on both SQL
and how to use it in a C++ program without MFC?

Is MySQL a good choice for this endeavor or is
there something better for free out there?

    Thanx,
        Christopher



Tue, 26 Oct 2004 13:52:01 GMT  
 MySQL and VC++


Quote:
>     My questons are:
> Can I use sql in my C++ program without MFC?

Yes.

I use ODBC, because it's the easiest and included in the platform SDK

Quote:
> Are there any tutorials on both SQL
> and how to use it in a C++ program without MFC?

SQL:
Jim Hoffman's Introduction to Structured Query Language
http://neural.cs.nthu.edu.tw/jang/sandbox/www+db/download/sqltut.htm

ODBC:
Platform SDK ODBC documentation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc...
sdkodbcoverview.asp

Also, there is a newsgroup called microsoft.public.platformsdk.databse, but
it doesn't seem much used.

Assuming you have a datasource named "ProjectManager" (You can create one
from the Control Panel's ODBC Data Source Administrator if you have Visual
C++, or you can do so from within ODBC if not), the following example code
may be of use:

Note: This is a very basic app, with all error checking and most comments
stripped for brevity. Also, it may make more sense after you read through
the documentation.

//--------------------------------------
// ODBC DEMO

#include <windows.h>
#include <stdio.h>

// sql libraries
#include <sql.h>
#include <sqlext.h>

// EnvironmentHandle
class Environment
{
 SQLHENV  m_Environment ;
public:
 Environment(void)
 {
  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_Environment);
  SQLSetEnvAttr( m_Environment, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_UINTEGER );
 }
 virtual ~Environment(void)
 {
  SQLFreeHandle(SQL_HANDLE_ENV, m_Environment) ;
 }
 operator SQLHENV () { return m_Environment ; }

Quote:
} ;

// ConnectionHandle
class Connection
{
 Environment  m_Environment ;
 SQLHDBC   m_Connection ;
public:
 Connection()
 {
  SQLAllocHandle(SQL_HANDLE_DBC, m_Environment, &m_Connection) ;
  SQLConnect( m_Connection, (SQLCHAR *)"ProjectManager", SQL_NTS, NULL,
SQL_NTS, NULL, SQL_NTS ) ;
 }
 virtual ~Connection()
 {
  SQLDisconnect(m_Connection) ;
  SQLFreeHandle(SQL_HANDLE_DBC, m_Connection) ;
 }
 operator SQLHDBC () { return m_Connection ; }

Quote:
} ;

// StatementHandle
class Statement
{
 SQLHSTMT  m_Statement ;
 Connection  m_Connection ;

public:
 Statement(void)
 {
  m_Statement = SQL_NULL_HSTMT ;
  SQLAllocHandle(SQL_HANDLE_STMT, m_Connection, &m_Statement);
 }
 virtual ~Statement()
 {
  SQLFreeHandle(SQL_HANDLE_STMT, m_Statement) ;
 }
 void ExecuteStatement(const char *StatementString)
 {
  SQLExecDirect(m_Statement, (SQLCHAR *)StatementString, SQL_NTS) ;
 }
 operator SQLHSTMT () { return m_Statement ; }

Quote:
} ;

// Function to retrieve database info
long GetDatabaseInfo(Statement &statement) ;

/***************************
 * main
 *
 ***************************/
int main(int argc, char* argv[])
{
 Statement statement ;
 statement.ExecuteStatement("CREATE TABLE TestTable ( Name TEXT, Code
SMALLINT)") ;
 GetDatabaseInfo(statement) ;
 printf("Press any key to quit\n") ;
 getchar() ;
 return 0;

Quote:
}

//------------------------
// GetDatabaseInfo
//
long GetDatabaseInfo(Statement &statement)
{
 const int  STR_LEN = 129 ;
 const int  REM_LEN = 255 ;

 SQLRETURN sql_return_code;

 /* Declare buffers for result set data */
 SQLCHAR  table_name[STR_LEN] ;
 SQLCHAR  column_name[STR_LEN];
 SQLCHAR  type_name[STR_LEN] ;

 /* Declare buffers for bytes available to return */
 SQLINTEGER table_buffer ;
 SQLINTEGER column_buffer ;
 SQLINTEGER type_buffer ;

 printf("\t\tNOTICE: Getting database info...\n") ;

 SQLColumns(statement,
     NULL, 0,      /* All catalogs */
     NULL, 0,      /* All schemas   */
     NULL, 0,  /* All tables */
     NULL, 0);     /* All columns  */

 /* Bind columns in result set to buffers */
 SQLBindCol(statement, 3, SQL_C_CHAR, table_name, STR_LEN,&table_buffer);
 SQLBindCol(statement, 4, SQL_C_CHAR, column_name, STR_LEN, &column_buffer);
 SQLBindCol(statement, 6, SQL_C_CHAR, type_name, STR_LEN, &type_buffer);

 sql_return_code = SQLFetch(statement) ;
 while ( sql_return_code != SQL_NO_DATA )
 {
  printf("\t\t+----------------------\n") ;
  printf("\t\t| TableName: %s\n", (char *) table_name ) ;
  printf("\t\t| \tColumnName: %s\n", (char *) column_name) ;
  printf("\t\t| \tTypeName: %s\n", (char *) type_name ) ;
  printf("\t\t+----------------------\n") ;
  sql_return_code = SQLFetch(statement) ;
 }
 return 1 ;

Quote:
}

//------------- EOF ------------

--
Regards,
Ryan Ginstrom



Sun, 07 Nov 2004 16:52:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How do I connect with MySQL by VC?

2. MySQL from VC 6.0

3. MySQL from VC 6.0

4. MySQL from VC 6.0

5. Embedded MySQL / VC.NET 2003 / MFC

6. Connecting to MySQL database on Linux server using VC++6.0

7. connecting to MySql with C#

8. C# and mySQL

9. MySQL and .NET

10. How can I implement mysql in DOTNET?

11. Data grid & mysql

12. MySQL++ API

 

 
Powered by phpBB® Forum Software