溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql編程入門以實例為引導(dǎo)

發(fā)布時間:2020-04-25 14:09:30 來源:億速云 閱讀:240 作者:三月 欄目:MySQL數(shù)據(jù)庫

本文主要給大家介紹mysql編程入門以實例為引導(dǎo),希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續(xù)在億速云行業(yè)資訊里面關(guān)注我的更新文章的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
////unsigned int mysql_errno(MYSQL *mysql)

/*
int  mysql_init(MYSQL  *handle)
{
MYSQL
tmphandle = (MYSQL )malloc(sizeof(MYSQL));
// .....
//...
handle = tmphandle;
}

int main()
{

mysql編程入門以實例為引導(dǎo)

int     ret = 0, i = 0;

MYSQL   mysql;
MYSQL   *connect = NULL;    
connect = mysql_init(&mysql) ;
if (connect == NULL)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_init() err \n");
    return ret;
}
printf("func mysql_init() ok \n");
connect = mysql_real_connect(connect, "localhost", "root", "mysql", "pos", 0, NULL, 0);
if (connect == NULL)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_init() err \n");
    return ret; 
}
printf("func mysql_real_connect() ok \n");
//查詢
const char *query = "select *from emp";
ret = mysql_query(&mysql, query);
if (ret != 0)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_query() err \n");
    return ret; 
}

//typedef char **MYSQL_ROW;               /* return data as array of strings */
//typedef unsigned int MYSQL_FIELD_OFFSET; /* offset to current field */

//獲取結(jié)果集合 
//結(jié)果集合中 可以含有10行數(shù)據(jù)
MYSQL_RES *result = mysql_store_result(&mysql);
if(result==NULL)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_query() err \n");
    return ret; 

}
unsigned int  num = mysql_field_count(&mysql) ;//從mysql句柄中獲取列

MYSQL_FIELD  *fields = mysql_fetch_fields(result);//獲取表頭地址結(jié)構(gòu)體
for (i = 0; i<num; i++)
{
    printf("%s\t",  fields[i].name);//表的名字
}
printf("\n");

MYSQL_ROW row = NULL;
while (row = mysql_fetch_row(result) ) 
{
    for (i=0; i<num; i++)
    {
        printf("%s\t", row[i]);
    }
    printf("\n");

mysql_free_result(result);
mysql_close(&mysql);
printf("hello...\n");
return ret;

}
這個程序一個很大的問題就是一個入口多個出口,怎么改呢?
即錯誤處理都讓他只有一個出口即可,提示goto語句,后面再詳細說
有了上面的知識,實踐一下
實現(xiàn)一個小小的mysql工具
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
int main(int argc,char* argv)
{
int     ret = 0, i = 0;

MYSQL   mysql;
MYSQL   *connect = NULL;
char    sqlbuf[2048];

connect = mysql_init(&mysql) ;      
if (connect == NULL)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_init() err \n");
    return ret;
}
printf("func mysql_init() ok \n");

connect = mysql_real_connect(connect, "localhost", "root", "123456", "mydb2", 0, NULL, 0);
if (connect == NULL)
{
    ret =  mysql_errno(&mysql) ;
    printf("func mysql_init() err \n");
    return ret; 
}
printf("func mysql_real_connect() ok \n");

for (;;)
{
    memset(sqlbuf, 0, sizeof(sqlbuf));
    printf("\nplease enter you sql: ");
    fgets(sqlbuf,sizeof(sqlbuf),stdin);
    buf[strlen(sqlbuf)-1]=0;
     /* EXIT(exit):退出 */  
    if ( 0 == strncmp(sqlbuf , "exit" , 4) || 0 == strncmp(sqlbuf , "EXIT" , 4) ||
         0 == strncmp(sqlbuf , "quit" , 4) || 0 == strncmp(sqlbuf , "QUIT" , 4) ) 
    {
        break;
    }

    ret = mysql_query(&mysql, "set names utf8");//避免中文亂碼
    if (ret != 0)
    {
        ret =  mysql_errno(&mysql) ;
        printf("func mysql_query() err \n");
        return ret; 
    }

    ret = mysql_query(&mysql, sqlbuf);
    if (ret != 0)
    {
        ret =  mysql_errno(&mysql) ;
        printf("func mysql_query() err \n");
        return ret; 
    }

    if (strncmp("select", sqlbuf, 6)==0 ||  strncmp("SELECT", sqlbuf, 6)==0)
    {
        //獲取結(jié)果集合 
        //結(jié)果集合中 可以含有10行數(shù)據(jù)
        MYSQL_RES *result = mysql_store_result(&mysql);

        //使用的過程中在獲取 結(jié)果
        //MYSQL_RES *result = mysql_use_result(&mysql);
        //

        unsigned int  num = mysql_field_count(&mysql) ;
        //獲取表頭
        MYSQL_FIELD  *fields = mysql_fetch_fields(result);
        for (i = 0; i<num; i++)
        {
            printf("%s\t",  fields[i].name);
        }
        printf("\n");
        //獲取內(nèi)容
        MYSQL_ROW row = NULL;
        while (row = mysql_fetch_row(result) ) 
        {
            for (i=0; i<num; i++)
            {
                printf("%s\t", row[i]);
            }
            printf("\n");
        }
        mysql_free_result(result);      
    }
}

