这是精心收集的精品经典资料,值得下载保存阅读!
c语言操作mysql数据库小例子_互帮互助(C language MySQL database operation example _ mutual help)
C language MySQL database operation example _ help Baidu home page | Baidu space | login help home page blog album | personal files | friends
View Article
C language operation MySQL database small example, 2008-07-15 09:30 Author: yds_amber
Database statement
Create database student
Create, table, stu
Id int (4) auto_increment,
SnO varchar (20),
Sname varchar (30)
Primary key (ID)
)
Insert, into, Stu, values (null,,'yds_amber')
C language source file example.c
#include "stdio.h""
#include "unistd.h""
#include "stdlib.h""
#include "string.h""
#include "/usr/include/mysql/mysql.h" / * on the can view the previous article.
#define SQL2 "select, sname, from, Stu, where, id =%d"
Int main (int, argc, char, **argv) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
Int, len, i;
Char *server= "localhost"";
Char *user= "root"";
Char *password= "860925"";
Char *database= "student"";
Char sqlbuf[160];
Char *sql= "select * from stu"";
Char *sql3= "insert, into, Stu, values (null,,'rhl')."";
Char *sql4= "update, Stu, set, sname='yds', where, id=1."";
If (argc = = 2) {
Fprintf (stderr, need, one, parameter, for, select:id, \n\n);
Exit (1);
}
Conn=mysql_init (NULL);
If (... Mysql_real_connect (Conn, server, user, password, database, 0, NULL, 0)) {
Fprintf (stderr,%s\n, mysql_error (conn));
Exit (1);
}
If (mysql_real_query (Conn, SQL, strlen (SQL)) {
Fprintf (stderr,%s\n, mysql_error (conn));
Exit (1);
}
If ((res=mysql_store_result (conn)) {
Fprintf (stderr, "Couldn't, get, result, from,%s\n"",
Mysql_error (conn);
Exit (1);
}
How many columns for return / * * /.
Len=mysql_num_fields (RES);
Printf (number, of, fields,%d\n, returned:, len);
Printf ("number of rows returned:%d\n", "mysql_num_rows" (RES));
While (row=mysql_fetch_row (RES))
{
For (i=0; i
Printf ("%s", row[i]);
Printf ("\n");
}
The release of the result set memory / * * /
Mysql_free_result (RES);
Parameterized query