mysql connect by c++
#include <stdio.h>
#include <windows.h>
#include <mysql.h>
#define host "localhost"
#define username "root"
#define password "123"
#define database "oa"
MYSQL *conn;
int main()
{
MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i,ret;
FILE *fp;
MYSQL_FIELD *field;
unsigned int num_fields;
conn = mysql_init(NULL);
ret = mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gbk");
mysql_real_connect(conn,host,username,password,database,0,NULL,0);
fp = fopen ( "t_sys_power.out","w");
fprintf(fp,"char set %s\n", mysql_character_set_name(conn));
mysql_query(conn,"SELECT * from t_sys_power ");//WHERE a1='abc'");
res_set = mysql_store_result(conn);
while((field = mysql_fetch_field(res_set)))
{
printf("field name %s\n", field->name);
}
num_fields = mysql_num_fields(res_set);
for(i = 0; i < num_fields; i++)
{
field = mysql_fetch_field_direct(res_set, i);
printf("Field %u is %s\n", i, field->name);
}
while ((row = mysql_fetch_row(res_set)) != NULL)
{
for (i=0; i<mysql_num_fields(res_set); i++)
{
fprintf(fp,"%s\n",row[i] != NULL ? row[i] : "NULL");
}
}
mysql_close(conn);
fclose(fp);
return 0;
}
//Ñ¡Ôñ->Ŀ¼£ºinclude:C:\PROGRAM FILES\MYSQL\MYSQL SERVER 5.0\INCLUDE£¨compiler option,directories,c++include)
//Ñ¡Ôñ->Ŀ¼£ºlib :C:\PROGRAM FILES\MYSQL\MYSQL SERVER 5.0\LIB\OPT(compiler option,directories,libraies)
//¹¤³Ì£ºÉèÖãºlink:libmysql.lib(updateÏÂÔØlibmysql,°²×°Ö®ºó£¬project,projcect option,parameters,linkerÖÐÊäÈë-lmysql
)
//libmysql.dll¿½±´µ½¿ÉÖ´ÐÐÎļþÏàͬĿ¼£¬»òÕßϵͳĿ¼ÏÂ
Ïà¹ØÎĵµ£º
1.µ¼³öÕû¸öÊý¾Ý¿â
¡¡¡¡mysqldump -u Óû§Ãû -p Êý¾Ý¿âÃû > µ¼³öµÄÎļþÃû
¡¡¡¡mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
2.µ¼³öÒ»¸ö±í
¡¡¡¡mysqldump -u Óû§Ãû -p Êý¾Ý¿âÃû ±íÃû> µ¼³öµÄÎļþÃû
¡¡¡¡mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
3.µ¼³öÒ»¸öÊý¾Ý¿â½á¹¹
¡¡¡¡mysql ......
1¡¢È·¶¨Êý¾Ý¿âĬÈϱàÂ룬±ÈÈç±àÂëΪutf8,½«¶ÁÈë;¾¶±àÂëͬÑùÉèΪutf8£¬ÃüÁîΪ£ºset names utf8;
2¡¢source d:/20080613.sql µ¼ÈëÊý¾Ý¡£ÑéÖ¤Êý¾Ý¿âÖеÄÊý¾ÝÊÇ·ñ´æÔÚÂÒÂë¡£
3¡¢Èç¹ûÈÔÈ»´æÔÚÂÒÂëÎÊÌ⣬Õâʱºò¾ÍÒª¿¼ÂǸı䵼ÈëÎļþµÄ±à ......
mysqlÀï×î³£ÓõÄÈÕÖ¾ÓÐ4ÖÖ,·Ö±ðÊÇ:´íÎóÈÕÖ¾,¶þ½øÖÆÈÕÖ¾,²éѯÈÕÖ¾ÒÔ¼°Âú²éѯÈÕÖ¾. 1.´íÎóÈÕÖ¾»á¼Ç¼µ±mysqlÆô¶¯ºÍ¹Ø±ÕÒÔ¼°ÔËÐÐÖеĴíÎóÐÅÏ¢,¿ÉÒÔʹÓà --log-error[=file_name]À´Ö¸¶¨´íÎóÈÕÖ¾´æ·ÅÎļþ.µ±mysql serverÒì³£µÄʱºòÓ¦¸ÃÊ×ÏȲ鿴¸ÃÈÕÖ¾.ÆäʵÀïÃæҲûʲô,ÎÒÓ¢ÓïËļ¶Ã»¹ý,ÅäÉÏ´Ê°ÔĿǰΪֹ»¹Ã»ÓÐÅöµ½½â¾ö²»Á˵ÄÎÊ ......
1¡¢ÔÚlinuxÏ£º
²é¿´mysql ÊÇ·ñÔËÐУºps -ef | grep myslq
Èç¹û MySQL ÕýÔÚÔËÐУ¬Ê×ÏÈɱ֮: kill mysqlµÄ½ø³ÌºÅ¡£
¡¡¡¡Æô¶¯ MySQL :/usr/bin/safe_mysqld --skip-grant-tables &
¡¡¡¡¾Í¿ÉÒÔ²»ÐèÒªÃÜÂë¾Í½øÈë MySQL ÁË¡£
¡¡¡¡È»ºó¾ÍÊÇ
¡¡¡¡>use mysql
¡¡¡¡ ......