首页 > 代码库 > 【Mysql】select

【Mysql】select

描述

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

大意:select用来从数据库中挑选数据

有以下两种形式

SELECT column_name,column_name
FROM table_name;

SELECT * FROM table_name;

下面是测试代码

#include <WinSock2.h>
#include <stdio.h>
#include <mysql.h>
#pragma comment(lib, "libmysql.lib")

int main(int argc, char *argv[])
{
    MYSQL mysql;
    MYSQL_RES *res;
    MYSQL_ROW row;

    mysql_init(&mysql);

    if (mysql_real_connect(&mysql, "localhost", "root", "Susake", "Susake", 0, NULL, 0))
        printf("数据库连接成功\n");

    mysql_query(&mysql, "insert into Ran(a, b) values(1, 2);");

    mysql_real_query(&mysql, "select *from Ran;", strlen("select *from Ran;"));

    res = mysql_store_result(&mysql);

    while (row = mysql_fetch_row(res))
        printf("%s %s\n", row[0], row[1]);
    return 0;
}