Page 1 of 1

Using Sqlite but how to draw information from the database

Posted: Fri Nov 23, 2007 1:26 am
by humbrol
ok, did alot of research and found this, was able to get sqllite connected and it all running in irrlicht. So hopefully this link will help someone else get sqlite running.

on to the question, how do i transfer the information from the database to a variable. I am able to display the information via console but would like to be able to pull information from the database and use it to create and place nodes as well as store information from within irrlicht.


http://www.yeohhs.com/articles/devcppsqlite.htm

Code: Select all

#include <irrlicht.h>

//for sqlite
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sqlite.h>



using namespace irr;
//for sqlite
using namespace std;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#pragma comment(lib, "Irrlicht.lib")

// used for sqllite

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

// end used for sqllite


int main(int argc, char **argv)
{
	
	IrrlichtDevice *device =
		createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 16,
			false, true, false, 0);

	device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
    
    // adds the first person shooter camera to the game
    // and make the cursor invisible 
	
	ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
    device->getCursorControl()->setVisible(false);
    
    

   ISceneNode* moon = smgr->addSphereSceneNode(); 
   moon->setScale(vector3df(5,5,5)); 
   moon->setPosition(vector3df(200,200,200));
   moon->setMaterialFlag(EMF_LIGHTING, false);
   moon->setMaterialTexture( 0, driver->getTexture("media/earth.bmp") );
   moon->getMaterial(0).EmissiveColor.set(1,1,0,0);
   
   scene::ISceneNodeAnimator* anim =
   smgr->createRotationAnimator(core::vector3df(0,0.01f,0));	

		moon->addAnimator(anim);

		anim->drop();


   sqlite *db;
    char *zErrMsg = 0;
    int rc;
    
    
    
    char sql[1024]="";
    sprintf(sql, "SELECT * FROM table1;");

    db = sqlite_open("sqlitepp.db", 0, &zErrMsg);
    if (db == NULL )
    {
       fprintf(stderr, "Can't open database: %s\n", zErrMsg);
       sqlite_close(db);
       system("PAUSE");
       exit(1);
    }
  
    rc = sqlite_exec(db, sql, callback, 0, &zErrMsg);
    if ( rc!=SQLITE_OK )
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
    }
    sqlite_close(db);
   
   smgr->addSkyDomeSceneNode(driver->getTexture("media/sky.jpg"),16,16,1.0f,2.0f);
   
	while(device->run())
	{
	 driver->beginScene(true, true, SColor(255,100,101,140));

		smgr->drawAll();
		 
		guienv->drawAll();

		driver->endScene();
	}

	device->drop();

	return 0;
}

[/url]

Posted: Fri Nov 23, 2007 1:43 am
by christianclavet
Wow, very nice!

A quick way could be to define your SQL as strings then convert all the data from IRRlicht to string. When retrieving data, you convert from string back to the type of desired data.

If you want to save a class, you will have to create a field and subfield.

Posted: Fri Nov 23, 2007 2:02 am
by humbrol
thats where im running into problems what would the syntax be to convert the sql to a string or integer.

what my end goal is is to


- store the x,y,z coordinates of a node in sql and then pull that information to create the nodes within my skydome.

- store the filename of the texture file for the node in the database

- store the misc information for node creation.

- more to come..

[/list]

Posted: Fri Nov 23, 2007 1:14 pm
by Acki
SQLite uses only strings (char*) so no need to convert to strings... ;)
to convert to integer or floats simply use the basic C functions atoi and atof...
for more information on this you should read a C/C++ book !!! ;)

Posted: Sat Nov 24, 2007 3:05 am
by humbrol

Code: Select all

test0
xpos = 150

couple questions, why is test0 printing before the sql calls to get xpos = 150? and why isnt test changing to test 150. I can see that its changing the value of xPos from 999 so there is some work going on,, but its also not displaying ypos and zpos in the console... soo close yet soo far..




Code: Select all

char sql[1024]="";
    sprintf(sql, "SELECT xPos FROM irrlicht where id = 1;");
    sprintf(sql, "SELECT yPos FROM irrlicht where id = 1;");
    sprintf(sql, "SELECT zPos FROM irrlicht where id = 1;");
    
    float xPos, yPos, zPos;
    xPos = 999;
    
    xPos = atof("SELECT xPos FROM irrlicht WHERE id = 2;");
     std::cout<< "TEST" << xPos <<std::endl;

Posted: Sat Nov 24, 2007 3:14 am
by humbrol
make matters worse

Code: Select all

xPos = sprintf(sql,"SELECT xPos FROM irrlicht where id = 1;");
gets me 39 where the table value is 150

Posted: Sat Nov 24, 2007 3:51 am
by Seven
Acki wrote:SQLite uses only strings (char*) so no need to convert to strings... ;)
to convert to integer or floats simply use the basic C functions atoi and atof...
for more information on this you should read a C/C++ book !!! ;)

Code: Select all

