Home » Community » Newbie corner » "simple" connection and operation with database
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 07:02:25 CEST 2025
Total time taken to generate the page: 0.04724 seconds
|