【powerlog2.c】: //Version: 2.0 #include <stdio.h> #include <mysql.h> #include <string.h>
int main(int argc, char* argv[]) { MYSQL conn; mysql_init(&conn); if (!mysql_real_connect(&conn,"localhost","用户名","密码","数据库名",0,NULL,0)) { printf("Can not connect the database server.\n"); return 1; } mysql_query(&conn,"SET NAMES utf8"); char* sql; if (argc>=2 && strcmp(argv[1],"on")==0) sql="INSERT INTO `powerlog` (`time`,`flag`) VALUES (now(),'POWER ON')"; else sql="INSERT INTO `powerlog` (`time`) VALUES (now())"; mysql_query(&conn,sql); //Display the time printf("Welcome\n"); sql="SELECT now()"; mysql_query(&conn,sql); MYSQL_RES* rs=mysql_store_result(&conn); MYSQL_ROW row=mysql_fetch_row(rs); char buffer[100]; sprintf(buffer,"%s",row[0]); printf("%s\n",buffer); mysql_free_result(rs); mysql_close(&conn); return 0; }
|