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 ......
MysqlµÄ°²×°Îļþ¿ÉÒÔÖ±½Óµ½ http://www.mysql.com ÏÂÔØ£¬»ñµÃfor win32µÄ.zip°ü£¬½¨ÒéÑ¡Ôñ4.0.20d°æ±¾¡£
¡¡¡¡ÏÂÔØ»ñµÃMysqlµÄfor win32°²×°°üºó£¬ÓÃwinzip½âѹËõ£¬Ö±½ÓÔËÐÐsetup.exe£¬ÐèҪעÒâµÄÊÇÑ¡ÔñÒ»¸ö°²×°Â·¾¶£¬µ±È»£¬°²×°Â·¾¶¿ÉÒÔÈÎÒ⣬½¨ÒéÑ¡ÔñC:\MySQLĿ¼¡£°²×°Íê³ÉºóMySQLÒ²¾ÍÍê³ÉÁË¡£Ä¬ÈϵÄÓû§ÃûÊÇroot£¬ÃÜÂë ......
1¡¢µÇ½MySQL:
mysql -u root -p
2¡¢²é¿´Óû§ÐÅÏ¢
select user,host,password from mysql.user;
select user,host from mysql.user;
3¡¢ÉèÖÃÃÜÂë
set password for root@localhost=password('
ÔÚÕâÀïÌîÈërootÃÜÂë
');
4¡¢ÐÞ¸ÄÃÜÂë
·½·¨1£ºmysqladmin -u root -p password newpassword
·½·¨2£º ££mysql -u root ......
½â¾öÂÒÂë×îºÃµÄ·½·¨ÊÇÔÚÏîÄ¿Éè¼ÆÖ®³õ£¬Í³Ò»ËùÓеÄ×Ö·û¼¯£¬ÀýÈçÒ³Ãæ¡¢request¶ÔÏóÒÔ¼°Êý¾Ý¿âµÈµÈ¡£
Ò»¡¢ ¼¸ÖÖ³£¼ûµÄÂÒÂëÏÖÏó£º
1¡¢Ò³ÃæÂÒÂë
µ¥´¿µÄÒ³ÃæÂÒÂëÊǺܺýâ¾öµÄ£¬Ö»ÒªÐÞ¸ÄÍ·²¿ÊʺϵÄ×Ö·û¼¯¼´¿É£¬Èç¹ûÒ³ÃæÖÐÎÄÏÔʾÂÒÂ룬Äã¿ÉÒÔ°Ñ×Ö·û¼¯ÐÞ¸ÄΪ£ºgb2312»ògbk¡£
2¡¢Ò³ÃæÖ®¼ä´«µÝµÄ²ÎÊýÊÇÂÒÂë ......
À´Ô´£ºccidnet.com ×÷ÕߣºSabine¡¡ ʱ¼ä£º2006-10-09
ÔÚÊý¾Ý¿â±í¶ªÊ§»òË𻵵ÄÇé¿öÏ£¬±¸·ÝÄãµÄÊý¾Ý¿âÊǺÜÖØÒªµÄ¡£Èç¹û·¢Éúϵͳ±ÀÀ££¬Äã¿Ï¶¨ÏëÄܹ»½«ÄãµÄ±í¾¡¿ÉÄܶªÊ§×îÉÙµÄÊý¾Ý»Ö¸´µ½±ÀÀ£·¢ÉúʱµÄ״̬¡£ÓÐʱ£¬ÕýÊÇMySQL¹ÜÀíÔ±Ôì³ÉÆÆ»µ¡£¹ÜÀíÔ±ÒѾ֪µÀ±íÒÑÆÆ»µ£¬ÓÃÖîÈçvi»òEmacsµÈ±à¼Æ÷ÊÔͼֱ½Ó±à¼ËüÃÇ£¬Õâ¶Ô±í¾ø¶Ô²»ÊǼ ......