https://github.com/jamesql/cplusplus-mysql-basic
Basic Setup for MySQL For C++
https://github.com/jamesql/cplusplus-mysql-basic
basic cpp lib library mysql setup setup-cpp setup-mysql sql
Last synced: about 1 month ago
JSON representation
Basic Setup for MySQL For C++
- Host: GitHub
- URL: https://github.com/jamesql/cplusplus-mysql-basic
- Owner: jamesql
- Created: 2019-09-12T01:48:53.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-12T02:07:39.000Z (almost 7 years ago)
- Last Synced: 2025-10-19T01:47:19.298Z (8 months ago)
- Topics: basic, cpp, lib, library, mysql, setup, setup-cpp, setup-mysql, sql
- Language: C++
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Basic C++ MySQL Setup
Basic Setup for MySQL For C++
# Where to get MySQL Connecter 8 for C++?
- [Click Here!](https://dev.mysql.com/doc/connector-cpp/8.0/en/)
# Connection String
```cpp
"tcp://127.0.0.1:3306", "root", "Password"
```
# Basic Includes
```cpp
#include "jdbc/mysql_connection.h"
#include
#include
#include
#include
```
# Namespace
```cpp
using namespace ::sql;
```
# Basic Objects
```cpp
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;
```
# Initilize Driver & Connect
```cpp
driver = get_driver_instance();
```
```cpp
con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
```
In a method :
```cpp
void connectDatabase() {
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
}
void disconnectDb() {
con->close();
delete con;
}
```
# Query w/ Result Set
```cpp
ResultSet* query(std::string statement) {
ResultSet *newRes;
stmt = con->createStatement();
newRes = stmt->executeQuery(statement);
return newRes;
}
```
# Full Basic File
```cpp
#include "jdbc/mysql_connection.h"
#include
#include
#include
#include
using namespace ::sql;
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;
void connectDatabase() {
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
}
void disconnectDb() {
con->close();
delete con;
}
void setSchema(std::string db) {
con->setSchema(db);
}
ResultSet* query(std::string statement) {
ResultSet *newRes;
stmt = con->createStatement();
newRes = stmt->executeQuery(statement);
return newRes;
}
int main() {
connectDatabase();
setSchema("database");
ResultSet *r = query("SELECT * FROM database");
while(r->next()) {
cout << res->getString(1) << endl;
}
disconnectDb();
return 0;
}
```