C mySQL程式設計 linux

2021-06-27 16:45:32 字數 3096 閱讀 3705

輸入相關設定,就能顯示查詢結果。

今天在linux下用c++封裝了一下mysql資料庫的連線,在執行

mysql_query(connection,  sql_str.c_str())

時,查詢得到的漢字是亂碼。 最後在網上查閱資料發現需要設定讀去資料庫的編碼格式

mysql_query(connection, "set names utf8")

即在執行資料查詢之前,要設定一下編碼方式!

set character_set_client = utf8 ;

set character_set_connection = utf8 ;

set character_set_database = utf8 ;

set character_set_results = utf8 ;

set character_set_server = utf8 ;

set collation_connection = utf8 ;

set collation_database = utf8 ;

set collation_server = utf8 ;

set character_set_client = utf8;

set character_set_results = utf8;

set character_set_connection = utf8;

show character set;

drop database if exists db_school;

create database db_school;

use db_school;

create table tb_student(

stu_id int auto_increment primary key,

stu_name varchar(20),

stu_*** varchar(5));

insert into tb_student(stu_id,stu_name,stu_***) values ('2001','申宇','男'),('2002','王麗蒙','女');

insert into tb_student( stu_name,stu_***) values ( '申2宇','男'),( '王3蒙','女');

select * from tb_student;

makefile檔案:

make run:

gcc -std=c99 -i/usr/include/mysql -i/usr/include/ncurses mysql1.c -l/usr/lib/mysql -lmysqlclient -lncurses -o mysql1.exe

./mysql1.exe

#include#include#include#include#includevoid error(char * msg)

int main(int argc, char * argv)

; char password[256]=;

char hostname[256]=;

char dbname[256] =;

char sqlquery[256]=;

move(lines/2-3,cols/2-10);

printw("username: "); getnstr(username,256);

move(lines/2-2,cols/2-10);

printw("password: ");

noecho();

int count=0;char c=0;

while( (c=getch())!='\n')

echo();

move(lines/2-1,cols/2-10);

printw("hostname: "); getnstr(hostname,256);

move(lines/2,cols/2-10);

printw("database: "); getnstr(dbname,256);

move(lines/2+1,cols/2-10);

printw("sqlquery: "); getnstr(sqlquery,256);

// connect to db

clear();

move(lines/2-1,cols/2-10);

printw("connecting...\n");

refresh();

connection = mysql_real_connect( connection, hostname ,username,password,dbname,0,null,0);

if(!connection)

else

printw("db connected\n");

move(0,0);

refresh();

endwin();

//to do

int res=mysql_query(connection, "set names utf8");

if(res) error("query [set names utf8] failed!");

mysql_res *res_ptr;

mysql_row sqlrow;

printf("excuting query %s ...\n",sqlquery);

res= mysql_query(connection,sqlquery);

if(res) error("query failed!");

res_ptr=mysql_use_result(connection);

if(res_ptr)

{ while((sqlrow=mysql_fetch_row(res_ptr)))

{ int field_count=0;

while(field_count

linux下c mysql程式設計函式總結2

說是c 下的mysql程式設計,其實用的還是mysql自帶的c api,因為在我學習的乙份遊戲的c 源 中,就是直接使用的c api,還有乙份是基於c 封裝的mysq api包,這個我還不了解,以後有時間研究一下。在對mysql的操作中,主要需要熟悉幾個api,這裡我總結了一下我遇到的常用的api以...

c mysql程式設計 C 資料庫程式設計

下面給出兩段典型的資料庫連線的例子。在此之前,請在程式頭部using處新增using system.data.sqlclient或system.data.oledb,以保證資料庫訪問時用到的命名空間能引用 sql server資料訪問 string strconn integrated securi...

Linux下c mysql程式的編譯命令

最好用 gcc o test test.c mysql config cflags libs 其它的編譯命令 gcc o test test.c i usr include mysql l usr lib mysql lmysqlclient lz lm gcc o test test.c i us...