I'm coding a setup for my "game" server and I use MySQL for database. I getting an error when I create the database + table and the columns. I tested the Mysql code in phpmyadmin and it worked.
Here's a sample of my code: `
int area;
char user[100];
char pass[100];
char ip[200];
char tentativaspm;
void mysqlsetup(){
MYSQL *CON = mysql_init(NULL);
if (CON == NULL)
{
fprintf(stderr, "%s\n", mysql_error(CON));
exit(1);
}
if (mysql_real_connect(CON, ip, user, pass,
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
sleep(2);
userepassmysql();
}
printf("Conecao establecida.\n");
if(mysql_query(CON, "CREATE DATABASE place;")){
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
exit(1);
}
if(mysql_query(CON, "USE place; CREATE TABLE grid (pixelID int,color int);"){
fprintf(stderr, "%s\n", mysql_error(CON));
mysql_close(CON);
exit(1);
}
And I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USE grid CREATE TABLE grid (pixelID int(11) NOT NULL,color int(11) NOT N' at line 1
By default the mysql_query c function doesn't support multiple SQL statements.
But you can combine the two queries
USE [database];
CREATE TABLE grid (pixelID int,color int)
into the oneliner
CREATE TABLE [database].grid (pixelID INT, color INT)
Note replace [database] with the correct database name.
Solution one
Using the oneliner
if(mysql_query(CON, "CREATE TABLE place.grid (pixelID int,color int);"){
Solution two
Use the function mysql_set_server_option() function to enable multiple SQL statements.
mysql_set_server_option(connection, MYSQL_OPTION_MULTI_STATEMENTS_ON);
Related
I created a MySQL database, with some stored procedures. Using MySQL Workbench the SP forks fine, and now I need to launch them using a c program.
I created the program, which connect successfully to my db, and I'm able to launch procedures which not requires parameters.
To launch more complex procedure I need to use the prepare statement in c: in particular, I want to call the procedure esame_cancella(IN code CHAR(5)) which deletes a selected row of the table 'esame'.
int status;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *rs_metadata;
MYSQL_STMT *stmt;
MYSQL_BIND ps_params[6];
unsigned long length[6];
char cod[64];
printf("Codice: ");
scanf ("%s",cod);
length[0] = strlen(cod);
stmt = mysql_stmt_init(conn);
if (stmt == NULL) {
printf("Could not initialize statement\n");
exit(1);
}
status = mysql_stmt_prepare(stmt, "call esame_cancella(?) ", 64);
test_stmt_error(stmt, status); //line which gives me the syntax error
memset(ps_params, 0, sizeof(ps_params));
ps_params[0].buffer_type = MYSQL_TYPE_VAR_STRING;
ps_params[0].buffer = cod;
ps_params[0].buffer_length = 64;
ps_params[0].length = &length[0];
ps_params[0].is_null = 0;
// bind parameters
status = mysql_stmt_bind_param(stmt, ps_params); //muore qui
test_stmt_error(stmt, status);
// Run the stored procedure
status = mysql_stmt_execute(stmt);
test_stmt_error(stmt, status);
}
I use test_stmt_error to see MySQL log calling procedures.
static void test_stmt_error(MYSQL_STMT * stmt, int status)
{
if (status) {
fprintf(stderr, "Error: %s (errno: %d)\n",
mysql_stmt_error(stmt), mysql_stmt_errno(stmt));
exit(1);
}
}
when I compile, and launch my program, I have the following log:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 (errno: 1064)
Any help?
It looks like the string length being passed to mysql_stmt_prepare is wrong - try changing the 64 to 24.
Or better yet, try something like:
const char sql_sp[] = "call esame_cancella(?) ";
...
status = mysql_stmt_prepare(stmt, sql_sp, sizeof(sql_sp));
I work on a C tool, in which I need to manipulate and query process-internal data intensively.
Hence, I've decided to use the MySQL C API on Embedded mode, so I can have an embedded DB for each running process of my tool, and use all the SQL features on it.
All the MySQL C API tutorials I've found over the web, deal with connecting to a running server, which is not my case.
There are some examples for working with Embedded mode in Oracle's MySQL website, but they are not simple, and it's hard to make them work.
Q: Can someone please point me or write me a short example for initiating a DB using MySQL C API on Embedded mode?
Thanks!
After a lot of tries, finally answering to myself and sharing it with you:
Create a central data directory that would be used for storing DBs. In this example, I'll use /tmp/mysql_embedded_data
> mkdir /tmp/mysql_embedded_data
Create a C file, as in the following example:
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv) {
static char *server_options[] = {
"mysql_test", // An unused string
"--datadir=/tmp/mysql_embedded_data", // Your data dir
NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "libmysqld_server",
"libmysqld_client", NULL };
// Init MySQL lib and connection
mysql_library_init(num_elements, server_options, server_groups);
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(con, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
// Connect to no host/port -> Embedded mode
if (mysql_real_connect(con, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Create a sample empty DB, named "aNewDatabase"
if (mysql_query(con, "CREATE DATABASE aNewDatabase")) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Close connection
mysql_close(con);
exit(0);
}
Compile your file (I assume in this example that my C filename is /tmp/mysql_try.c)
gcc /tmp/mysql_try.c -o /tmp/mysql_try -lz `mysql_config --include --libmysqld-libs`
Run your compiled program:
> /tmp/mysql_try
When your program returnes, ensure that the DB creation succeeded. We named our sample DB aNewDatabase, so we'll check if it now has a directory inside the data directory we created -> We'll check if the directory /tmp/mysql_embedded_data/aNewDatabase was created:
> ls /tmp/mysql_embedded_data/aNewDatabase
db.opt
Enjoy!
Does anyone know why the following would cause a segmentation fault when run?
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
printf("MySQL client version : %s\n", mysql_get_client_info());
MYSQL *conn=NULL;
mysql_init(conn);
char *server = "localhost";
char *user = "root";
char *password = "pass";
char *database = "weather";
char *table ="room_temp";
char *tst_qry="INSERT INTO `weather`.`room_temp` (`idx`, `date`, `temperature`) VALUES (NULL, CURRENT_TIMESTAMP, '10')";
mysql_real_connect(conn, server, user, password, database, 0, NULL, 0);
}
I complied as follows
gcc -o mysql $(mysql_config --cflags) mysql.c $(mysql_config --libs)
The output was as follows,
MySQL client version : 5.5.31
Segmentation fault
Please help!
The allocated new object isn't stored in your code. Hence you are passing the NULL to mysql_real_connect().
Change this line:
mysql_init(conn);
to:
conn = mysql_init(conn);
or rather directly:
conn = mysql_init(NULL);
You're passing a NULL-pointer to mysql_real_connect. According to the documentation mysql_init returns an initialized MYSQL object (when passed a NULL-pointer). Change your code either to this to use the return value:
conn = mysql_init(conn);
or this, to have mysql_init fill the object:
MYSQL conn; /* note that this isn't a pointer */
mysql_init(&conn);
...
mysql_real_connect(&conn, ...);
hello all i have write a c program which connects to a mysql server and executes a sql query from a text file which has only one query.
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "127.0.0.1";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "har";
conn = mysql_init(NULL);
char ch, file_name[25];
char *ch1;
FILE *fp;
printf("Enter the name of file you wish to see ");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(0);
}
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
ch1=ch;
/* Connect to database */
if (!mysql_real_connect(conn, server,
NULL , NULL, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
printf("%c",ch);
/* send SQL query */
if (mysql_query(conn, ch1)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
fclose(fp);
}
i am unable to understand where i have gone wrong....
thanks in advance...
This is the line causing problem:
ch1=ch;
ch1 is a pointer to a character, whereas ch is a character.
Do you intend to store the bytes read from fp in a char array pointed by ch1? What you are doing is, every time in the while loop you are reading a character using fgetc storing it in ch and printing it.
Then, when while loop gets over, you are assigning a char to a char pointer. I am not sure what you are trying to do with this. But this definitely causes the problem.
You're going wrong in a lot of ways:
You don't declare the return type or arguments for main.
You're using gets. Never ever use gets, don't even think about. Use fgets instead.
fgetc returns an int, not a char so your ch should be an int. You won't be able to recognize EOF until you fix this.
You're declaring char ch and char *ch1 but assigning ch to ch1. That's where the error in your title is coming from.
Your code appears to be trying feed your SQL to MySQL one byte at a time and that's not going to do anything useful. I think you're meaning to use fgets to read the SQL file one line a time so that you can feed each line to MySQL as a single SQL statement.
You should spend some time reading about your compiler's warning switches
I tried to create a very simple application using the MySQL embedded server.
I basically took the simple example from the MySQL documentation and modified it a bit.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "mysql.h"
MYSQL *mysql;
static char *server_options[] = { "mysql_test", "--datadir=/Users/admin/libmysqldtest", "--language=/Users/admin/libmysqldtest/german", NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "libmysqld_server", "libmysqld_client", NULL };
int main(void)
{
mysql_library_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
//Do some queries here...
mysql_close(mysql);
mysql_library_end();
return 0;
}
On start-up
mysql_embedded: Unknown error 1146
is logged and InnoDB initializes.
Afterwards the app crashes at mysql_init.
Linking against libmysqld-debug I get the following error message:
Assertion failed: (argc && *argc >= 1), function handle_options, file
/Volumes/hd2/pb2/build/sb_0-3198286-1302522144.5/mysql-5.5.12/mysys/my_getopt.c, line 167
I use the static libmysqld(-debug) library distributed with the community server TAR-Archive for Mac OS X from the MySQL website (64 bit).