Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kezhengjie/mysql_util
https://github.com/kezhengjie/mysql_util
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kezhengjie/mysql_util
- Owner: kezhengjie
- License: mit
- Created: 2021-08-27T05:51:27.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-27T06:25:44.000Z (over 3 years ago)
- Last Synced: 2024-12-25T03:22:22.517Z (10 days ago)
- Language: C
- Size: 1.22 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mysql_util
**a mysql util based on c mysql lib**## usage
### 1.connect to mysql
```
const char *user = "user";
const char *pass = "password";
const char *host = "host";
const char *db_name = "db_name";
const int port = port;mysql_util mysql_util(user, pass, host, port, db_name);
// call is_connected() to know whether the mysql is connected or not
if (!mysql_util.is_connected()){
return 1;
}
```### 2.Query SQL
```
mysql_result *result = m->query("select id,name from user");
// return nullptr if error occurs.
if(result == nullptr){
return 1;
}else
{
while (result->next())
{
// directly get row
const char *id = result->get(0);
// copy row to char*
char name[256];
result->get(1, name);
}
}
```### 3.Execute SQL
```
int num = m->exec("insert into user values (255,'jack')");
// return -1 if there's an error on query.
if (num == -1)
{
char err_msg[256];
m->last_error(err_msg);
println(err_msg);
return 1;
}else //return affected nums if success.
{
std::cout << "num affected " << num << std::endl;
}
```## look src/cmd/main.cpp for full demo;