数据库编程
#include <mysql/mysql.h>
gcc -lmysqlclient
mysql_init(MYSQL *pmysql);
MYSQL *mysql_real_connect(MYSQL *pmysql,const char *hostname, ,const char *usename, const char *passwd,const char *dbname,0,0,0);
int main(int arg, char *args[]) { MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, "localhost", "dbuser1", "dbuser1", "db1",0,0,0); if (connect == NULL) { printf(mysql_error(&mysql)); return EXIT_FAILURE; } mysql_close(connection); return EXIT_SUCCESS; }
int mysql_query(MYSQL *pmysql,const char *sql);
int main(int arg, char *args[]) { MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, "localhost", "dbuser", "dbuser", "db1",0,0,0); if (connect == NULL) { printf(mysql_error(&mysql)); return EXIT_FAILURE; } int state = mysql_query(connection, "INSERT INTO table1 (NAME,SEX,AGE,CLASS) VALUES (‘HAHA‘,1,30,‘666‘)"); if (state != 0) { printf(mysql_error(connection)); return EXIT_FAILURE; } mysql_close(connection); return EXIT_SUCCESS; }
int main(int arg, char *args[]) { MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, "localhost", "dbuser1", "dbuser1", "db1",0,0,0); if (connect == NULL) { printf(mysql_error(&mysql)); return EXIT_FAILURE; } int state = mysql_query(connection, "UPDATE table1 SET CLASS = ‘250‘ WHERE NAME = ‘HAHA‘"); if (state != 0) { printf(mysql_error(connection)); return EXIT_FAILURE; } mysql_close(connection); return EXIT_SUCCESS; }
int main(int arg, char *args[]) { MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, "localhost", "dbuser1", "dbuser1", "db1",0,0,0); if (connect == NULL) { printf(mysql_error(&mysql)); return EXIT_FAILURE; } int state = mysql_query(connection, “DELETE FROM table1 WHERE NAME = ‘HAHA‘"); if (state != 0) { printf(mysql_error(connection)); return EXIT_FAILURE; } mysql_close(connection); return EXIT_SUCCESS; }
MYSQL_RES *mysql_store_result(MYSQL *pmysql);
mysql_free_result(MYSQL_RES *res)
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
while(row = mysql_fetch_row(result)) != NULL)
{
printf(“name:%s,sex:%s,age:%s,class:%s\n”,row[0], row[1], row[2],row[3]
}
MYSQL_FIELD *mysql_fetch_field (MYSQL_RES *result);
while ((sqlField = mysql_fetch_field(result)) != NULL)
{
printf("%s\n", sqlField->name);
}
int main(int arg, char *args[]) { MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, "localhost", "dbuser1", "dbuser1", "db1",0,0,0); if (connect == NULL) { printf(mysql_error(&mysql)); return EXIT_FAILURE; } int state = mysql_query(connection, "SELECT NAME,SEX,AGE,CLASS FROM table1"); if (state != 0) { printf(mysql_error(connection)); return EXIT_FAILURE; } MYSQL_RES *result = mysql_store_result(connection); if (result == (MYSQL_RES *)NULL) { printf(mysql_error(connection)); return EXIT_FAILURE; } MYSQL_ROW row; while((row = mysql_fetch_row(result)) != NULL) { printf("name %s, sex %s, age %s, class %s\n", row[0],row[1],row[2],row[3]); } mysql_free_result(result); mysql_close(connection); return EXIT_SUCCESS; }
int main(int arg, char *args[]) { if (arg < 5) return 0; MYSQL *connection, mysql; mysql_init(&mysql); connection = mysql_real_connect(&mysql, args[1], args[2], args[3], args[4], 0, 0, 0); if (connection == NULL) { printf("%s\n", mysql_error(&mysql)); return EXIT_FAILURE; } printf("success connect to mysql\n"); int state = mysql_query(connection, "SET NAMES utf8"); char buf[1024]; while (1) { memset(buf, 0, sizeof(buf)); read(STDIN_FILENO, buf, sizeof(buf)); if (strncmp(buf, "exit", 4) == 0) { break; } state = mysql_query(connection, buf); if (state != 0) { printf("%s\n", mysql_error(connection)); } MYSQL_RES *result = mysql_store_result(connection); if (result == NULL) { printf("%s\n", mysql_error(connection)); break; } MYSQL_FIELD *sqlField; int iFieldCount = 0; while ((sqlField = mysql_fetch_field(result)) != NULL) { printf("%s\t", sqlField->name); iFieldCount++; } printf("\n"); MYSQL_ROW row; while ((row = mysql_fetch_row(result)) != NULL) { int i; for (i = 0; i < iFieldCount; i++) { printf("%s\t", row[i]); } printf("\n"); } mysql_free_result(result); } mysql_close(connection); return EXIT_SUCCESS; }
mysql_close(MYSQL *connect);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。