Jump to content
  • Advertisement
Sign in to follow this  
DMINATOR

choosing right database

This topic is 4752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am working on a program right now, and having some difficulties. I am trying to understand how can I access or create database in C++ .I am using Visual C++ 6.0. I have searched through the web but couldn't find the working example. P.S. and also the database should process the SQL commands. Thanks for any response.

Share this post


Link to post
Share on other sites
Advertisement
There are many ways.

One is to use the ODBC interface, or the OLE database interface. Do a search on msdn.microsoft.com for those terms and you'll find lots of hits.

Another is to use a database-specific interface for your database of choice. Examples include MySQL or Postgres, which are open source (and thus free to download and use), or Oracle, which is expensive. Try http://www.mysql.com/ or http://www.postgres.org/ for starters.

A third is to bundle a SQL database library in your program (as long as you don't need multi-user capabilities). SQLite is popular for this -- it does most of SQL, and only adds about 40 kB to your program. You get the sources, and add them to your program, and you're done. http://www.sqlite.org/

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
There are many ways.

One is to use the ODBC interface, or the OLE database interface. Do a search on msdn.microsoft.com for those terms and you'll find lots of hits.

Another is to use a database-specific interface for your database of choice. Examples include MySQL or Postgres, which are open source (and thus free to download and use), or Oracle, which is expensive. Try http://www.mysql.com/ or http://www.postgres.org/ for starters.

A third is to bundle a SQL database library in your program (as long as you don't need multi-user capabilities). SQLite is popular for this -- it does most of SQL, and only adds about 40 kB to your program. You get the sources, and add them to your program, and you're done. http://www.sqlite.org/


Thanks I looked at mysql , but API doesn't seem to support VS 6.0. So I am now searching for any working example for ODBC or OLE , but I can't seem to find anything on msdn.

[Edited by - DMINATOR on May 21, 2005 11:31:44 AM]

Share this post


Link to post
Share on other sites
OK I have found the cause of my problems with DAO. Here is the version that works under Visual studio 6.0


// PROJECT->SETTINGS->GENERAL -> Use MFC as shared DLL - option required


#include <stdio.h>
#include <afxdao.h>


void CreateDB()
{
AfxGetModuleState()->m_dwVersion = 0x0601;

CDaoDatabase *db = new CDaoDatabase();
CDaoTableDef *tbldef;
// CDaoRecordset recset(db);


db->Create( "C:\\test.mdb" );
tbldef = new CDaoTableDef( db );

//let's create table with 2 fields
tbldef->Create( "TABLE_1" );
tbldef->CreateField( "FIELD_1", dbText, 16, dbFixedField );
tbldef->CreateField( "FIELD_2", dbText, 16, dbFixedField );

//update tabledef
tbldef->Append();

db->Execute("INSERT INTO TABLE_1(FIELD_1,FIELD_2) values('x1','y1')");
db->Execute("INSERT INTO TABLE_1(FIELD_1,FIELD_2) values('x1','y1')");

tbldef->Close();

delete tbldef;
delete db;

AfxDaoTerm();
}

void ReadDB()
{
//counter
int i = 0;
AfxGetModuleState()->m_dwVersion = 0x0601;


COleVariant var1,var2; // Start Variables

var1.ChangeType(VT_BSTR, NULL);
var2.ChangeType(VT_BSTR, NULL);


CDaoDatabase cDaoDB;
CDaoRecordset recset(&cDaoDB);


cDaoDB.Open("C:\\test.mdb");

printf(" OK database opened \n");


recset.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * from TABLE_1",NULL);

printf(" OK recordset \n");


while(!recset.IsEOF()) // Do until EOF
{
i++;

recset.GetFieldValue("FIELD_1",var1);
recset.GetFieldValue("FIELD_2",var2);

printf(" %d = FIELD_1 - %s , FIELD_2 - %s \n",i , (LPCSTR)var1.pbstrVal , (LPCSTR)var2.pbstrVal);

recset.MoveNext();
}

recset.Close();

cDaoDB.Close();
AfxDaoTerm();

printf(" OK closed \n");

}


int main()
{




ReadDB();


return 0;
}



[Edited by - DMINATOR on May 22, 2005 4:45:05 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!