Difference between revisions of "Test MySQL installation"
(→Test Source) |
(→Test from PHP) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
place | place | ||
− | The ''id'' field shall be | + | The ''id'' field shall be set to |
+ | type int | ||
+ | 10 in length | ||
+ | unsigned attribute | ||
+ | Not Null | ||
+ | auto_increment | ||
+ | Primary | ||
Insert a couple of fields into the table. | Insert a couple of fields into the table. | ||
− | ==Test | + | ===Create a test user=== |
+ | |||
+ | Create - in '''Privileges''' - a new user with '''localhost''' as the host. | ||
+ | |||
+ | Hit the '''Go''' button. | ||
+ | |||
+ | Use the edit button at the end of the line with the name to be edited in this view: | ||
+ | |||
+ | [[File:User.png|500 px]] | ||
+ | |||
+ | then click the '''Check ALL''' in the following | ||
+ | |||
+ | [[File:User1.png|500px]] | ||
+ | |||
+ | and finally the '''Go''' button to save the privileges. | ||
+ | |||
+ | ==Test From C== | ||
Using Eclipse there are a couple of settings, that needs to be ensured. | Using Eclipse there are a couple of settings, that needs to be ensured. | ||
− | In the project settings ensure that you have the correct paths for the compiler to search for include files: | + | In the project settings ensure that you have the correct paths for the compiler to search for include files (Notice these settings are for a C++ project - they are similar for C): |
[[File:Includes.png|600px]] | [[File:Includes.png|600px]] | ||
Line 29: | Line 51: | ||
<source lang="c"> | <source lang="c"> | ||
+ | /* | ||
+ | * ifTest.cpp | ||
+ | * | ||
+ | * Created on: 21 Jan 2014 | ||
+ | * Author: klausk | ||
+ | */ | ||
+ | |||
/* Simple C program that connects to MySQL Database server*/ | /* Simple C program that connects to MySQL Database server*/ | ||
#include <mysql.h> | #include <mysql.h> | ||
Line 36: | Line 65: | ||
int main() | int main() | ||
{ | { | ||
− | MYSQL *conn; | + | MYSQL *conn; /* Connection */ |
− | MYSQL_RES *res; | + | MYSQL_RES *res; /* Result set */ |
− | MYSQL_ROW row; | + | MYSQL_ROW row; /* A row */ |
char *server = "localhost"; | char *server = "localhost"; | ||
− | char *user = " | + | char *user = "klausk"; |
− | char *password = " | + | char *password = "pingeling"; |
char *database = "test"; | char *database = "test"; | ||
+ | |||
+ | /* Show the client version */ | ||
+ | printf("MySQL client version: %s\n", mysql_get_client_info()); | ||
+ | |||
conn = mysql_init(NULL); | conn = mysql_init(NULL); | ||
/* Connect to database */ | /* Connect to database */ | ||
Line 56: | Line 89: | ||
exit(1); | exit(1); | ||
} | } | ||
+ | /* Tell database that we're going to use the result */ | ||
res = mysql_use_result(conn); | res = mysql_use_result(conn); | ||
/* output table name */ | /* output table name */ | ||
printf("MySQL Tables in mysql database:\n"); | printf("MySQL Tables in mysql database:\n"); | ||
while ((row = mysql_fetch_row(res)) != NULL) | while ((row = mysql_fetch_row(res)) != NULL) | ||
+ | { | ||
printf("%s \n", row[0]); | printf("%s \n", row[0]); | ||
− | /* | + | } |
+ | /* Tell database that we wont be needing this result anymore */ | ||
mysql_free_result(res); | mysql_free_result(res); | ||
+ | |||
+ | /* Prepare a new query */ | ||
+ | if (mysql_query(conn, "SELECT * FROM tstTab")) | ||
+ | { | ||
+ | fprintf(stderr, "%s\n", mysql_error(conn)); | ||
+ | exit(1); | ||
+ | } else | ||
+ | { | ||
+ | res = mysql_use_result(conn); | ||
+ | printf("\n\ntstTab contains:\n"); | ||
+ | while ((row = mysql_fetch_row(res)) != NULL) | ||
+ | { | ||
+ | printf("id: %s, name %s, place: %s \n", row[0], row[1], row[2]); | ||
+ | } | ||
+ | /* Tell database that we wont be needing this result anymore */ | ||
+ | mysql_free_result(res); | ||
+ | } | ||
+ | printf("\n"); | ||
mysql_close(conn); | mysql_close(conn); | ||
+ | return 0; | ||
} | } | ||
</source> | </source> | ||
− | Be sure to change the | + | Be sure to change the USERNAME and PASSWORD to a valid user on the database server. |
+ | |||
+ | The MySQL C API is documented at [http://dev.mysql.com/doc/refman/5.0/en/c-api.html| C API] |
Latest revision as of 12:10, 12 March 2014
After installing the necessary development tools and the MySQL database (see Install essential development utilities) it is time for at quick test to see if everything works.
Test Database
In PHPMyAdmin create a new database called test
Create a new table in the test database. Create 3 fields. The fields shall be named:
id name place
The id field shall be set to
type int 10 in length unsigned attribute Not Null auto_increment Primary
Insert a couple of fields into the table.
Create a test user
Create - in Privileges - a new user with localhost as the host.
Hit the Go button.
Use the edit button at the end of the line with the name to be edited in this view:
then click the Check ALL in the following
and finally the Go button to save the privileges.
Test From C
Using Eclipse there are a couple of settings, that needs to be ensured.
In the project settings ensure that you have the correct paths for the compiler to search for include files (Notice these settings are for a C++ project - they are similar for C):
and the correct paths to the libraries:
Enter the source below in a .c-file or .cpp-file:
/* * ifTest.cpp * * Created on: 21 Jan 2014 * Author: klausk */ /* Simple C program that connects to MySQL Database server*/ #include <mysql.h> #include <stdio.h> #include <stdlib.h> int main() { MYSQL *conn; /* Connection */ MYSQL_RES *res; /* Result set */ MYSQL_ROW row; /* A row */ char *server = "localhost"; char *user = "klausk"; char *password = "pingeling"; char *database = "test"; /* Show the client version */ printf("MySQL client version: %s\n", mysql_get_client_info()); conn = mysql_init(NULL); /* Connect to database */ if ( ! mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } /* send SQL query */ if (mysql_query(conn, "show tables")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } /* Tell database that we're going to use the result */ 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]); } /* Tell database that we wont be needing this result anymore */ mysql_free_result(res); /* Prepare a new query */ if (mysql_query(conn, "SELECT * FROM tstTab")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } else { res = mysql_use_result(conn); printf("\n\ntstTab contains:\n"); while ((row = mysql_fetch_row(res)) != NULL) { printf("id: %s, name %s, place: %s \n", row[0], row[1], row[2]); } /* Tell database that we wont be needing this result anymore */ mysql_free_result(res); } printf("\n"); mysql_close(conn); return 0; }
Be sure to change the USERNAME and PASSWORD to a valid user on the database server.
The MySQL C API is documented at C API