Linux C implements simple instances of mysql database operations

xiaoxiao2021-03-06  14

//Test1.c:

#include #include

INT Main (int Argc, char * argv []) {INT i; char * names [3]; names [0] = "haha"; names [1] = "xixi"; names [2] = "wuwu";

Char Execsql [1024];

Mysql mysql_conn;

IF (MySQL_INIT (& Mysql_conn)! = null) {if (Mysql_Real_Connect (& mysql_connect ("Localhost", "Root", "Xiaodan", "Test", MySQL_Port, NULL, 0)! = null) {Printf ("Good connection! / N "); sprintf (ExecSql," CREATE DATABASE One_DB "); mysql_real_query (& mysql_conn, execsql, strlen (execSql));

// if (! Mysql_create_db (& mysql_conn, "one_db")) {i = mysql_affected_rows (& mysql_conn); if (i <= 0) {printf ( "Can not create database one_db / n");} else {mysql_select_db (& mysql_conn, "one_db"); sprintf (execsql, "create table girls (name char (10), age int (8))"); mysql_real_query (& mysql_conn, execsql, strlen (execsql)); mysql_select_db (& mysql_conn, "one_db"); For (i = 0; i <3; i ) {sprintf (execSql, "INSERT INTO GIRLS VALUES, NAMES [I], 19 I); Printf ("% S / N ", execSql); mysql_query (& mysql_conn, execsql);}}} else}}}}}}} Else {Printf (" Connection Failed! / N ");}}}}}}}}} else {printf (" Initialization Failed./N "); return -1;}

MySQL_Close (& mysql_conn); return 0;}

Compiling Command: gcc -g -o test1 -i / usr / include / mysql test1.c -l / usr / lib / mysql -lmysqlclient -lz

//Test2.c

#include

/ / # include

#include

INT main (int Argc, char ** argv) {

MySQL mysql_conn; / * connection handle * /

Mysql_res * mysql_result; / * result handle * / mysql_row mysql_row; / * row data * /

INT F1, F2, Num_Row, Num_col;

IF (mysql_init (& mysql_conn)! = null) {

IF (MySQL_Real_Connect (& mysql_conn, "localhost", "root", "xiaodan", "one_db", mysql_port, null, 0)! = null) {

IF (MySQL_QUERY (& mysql_conn, "select * from girls") == 0) {

/ * We have executed SELECT statement, in the query table, so record * /

MySQL_Result = mysql_store_result (& mysql_conn); // Get The Result from The Executing Select Query

Num_row = mysql_num_rows (mysql_result); / * Get the no. of row * /

Num_col = mysql_num_fields (mysql_result); / * get the no color @ /

FOR (f1 = 0; f1

// for (f2 = 0; f2

MySQL_ROW = mysql_fetch_row (mysql_result); / * fetch one by one * /

Printf ("[ROW% D, COL 0] ==> [% s] / n", f1, mysql_row [0]); Printf ("[Row% D, COL 1] ==> [% s] / n ", f1, mysql_row [1]);

//}

}

} else {

(void) Printf ("Query Fails / N");

}

} else {

(void) Printf ("Connection Fails / N");

}

} else {

(void) Printf ("INITIALIZATION FAILS / N");

}

mysql_free_result (mysql_result);

MySQL_Close (& mysql_conn);

Return 0;

} Compile command: gcc -g -o test2 -i / usr / include / mysql test2.c -l / usr / lib / mysql -lmysqlclient -lz

Note: The database installed using the RPM package is not used to develop head files and libraries, you need to download the RPM development package of mysql-wevel

转载请注明原文地址:https://www.9cbs.com/read-50773.html

New Post(0)