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¿½±´µ½¿ÉÖ´ÐÐÎļþÏàͬĿ¼£¬»òÕßϵͳĿ¼ÏÂ
Ïà¹ØÎĵµ£º
windows2003----¿ªÊ¼----ÔËÐÐ---mysql -uroot -p£¬½øÈëMYSQLÊý¾Ý¿â£¬ÊäÈëÃÜÂ룬µÇ½¡£
show databases;¿´¿´ÓÐÄÄЩÊý¾Ý¿â
use xxxxxx£»(xxxxxxΪÊý¾Ý¿âÃû£¬½øÈëÊý¾Ý¿â)
update user set passwd=MD5('111111*') where user_name = 'admin';£¨111111ΪҪÐ޸ĵÄÃÜÂ룩 ......
mysqlÀï×î³£ÓõÄÈÕÖ¾ÓÐ4ÖÖ,·Ö±ðÊÇ:´íÎóÈÕÖ¾,¶þ½øÖÆÈÕÖ¾,²éѯÈÕÖ¾ÒÔ¼°Âú²éѯÈÕÖ¾. 1.´íÎóÈÕÖ¾»á¼Ç¼µ±mysqlÆô¶¯ºÍ¹Ø±ÕÒÔ¼°ÔËÐÐÖеĴíÎóÐÅÏ¢,¿ÉÒÔʹÓà --log-error[=file_name]À´Ö¸¶¨´íÎóÈÕÖ¾´æ·ÅÎļþ.µ±mysql serverÒì³£µÄʱºòÓ¦¸ÃÊ×ÏȲ鿴¸ÃÈÕÖ¾.ÆäʵÀïÃæҲûʲô,ÎÒÓ¢ÓïËļ¶Ã»¹ý,ÅäÉÏ´Ê°ÔĿǰΪֹ»¹Ã»ÓÐÅöµ½½â¾ö²»Á˵ÄÎÊ ......
MySQLÓкܶà¿ÉÒÔµ¼ÈëÊý¾ÝµÄ·½·¨£¬È»¶øÕâЩֻÊÇÊý¾Ý´«ÊäÖеÄÒ»°ë£¬ÁíÍâµÄÒ»°ãÊÇ´ÓMySQLÊý¾Ý¿âÖе¼³öÊý¾Ý¡£ÓÐÐí¶àµÄÔÒòÎÒÃÇÐèÒªµ¼³öÊý¾Ý¡£Ò»¸öÖØ
ÒªµÄÔÒòÊÇÓÃÓÚ±¸·ÝÊý¾Ý¿â¡£Êý¾ÝµÄÔì¼Û³£³£ÊÇ°º¹óµÄ£¬ÐèÒª½÷É÷´¦ÀíËüÃÇ¡£¾³£µØ±¸·Ý¿ÉÒÔ°ïÖú·ÀÖ¹±¦¹óÊý¾ÝµØ¶ªÊ§£»ÁíÍâÒ»¸öÔÒòÊÇ£¬Ò²ÐíÄúÏ£Íûµ¼³öÊý¾ÝÀ´¹²
Ïí¡£ ÔÚÕâ¸öÐÅÏ¢¼¼Ê ......
TIMESTAMPÁÐÀàÐÍÌṩһÖÖÀàÐÍ£¬Äã¿ÉÒÔʹÓÃËü×Ô¶¯µØÓõ±Ç°µÄÈÕÆÚºÍʱ¼ä±ê¼ÇINSERT»òUPDATEµÄ²Ù×÷¡£Èç¹ûÄãÓжà¸öTIMESTAMPÁУ¬Ö»ÓеÚÒ»¸ö×Ô¶¯¸üС£
×Ô¶¯¸üеÚÒ»¸öTIMESTAMPÁÐÔÚÏÂÁÐÈκÎÌõ¼þÏ·¢Éú£º
ÁÐûÓÐÃ÷È·µØÔÚÒ»¸öINSERT»òLOAD DATA INFILEÓï¾äÖÐÖ¸¶¨¡£
ÁÐûÓÐÃ÷È·µØÔÚÒ ......
MySQL ÃüÁîÐÐ
µÚÒ»ÕС¢mysql·þÎñµÄÆô¶¯ºÍÍ£Ö¹
net stop mysql
net start mysql
µÚ¶þÕС¢µÇ½mysql
Óï·¨ÈçÏ£º mysql -uÓû§Ãû -pÓû§ÃÜÂë
¼üÈëÃüÁîmysql -uroot -p£¬ »Ø³µºóÌáʾÄãÊäÈëÃÜÂ룬ÊäÈë12345£¬È»ºó»Ø³µ¼´¿É½øÈëµ½mysqlÖÐÁË£¬mysqlµÄÌáʾ·ûÊÇ£º
mysql>
×¢Ò⣬Èç¹ûÊÇÁ¬½Óµ½ÁíÍâµÄ»úÆ÷ÉÏ£¬ÔòÐèÒª¼ÓÈëÒ»¸ö² ......