Linux下C語言進行MySQL資料庫操作筆記

來源:互聯網
上載者:User

vim 編輯模式下可以顯示行數“sudo vi /etc/vim/vimrc”在開啟的檔案中加入“set nu”儲存後退出。

利用:”vi .bash_history“查看之前輸入過的命令

編譯操作資料庫的程式test.c時 輸入”./test“提示錯誤:
Connection error 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
此時更改mysql下進行資料庫連接時的mysql_real_connect(&mysql, "localhost", "localhost", "", "myclink", 0, NULL, 0)其中的第一個localhost換為”127.0.0.1“,記得輸入密碼”111“

編譯操作資料庫的程式test.c時 輸入”./test”提示錯誤:./test: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory,此時用命令”export“查看環境變數,發現沒有mysql的lib的路徑,此時輸入”vi .bashrc“,在末尾行輸入”export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib“將該路徑動態添加到環境變數(如果直接在export中間添加,則屬於session對話,此時對環境變數的改變屬於臨時改變,www.bkjia.com在該對話結束後,環境變數會恢複為以前的狀態,而.bashrc在每次編譯時間會動態執行一次,故可以一次寫入,每次調用.bashrc將環境變數添加進去)

輸入 gcc -o test $(./mysql_config --cflags) test.c $(./mysql_config –libs)
編譯時間,提示錯誤資訊:
bash: ./mysql_config:
No such file or directorybash: ./mysql_config:
No such file or directorytest.c: In function ‘main’:test.c:36:50:
warning: incompatible implicit declaration of built-in function ‘strlen’/tmp/ccfnwW6I.o:
In function `main':test.c:(.text+0xa9): undefined reference to `mysql_init'test.c:(.text+0xed): undefined reference to `mysql_real_connect'test.c:(.text+0xfd):
undefined reference to `mysql_error'test.c:(.text+0x14f):
undefined reference to `mysql_real_query'test.c:(.text+0x166):
 undefined reference to `mysql_error'test.c:(.text+0x1a2):
undefined reference to `mysql_close'collect2:
 ld returned 1 exit status,
此時將mysql_config複製到程式當前路徑下並命名為mysql_config,輸入
”cp ./workspace/insert/mysql_config ./mysql_config“
此時”ls“看到目前的目錄下複製了一個mysql_config檔案,
輸入 gcc -o test $(./mysql_config --cflags) test.c $(./mysql_config –libs)
再輸入”gcc -o test $(./mysql_config --cflags) test.c $(./mysql_config –libs)”
此時在該目錄下產生可執行檔test

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.