Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Sep 262010
 

Connect MySQL server using C

Answer:

Below show a code sample for how to connect to MySQL server using C programming language.

#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"

MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

void exiterr(int exitcode)
{
	fprintf( stderr, "%s\n", mysql_error(&mysql) );
	exit( exitcode );
}

int main()
{
	uint i = 0;

	if (!(mysql_connect(&mysql,"host","username","password"))) 
		exiterr(1);

	if (mysql_select_db(&mysql,"payroll"))
		exiterr(2);

	if (mysql_query(&mysql,"SELECT name,rate FROM emp_master"))
		exiterr(3);

	if (!(res = mysql_store_result(&mysql)))
		exiterr(4);

	while((row = mysql_fetch_row(res))) {
		for (i=0 ; i < mysql_num_fields(res); i++) 
			printf("%s\n",row[i]);
	}

	mysql_free_result(res);
	mysql_close(&mysql);
}
Sep 132010
 

How to set the filetype in Vim

Answer:

When you created a new file by using the command vi, vim does not know the file type unless you save the file with a specific extension, e.g. test.c, or sometimes when the extension cannot be recognized by vim, you need to set the file type manually in command mode - pressing Esc + :

:set filetype=c

That is it.