MySQL C API custom values
NickName:VV589 Ask DateTime:2009-10-21T16:16:59

MySQL C API custom values

I have been working with the tutorial on MySQL C API from http://zetcode.com/tutorials/mysqlcapitutorial/ the following example is working fine:

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{

MYSQL *conn;

conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);

mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");

mysql_query(conn, "INSERT INTO writers VALUES('Leo Tolstoy')");
mysql_query(conn, "INSERT INTO writers VALUES('Jack London')");
mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");
mysql_query(conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')");
mysql_query(conn, "INSERT INTO writers VALUES('Emile Zola')");

mysql_close(conn);

}

How can i change the code to accept custom values instead of the hardcoded ones, is it possible to replace writers and ex. Leo Tolstoy with a char pointer or something?

Copyright Notice:Content Author:「VV589」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1599415/mysql-c-api-custom-values

Answers
Svante 2009-10-21T08:28:36

You will likely have to compose your strings, e.g. using sprintf().",


Lukáš Lalinský 2009-10-21T08:37:16

You have basically two options:\n\n\nYou can construct the query yourself, using sprintf. Then you should use mysql_real_escape_string on all your variables, otherwise your code is vulnerable to SQL injection.\nYou can use prepared statements. The documentation for mysql_stmt_execute has some examples. (this is the better option)\n",


More about “MySQL C API custom values” related questions

MySQL C API custom values

I have been working with the tutorial on MySQL C API from http://zetcode.com/tutorials/mysqlcapitutorial/ the following example is working fine: #include &lt;my_global.h&gt; #include &lt;mysql.h&g...

Show Detail

Get CATEGORY PARAMETER Values from Custom Fields in Attask Api C#

How to get CATEGORY PARAMETER Values from Custom Fields in Attask Api C# Suppose we have a project template. there is a custom field like xyz this custm field has drop down values. i want to get that

Show Detail

MySQL C API mysql_query

I have two problems with the MySQL C API. How do I get variable to be added to the table when I do the mysql_query() and how do I get the table show decimals? Here is my code: void insertDataToT...

Show Detail

mysql_store_result() @ MySQL C API - does it really allocate memory in each call?

I've read that mysql_store_result() in the MySQL C API will allocate memory for each and every call to it; mysql_store_result() reads the entire result of a query to the client, allocates a MYSQL_...

Show Detail

LoadError using MySQL with ruby (mysql_api.so not found)

I installed MySQL 5.5 ( mysql-installer-5.5.15.0.msi ) and mysql gem ( with "gem install mysql" command ) . Now I write a simple program and when I run it ruby give me this message : C:/Ruby192/li...

Show Detail

taking input from user in mysql c api

if we have to insert records into a mysql database using c api then the general code is this if (mysql_query(conn, "insert into empinfo values ('saikat banerjee')")) { printf("4Err...

Show Detail

MySQL C API using results

I am using the MySQL C API to query the database and I have the results stored in MYSQL_ROW types. I am able to print the results to the console with printf("%s", row[0]); however, according to ...

Show Detail

C - MySQL API prepared statements

I've followed this MySQL Manual to the letter, and changed it slightly to try and make a program that inserts 4 values into table. The structure of the table is: MariaDB [(none)]&gt; desc analytics...

Show Detail

ATTask API - Updating Custom Field with API and C Sharp

I have the API functioning fine within my project for non custom fields, but the documentation doesn't really seem to work in relation to explaining what the actual put request format should be for

Show Detail

mysql c api: inserting numbers without converting them to a string

I have to put tons of integers and floats into a mysql database from a c application. Currently I am doing this by converting all those values into a string and then transfering them to the databa...

Show Detail