mysql_close(&mysql);

printf("hello...\n");
return ret;

}

這樣一個小工具就完成了,用沒有問題呢?如果你輸入backspace鍵和delete和上下鍵呢?問題出現(xiàn)了,如何解決呢?
請看下面代碼
#include <termios.h>

struct termios oldterm;
void setstty2()//設(shè)置輸入退格鍵,不回顯
{
//system("stty erase ^H");//執(zhí)行shell命令,也可以 用來設(shè)置讀取用戶鍵盤輸入的時候,退格鍵不回顯

struct termios term;
if(tcgetattr(STDIN_FILENO, &term) == -1)//得到系統(tǒng)termion的設(shè)置
{
    printf("tcgetattr error is %s\n", strerror(errno));
    return;
}

oldterm = term;//保留當前termios設(shè)置,以便程序退出的時候可以恢復(fù)termios //tty

/*
term.c_lflag &= ~ICANON;//取消ICANON選項(不規(guī)范輸入)
term.c_lflag |= ICANON;//設(shè)置ICANON選項(規(guī)范輸入)
term.c_cc字段為要設(shè)置的具體特殊輸入字符,如c_cc[VERASE]代表退格鍵,
term.c_cc[VERASE] = '\b';意思為把退格鍵修改為'\b'
VERASE代表向前擦出一個字符,VINTR代表發(fā)送ctrl + C中斷信號,ctrl + c的ASCII碼為3
例如:term.c_cc[VINTR] = '\t';意思為將tab鍵設(shè)置為終端信號
tcsetattr中,第二個參數(shù)說明,TCSAFLUSH:發(fā)送了所有輸出后更改才生效,在更改發(fā)生時,未讀取的所有輸入數(shù)據(jù)都被刪除
TCSANOW:更改立即生效
TCSADRAIN:發(fā)送了所有輸出后更改才發(fā)生,如果更改輸出參數(shù)則應(yīng)該使用該選項
*/
term.c_cc[VERASE] = '\b';//'\b'為退格鍵的ASCII碼
if (tcsetattr(STDIN_FILENO, TCSANOW, &term) == -1)//設(shè)置系統(tǒng)termion
{
    printf("tcsetattr error is %s\n", strerror(errno));
}
return;

}

void setstty()//設(shè)置輸入退格鍵,不回顯
{
system("stty erase ^H");//執(zhí)行shell命令,也可以 用來設(shè)置讀取用戶鍵盤輸入的時候,退格鍵不回顯

}

void returnstty()//恢復(fù)系統(tǒng)的termios設(shè)置
{
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldterm) == -1)//設(shè)置系統(tǒng)termion
{
printf("tcsetattr error is %s\n", strerror(errno));
}
return;
}

這是另外一套函數(shù),可以用到時再去學(xué)習(xí)它
Linux中SQLplus上下鍵不能顯示歷史命令的問題
在Linux的sqlplus中運行SQL語句之后,想用上下鍵把歷史命令找出來,發(fā)現(xiàn)不支持。
按照下面的步驟可以設(shè)置
1,從http://download.csdn.net/user/kkdelta下載 rlwrap2,安裝rlwrap
1.[root@localhost]# tar -zxvf rlwrap-0.30.tar.gz  
br/>3.[root@localhost rlwrap-0.30]# ./configure  <br/4.[root@localhost rlwrap-0.30]# make  5.[root@localhost rlwrap-0.30]# make install  <br/" rel="nofollow">br/>5.[root@localhost rlwrap-0.30]# make install  <br/6.[root@localhost rlwrap-0.30]# rlwrap  
3,使用rlwrap,rlwrap sqlplus / as sysdba
這時候,熟悉的上下鍵功能有回來了。
4,設(shè)置別名,讓sqlplus默認在rlwrap方式下運行。
[root@localhost rlwrap-0.30]# vi /home/oracle/.bash_profile
添加下面的內(nèi)容
alias sqlplus='rlwrap sqlplus'
alias定義中使用的是單引號,而不是TAB上面的那個,/home/oracle/是oracle用戶的主目錄。
上面的設(shè)置需要Oracle用戶重新登錄,
如果上面的設(shè)置不起作用的話,可以設(shè)置在/home/oracle/.bashrc中,這樣每次改變到bash的時候都會設(shè)置alias。
用type sqlplus驗證,如果顯示下面的信息證明alias設(shè)置成功。
sqlplus is aliased to ‘rlwrap sqlplus’
 rlwrap
rlwrap is a wrapper that uses the GNU readline library to allow the editing of keyboard input for any other command. Input history is kept between invocations, separately for each command; history completion and search work as in bash and completion word lists can be specified on the command line.

看了以上關(guān)于mysql編程入門以實例為引導(dǎo),希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時售前售后,隨時幫您解答問題的。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI