CとMySQLCAPIから始めたところです。mysql_query()を使用してテーブルを作成しています。マニュアルによると、関数の使用法はint mysql_query(MYSQL *mysql, const char *stmt_str)
であり、関数は成功した場合は0を返し、エラーの場合はゼロ以外を返します。ただし、if (mysql_query())
またはを使用するかどうかに関係なくif ! (mysql_query())
、プログラムは常にstdout「エラー0:」に出力します。また、どちらの場合でも、プログラムはデータベースとテーブルを作成します。
#include <my_global.h>
#include <mysql.h>
int main (int argc, char **argv)
{
MYSQL *connection;
const char DATABASE[] = "test";
const char HOSTNAME[] = "localhost";
const char USERNAME[] = "root";
const char PASSWORD[] = "123456";
connection = mysql_init(NULL);
if (connection == NULL)
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
if (mysql_real_connect(connection, HOSTNAME, USERNAME, PASSWORD, NULL, 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
if (mysql_query(connection, "DROP DATABASE IF EXISTS test"))
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
if (mysql_query(connection, "CREATE DATABASE test"))
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
if (mysql_select_db(connection, DATABASE))
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
/* The problem that I do not understand is happening in the statement below.
* It seems that a successful query returns zero, so I should not see "Error 0:"
*/
if (mysql_query(connection, "CREATE TABLE jobs (id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, PRIMARY KEY (id), UNIQUE KEY name_index (name)) ENGINE=InnoDB DEFAULT CHARSET=utf8"));
{
printf("Error %u: %s\n", mysql_errno(connection), mysql_error(connection));
exit(1);
}
mysql_close(connection);
}