SQL / C++ Tutorial using sqlwrapped
3. Connecting to a database
PreviousNextIndex
To connect to a database, an instance of class Database is created with
the necessary connection parameters. In the case of MySQL the constructor
of the class needs the hostname where the database server is running,
user and password to gain access to the database and finally the database
name. Sqlite3 needs only the filename of the database file.
MySQL example
#include <libmysqlwrapped.h>
int main()
{
Database db("localhost", "root", "", "tutorialdb");
if (!db.Connected())
{
printf("Database not connected - exiting\n");
exit(-1);
}
}
example1_mysql.cpp
sqlite3 example
#include <libsqlitewrapped.h>
int main()
{
Database db("tutorial.db");
if (!db.Connected())
{
printf("Database not connected - exiting\n");
exit(-1);
}
}
example1_sqlite3.cpp
PreviousNext