char ConvertedString[255];
char* IntToString(int i)
{
	sprintf(ConvertedString,"%d",i);
	return ConvertedString;
}

int StringToInt(char* string)
{
	return atoi(string);
}

float StringToFloat(char* string)
{
	return (float)atof(string);
}

long StringToLong(char* string)
{
	return atol(string);
}

char* RectToString(RECT r)
{
	sprintf(ConvertedString,"%d %d %d %d",r.left,r.top,r.right,r.bottom);
	return ConvertedString;
}

char* Vec3DToString(vector3df v)
{
	sprintf(ConvertedString,"%f %f %f",v.X,v.Y,v.Z);
	return ConvertedString;
}

vector3df StringToVec3D(char* string)
{
	vector3df v;
	float x,y,z;
	sscanf(string,"%f %f %f",&x,&y,&z);
	v.X = x;
	v.Y = y;
	v.Z = z;
	return v;
}

char* SColorToString(SColor c)
{
	sprintf(ConvertedString,"%d %d %d %d",c.getAlpha(),c.getRed(),c.getGreen(),c.getBlue());
	return ConvertedString;
}

SColor StringToSColor(char* string)
{
	int a,r,g,b;
	sscanf(string,"%d %d %d %d",&a,&r,&g,&b);
	SColor c(a,r,g,b);
	return c;	
}
char* SColorfToString(SColorf c)
{
	sprintf(ConvertedString,"%f %f %f %f",c.r,c.g,c.b,c.a);
	return ConvertedString;
}

SColorf StringToSColorf(char* string)
{
	float a,r,g,b;
	sscanf(string,"%f %f %f %f",&r,&g,&b,&a);
	SColorf c(r,g,b,a);
	return c;	
}

aabbox3di StringToAABBOX3DI(char* string)
{
	int x,y,z,x2,y2,z2;
	sscanf(string,"%d %d %d %d %d %d",&x,&y,&z,&x2,&y2,&z2);
	aabbox3di b(x,y,z,x2,y2,z2);
	return b;	
}

char* AABBOX3DIToString(aabbox3di b)
{
	sprintf(ConvertedString,"%d %d %d %d %d %d",b.MinEdge.X,b.MinEdge.Y,b.MinEdge.Z,b.MaxEdge.X,b.MaxEdge.Y,b.MaxEdge.Z);
	return ConvertedString;
}

aabbox3df StringToAABBOX3DF(char* string)
{
	float x,y,z,x2,y2,z2;
	sscanf(string,"%f %f %f %f %f %f",&x,&y,&z,&x2,&y2,&z2);
	aabbox3df b(x,y,z,x2,y2,z2);
	return b;	
}

char* AABBOX3DFToString(aabbox3df b)
{
	sprintf(ConvertedString,"%f %f %f %f %f %f",b.MinEdge.X,b.MinEdge.Y,b.MinEdge.Z,b.MaxEdge.X,b.MaxEdge.Y,b.MaxEdge.Z);
	return ConvertedString;
}

dimension2d<f32> StringToDim2df(char* string)
{
	float w,h;
	sscanf(string,"%f %f",&w,&h);
	dimension2d<f32> d(w,h);
	return d;	
}

char* Dim2dfToString(dimension2d<f32> d)
{
	sprintf(ConvertedString,"%f %f",d.Width, d.Height);
	return ConvertedString;
}



RECT StringToRect(char* string)
{
	int l,t,r,b;
	sscanf(string,"%d %d %d %d",&l,&t,&r,&b);
	RECT rr;
	rr.left = l;
	rr.top = t;
	rr.right = r;
	rr.bottom = b;
	return rr;
}

char* FloatToString(float f)
{
	sprintf(ConvertedString,"%f",f);
	return ConvertedString;
}

char* LongToString(long l)
{
	ltoa(l,ConvertedString,10);
	return ConvertedString;
}

Posted: Sat Nov 24, 2007 4:06 am
by humbrol
tried using atof but keep getting
102 C:\irrlicht\main.cpp invalid conversion from `char' to `const char*'

Posted: Sat Nov 24, 2007 4:13 am
by Seven
float StringToFloat(char* string)
{
return (float)atof(string);
}

Posted: Sat Nov 24, 2007 4:52 am
by vitek
make matters worse
Code:
xPos = sprintf(sql,"SELECT xPos FROM irrlicht where id = 1;");

gets me 39 where the table value is 150
That is because sprintf returns the number of characters put into the buffer named sql. You've effectively written

Code: Select all

const char buf[] = "SELECT xPos FROM irrlicht where id = 1;";
char sql[1024];

strcpy (sql, buf);
xPos = strlen (sql);
Travis

Posted: Sat Nov 24, 2007 4:58 am
by fireside
float StringToFloat(char* string)
{
return (float)atof(string);
}
That probably works but I don't quite understand it. A constant can't be changed but you can make a string variable equal to the constant and then change the string with atof. Oh, It's because it makes a copy when it gets sent.