Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » Simple U++ Sql Tutorial
Simple U++ Sql Tutorial [message #9408] Sat, 05 May 2007 17:39 Go to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Hi there,

i'm very new to upp and i'm looking for a really simple tutorial how i can connect to an sql-db (best would be via odbc or directly to an ms-sql, maybe also to my-sql).

The SQLApp Example is a little bit to complicated for me at the moment. It would be very nice if someone could explain me a real basic upp-sql example.

Michael

Edit:

i have tried the Reference Example SQL MySQL and was not able to compile. I got the following error:

In file included from J:\Entwicklung\UPP\MySQL\MySQL.cpp:1:
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:12:19: MySql.h: No such file or directory
In file included from J:\Entwicklung\UPP\MySQL\MySQL.cpp:1:
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:44: error: ISO C++ forbids declaration of `MYSQL' with no type
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:44: error: expected `;' before '*' token
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:56: error: `MYSQL' has not been declared
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:50: error: `MYSQL_PORT' was not declared in this scope
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h: In constructor `Upp::MySqlSession::MySqlSession()':
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:62: error: `mysql' undeclared (first use this function)
C:/Programme/UltimatePP/uppsrc/MySql/MySql.h:62: error: (Each undeclared identifier is reported only once for each function it appears in.
	)
MySQL: 1 file(s) built in (0:01.93), 1935 msecs / file, duration = 3750 msecs, parallelization 96%

There were errors. (0:04.67)


Seems that there is something missing... what do i have to do to get this mysql-example work?

Michael

[Updated on: Sat, 05 May 2007 19:16]

Report message to a moderator

Re: Simple U++ Sql Tutorial [message #9411 is a reply to message #9408] Sun, 06 May 2007 09:27 Go to previous messageGo to next message
mezise is currently offline  mezise
Messages: 54
Registered: April 2006
Member
Mysql libs are not bundled with TheIDE:

- go to http://dev.mysql.com/downloads/, choose Mysql version you need and while installing just check dev files option

- set correct paths to installed Mysql bin, include and lib directories (TheIDE menu: Setup/Build methods../)

- if you compile using free MSC71 in Debug mode and have error about lacking file 'libcpmtd.lib' change method e.g. to Optimal

Michal
Re: Simple U++ Sql Tutorial [message #9412 is a reply to message #9408] Sun, 06 May 2007 09:39 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
Hello Michael,

to get rid of the error you need to install the MySql development files.

SQL with upp is pretty easy, first you have to open a database then start using it like this (some simple examples)
mySQL*INSERT(MyTable)(COLUMN1,"data")(COLUMN2,"data2);
mySQL*Update(MyOtherTable)(COLUM6,"changed data").Where(NAME=="Michael");
mySQL*Delete(MyTable).Where(ToBeDelete==true);
mySQL*Select(ID).FROM(MyOtherTable).Where(NAME=="Michael");


The only thing you need is a .sch file defining to schema of your database (which tables which colums) UPP creates everything to access it then: Have a look at the SQLApp example.

Bas
Re: Simple U++ Sql Tutorial [message #9415 is a reply to message #9411] Sun, 06 May 2007 12:15 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
mezise wrote on Sun, 06 May 2007 09:27

Mysql libs are not bundled with TheIDE:

- go to http://dev.mysql.com/downloads/, choose Mysql version you need and while installing just check dev files option

- set correct paths to installed Mysql bin, include and lib directories (TheIDE menu: Setup/Build methods../)

- if you compile using free MSC71 in Debug mode and have error about lacking file 'libcpmtd.lib' change method e.g. to Optimal

Michal


Thanks alot, Michal - thats it!

Now i can compile the example, great.

And thanks alot to you, Bas. I think there will be a lot of further questions coming up... I'm very new to upp and have only basic skills in C/C++. I tried several C++ Frameworks over the years and U++ seems to be what i was looking for.

Thanks again.

Michael
Re: Simple U++ Sql Tutorial [message #9416 is a reply to message #9408] Sun, 06 May 2007 17:06 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
I now have a Schema-File where i defined my tables. How do i use it? When i use your examples i got several error because some things are undefined, like below:

Quote:

J:\Entwicklung\UPP\prohibisZA\database.cpp:25: error: `ID' undeclared (first use this function)


I know, there is the SQLApp Example... but at the moment its no help for me. I do not understand how the schema-file works with my code. I think i need a little help to get into the usage of sql and u++.

Michael
Re: Simple U++ Sql Tutorial [message #9418 is a reply to message #9416] Sun, 06 May 2007 17:49 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
this is what you need in a header file (see SQLApp.h):
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h> //not sure atm which file defines the SCHEMADIALECT for MySQL!!!
#define MODEL <SQLApp/SQLApp.sch> //your .sch file here
#include <Sql/sch_header.h>

and in a sourcefile
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h> //again you need the one for MySQL
#include <Sql/sch_source.h>


that should do the trick,
Bas
Re: Simple U++ Sql Tutorial [message #9419 is a reply to message #9418] Sun, 06 May 2007 19:15 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
Ok, found MySqlSchema and added this to my source-file:

#define SCHEMADIALECT <MySql/MySqlSchema.h>
#include "prohibisZA.h"
#include <Sql/sch_source.h>


and this to my header-file:

#define SCHEMADIALECT <MySql/MySqlSchema.h>
#define MODEL "prohibisZA.sch"
#include <Sql/sch_header.h>

but got these errors...

main.cpp
In file included from C:/Programme/UltimatePP/uppsrc/MySql/MySqlSchema.h:51,
                 from C:/Programme/UltimatePP/uppsrc/Sql/sch_header.h:35,
                 from J:\Entwicklung\UPP\prohibisZA\/prohibisZA.h:16,
                 from J:\Entwicklung\UPP\prohibisZA\mainmenu.cpp:7,
                 from C:/Programme/UltimatePP/out/prohibisZA/MINGW.Blitz.Gui.Main\$blitz.cpp:3:
C:/Programme/UltimatePP/uppsrc/Sql/sch_model.h:149:15: prohibisZA.sch: No such file or directory
C:/Programme/UltimatePP/uppsrc/GridCtrl/GridCtrl.h:916: warning: inline function `Upp::Item& Upp::GridCtrl::GetItem(int, int)' used but ne
	ver defined
In file included from C:/Programme/UltimatePP/uppsrc/MySql/MySqlSchema.h:51,
                 from C:/Programme/UltimatePP/uppsrc/Sql/sch_header.h:35,
                 from J:\Entwicklung\UPP\prohibisZA\/prohibisZA.h:16,
                 from J:\Entwicklung\UPP\prohibisZA\main.cpp:9:
C:/Programme/UltimatePP/uppsrc/Sql/sch_model.h:149:15: prohibisZA.sch: No such file or directory
C:/Programme/UltimatePP/uppsrc/GridCtrl/GridCtrl.h:916: warning: inline function `Upp::Item& Upp::GridCtrl::GetItem(int, int)' used but ne
	ver defined
prohibisZA: 4 file(s) built in (0:07.36), 1840 msecs / file, duration = 7609 msecs, parallelization 100%

There were errors. (0:08.82)


Any further ideas?

Michael

[Updated on: Sun, 06 May 2007 23:12]

Report message to a moderator

Re: Simple U++ Sql Tutorial [message #9423 is a reply to message #9419] Mon, 07 May 2007 08:15 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
did you add the Packages MySql and CtrlLib to your Application?

Bas
Re: Simple U++ Sql Tutorial [message #9425 is a reply to message #9423] Mon, 07 May 2007 15:14 Go to previous messageGo to next message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
fallingdutch wrote on Mon, 07 May 2007 08:15

did you add the Packages MySql and CtrlLib to your Application?

Bas


Hi Bas,

Yes, those packages were already added to my project.

But i've now realized my requirements like this:

I've added this packages to my application:
MySql
Sql
SqlCtrl


Added this to my header-file:
#include <SqlCtrl/SqlCtrl.h>
#include <MySQL/MySQL.h>


Added this to my constructor:
MySqlSession session;


And this is how i make my queries:
#include "prohibisZA.h"

void prohibisZA::ConnectMySQL()
{
	if(!session.Connect("prohibisZA", "password", "prohibisZA", "localhost"))
	{
    	PromptOK("Verbindungsaufbau zur Datenbank fehlgeschlagen!");
    }
}

void prohibisZA::SelectData(String sqlWhat, String sqlTable, String sqlWhere, String sqlOrder)
{
	Sql sql(session);
	sql.Execute("SELECT " + sqlWhat + " FROM " + sqlTable + sqlWhere + sqlOrder);
	while(sql.Fetch())
	{
		if(sqlTable == "prohibisZA_Kunden") {
			datagrid.Add(String(sql[1]), IntStr(sql[2]), IntStr(sql[3]), String(sql[4]), String(sql[5]), String(sql[6]));
		}
		else if(sqlTable == "prohibisZA_Server") {
			datagrid.Add(IntStr(sql[1]), String(sql[2]), String(sql[3]), String(sql[4]), String(sql[5]), String(sql[6]), String(sql[7]), String(sql[8]));
		}
	}	
}


Works good for me.

But what about the session? Now with this code i always create a new session for every query. Is this the right way?

Michael

[Updated on: Mon, 07 May 2007 15:14]

Report message to a moderator

Re: Simple U++ Sql Tutorial [message #9427 is a reply to message #9425] Mon, 07 May 2007 15:45 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

datagrid.Add(String(sql[1]), IntStr(sql[2]), IntStr(sql[3]), String(sql[4]), String(sql[5]), String(sql[6]));

BTW, I suppose datagrid is an ArrayCtrl variable. There is no need to conver sql values to string type as arrayctrl can store different type values and display them properly. This line of code should rather look like:
datagrid.Add(sql[1], sql[2], sql[3], sql[4], sql[5], sql[6]);

or like in your case if you store sql values in array ctrl one by one this row can be written simply as:
datagrid.Add(sql);


[Updated on: Mon, 07 May 2007 15:45]

Report message to a moderator

Re: Simple U++ Sql Tutorial [message #9428 is a reply to message #9427] Mon, 07 May 2007 16:40 Go to previous message
michael is currently offline  michael
Messages: 153
Registered: May 2007
Location: Germany
Experienced Member
unodgs wrote on Mon, 07 May 2007 15:45

datagrid.Add(sql[1], sql[2], sql[3], sql[4], sql[5], sql[6]);

or like in your case if you store sql values in array ctrl one by one this row can be written simply as:
datagrid.Add(sql);




good point, but what if i dont want to display the first sql value?

datagrid.Add(sql) adds all sql values from 0 to 6. can i use something like sql[1-6]? (Ok, tried but did not work)

Michael
Previous Topic: small patch for PostgreSQL
Next Topic: Sql::Execute()
Goto Forum:
  


Current Time: Thu Mar 28 14:44:02 CET 2024

Total time taken to generate the page: 0.01272 seconds