Using Sqlite but how to draw information from the database

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Using Sqlite but how to draw information from the database

Post 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]
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post 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.
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post 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]
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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 !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post 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;
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post 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
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post 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;
}
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

tried using atof but keep getting
102 C:\irrlicht\main.cpp invalid conversion from `char' to `const char*'
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

float StringToFloat(char* string)
{
return (float)atof(string);
}
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
fireside
Posts: 158
Joined: Thu Dec 01, 2005 10:55 pm

Post 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.
Post Reply