Home » Community » Newbie corner » "simple" connection and operation with database
"simple" connection and operation with database [message #55217] |
Tue, 20 October 2020 04:19  |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi, I have difficulties to assimilate the evolution of U ++. In this way I come to ask for your help:
could show an example, how to connect to the sqlite3 and mysql databases, without using the ".sch" structure, that is, in the conventional way.
for example how to read a vector from a data class and transfer it to the database using an insert, update, delete and then the reverse process: read the data set and transfer it to the class vector.
I think that to understand the current structure, I have to start from an old structure. This will make it less laborious.
thanks
|
|
|
Re: "simple" connection and operation with database [message #55222 is a reply to message #55217] |
Tue, 20 October 2020 15:44  |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi, this work!
(i.e. console)
#include <MySql/MySql.h>
using namespace Upp;
int main ( int argc, const char *argv[] )
{
MySqlSession session;
if ( session.Connect ( "root", "10", "test" ) )
{
Cout() << "Connected\n";
Sql sql ( session );
const String dq="\"";
try
{
sql.Begin();
for ( int i = 0; i < 10; i++ )
sql.Execute ( "insert into test_table(ID, VALUE) values (?, ?)", i, AsString ( 3 * i ) );
sql.Clear();
String a = "";
String b = "";
sql.Execute ( "SELECT * FROM test_table" );
while ( sql.Fetch() )
{
Value v;
sql.GetColumn ( 0, v );
b << AsString ( sql["ID"] ) + " , " + AsString ( sql["VALUE"] ) << "\n";
a += b;
}
LOG ( a );
sql.Clear();
sql.Execute ( "UPDATE test_table Set VALUE="+dq+"zero"+dq+" where VALUE="+dq+"9"+dq);
sql.Execute ( "DELETE FROM test_table where VALUE="+dq+"3"+dq+" or VALUE="+dq+"18"+dq);
sql.Commit();
}
catch ( SqlExc &ex )
{
Cerr() << "ERROR: " << ex << "\n";
SetExitCode ( 1 );
}
}
else
{
Cerr() << "ERROR: Unable to connect to database\n";
SetExitCode ( 1 );
}
SetExitCode ( 0 );
}
and from now on, the next step would be this example using SQL Schema Files!
|
|
|
Goto Forum:
Current Time: Sun May 11 01:43:38 CEST 2025
Total time taken to generate the page: 0.03123 seconds
|