MySQL資料庫操作執行個體

來源:互聯網
上載者:User

環境設定:

安裝完MySQL之後,將安裝目錄中的include目錄下的libmysql.lib檔案拷到VS2008安裝目錄中的VC\lib\下,然後在 項目-選項-c/c++-常規 中的附加元件封裝含目錄以及 連結器-常規 中的附加庫目錄中加入“c:\MySQL\include\”,並且在 連結器-輸入 中的附加依賴項內添加“libmysql.lib”,這樣即可使編譯器找到mysql.h標頭檔,並可在程式中使用c語言的mysql API來操作資料庫。(如果MySQL安裝目錄中無include目錄,可到MySQL官網下載並安裝MySQL connector for C,並修改include目錄路徑)

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
using namespace std;

int main()
{
    const char user[] = "root";         //username
    const char pswd[] = "root";         //password
    const char host[] = "localhost";    //or"127.0.0.1"
    const char table[] = "test";        //database
    unsigned int port = 3306;           //server port       
    MYSQL myCont;
    MYSQL_RES *result;
    MYSQL_ROW sql_row;
    MYSQL_FIELD *fd;
    char column[32][32];
    int res;
    mysql_init(&myCont);
    if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
    {
        cout<<"connect succeed!"<<endl;
        mysql_query(&myCont, "SET NAMES GBK"); //設定編碼格式,否則在cmd下無法顯示中文
        res=mysql_query(&myCont,"select * from samples");//查詢
        if(!res)
        {
            result=mysql_store_result(&myCont);//儲存查詢到的資料到result
            if(result)
            {
                int i,j;
                cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
                for(i=0;fd=mysql_fetch_field(result);i++)//擷取列名
                {
                    strcpy(column[i],fd->name);
                }
                j=mysql_num_fields(result);
                for(i=0;i<j;i++)
                {
                    printf("%s\t",column[i]);
                }
                printf("\n");
                while(sql_row=mysql_fetch_row(result))//擷取具體的資料
                {
                    for(i=0;i<j;i++)
                    {
                        printf("%s\n",sql_row[i]);
                    }
                    printf("\n");
                }
            }
        }
        else
        {
            cout<<"query sql failed!"<<endl;
        }
    }
    else
    {
        cout<<"connect failed!"<<endl;
    }
    if(result!=NULL) mysql_free_result(result);//釋放結果資源
    mysql_close(&myCont);//中斷連線
    return 0;
}

代碼2:

測試環境:MySQL 5.1.35
安裝MySQL之後,開啟MySQL Command Line Client,輸入root密碼,即可操作資料庫

//查看MySQL版本
mysql> select version();

//顯示所有資料庫
mysql> show databases;

//使用資料庫
mysql> use database_name;

//顯示所有資料表
mysql> show tables;

//顯示資料表結構
mysql> describe table_name;

//建立資料庫
mysql> create database database_name;

//刪除資料庫
mysql> drop database database_name;

//建立資料表
mysql> use database_name;
mysql> create table table_name (欄位名 VARCHAR(20), 欄位名 CHAR(1));

//刪除資料表
mysql> drop table table_name;

//查詢記錄
mysql> select * from table_name;

//匯入.sql檔案
mysql> use database_name;
mysql> source c:/mysql.sql

//修改root密碼
mysql> UPDATE mysql.user SET password=PASSWORD('新密碼') WHERE User='root';

//退出
mysql> quit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.