hholzgra / connector-c-examples Goto Github PK
View Code? Open in Web Editor NEWMySQL/MariaDB C API examples
License: GNU General Public License v2.0
MySQL/MariaDB C API examples
License: GNU General Public License v2.0
For learning resources such as examples maybe a very permissive license such as MIT would be a better fit?
right now all examples have the full initialization and connect code in their respective source files, this makes finding the actual example code for the function at had hard for the casual reader
I'm not sure if it is fair to raise an issue report here...
The mysql_real_escape_string
example does not show how to avoid a buffer overflow on to
. The best I can tell, the function is unsafe because it does not take a destination buffer size. Attacker controlled input to the function can simply enlist enough escapes to exceed the 201 character destination buffer.
For convenience here is the function:
const char *from = "\\";
char to[201];
unsigned long len;
len = mysql_real_escape_string(mysql, to, from, strlen(from));
to[len] = '\0';
printf("Escaped string is: \"%s\" (%lu)\n", to, len);
My apologies if I am parsing things incorrectly.
(I'm here because the MySQL example seems to have the same problem, and the MariaDB example does not exist).
Hi.
I wrote the following code based on your MySQL access demo.
I want to run this to create a database.
Please give me some advice to build this program.
Written in *.c in Visual Studio 2019.
Is it executable in Visual Studio 2019?
Which field of Visual Studio 2019 should you try to create?
What is MySQL library?
Isn't my.CNF file possible in Ubuntu system?
How do I build and run this on Ubuntu?
Please explain it step by step.
Thank you.
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <netdb.h>
#include <time.h>
#include <netinet/in.h>
#include <mysql.h>
#include <curl/curl.h>
#include <ctype.h>
MYSQL *mysql_con;
void
mysql_start()
{
mysql_con = mysql_init(NULL);
if (mysql_con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(1);
}
if (mysql_real_connect(mysql_con, "localhost", "happy", "123456", "happy", 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(EXIT_FAILURE);
}
if (mysql_query(mysql_con, "CREATE TABLE IF NOT EXISTS `frontier` (`url` varchar(2084) NOT NULL, `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), UNIQUE KEY `url` (`url`)) ENGINE=InnoDB AUTO_INCREMENT=1"))
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
mysql_stop();
exit(1);
}
if (mysql_query(mysql_con, "CREATE TABLE IF NOT EXISTS `crawled` (`id` int NOT NULL AUTO_INCREMENT, `url` varchar(2084) DEFAULT NULL, `title` varchar(768) DEFAULT NULL, `date` varchar(128) DEFAULT NULL, `last_modified` varchar(128) DEFAULT NULL, `links` int DEFAULT NULL, `backlinks` int DEFAULT NULL, `http_code` int DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `url` (`url`), KEY `http_code` (`http_code`), KEY `title` (`title`)) ENGINE=InnoDB AUTO_INCREMENT=1"))
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
mysql_stop();
exit(1);
}
if (mysql_query(mysql_con, "CREATE TABLE IF NOT EXISTS `emails` (`email` varchar(2084) NOT NULL, `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1"))
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
mysql_stop();
exit(1);
}
}
void
mysql_stop()
{
mysql_close(mysql_con);
}
int
main(int argc, char **argv)
{
int cleanup = 0, opt, ret;
mysql_start();
mysql_stop();
printf("Exiting.\n");
return (0);
}
Right now the mysql_ssl_set example contains MariaDB specific code, in the long run it should work with both MariaDB and MySQL
When using libmariadb dev packages only mariadb_config gets installed by default, mysql_config gets created as a symlink to mariadb_config only if libmariadb-dev-compat is also installed
The autoconf macros should check for both names
This is especially needed on the MariaDB side when trying to use mariadb_*() functions like mariadb_get_infov()
See also Issue #5
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.