在mysql數(shù)據(jù)庫插入中文及中文查詢實例教程
來源:易賢網(wǎng) 閱讀:1216 次 日期:2015-12-10 11:53:38
溫馨提示:易賢網(wǎng)小編為您整理了“在mysql數(shù)據(jù)庫插入中文及中文查詢實例教程”,方便廣大網(wǎng)友查閱!

//我修改之,能正常運行,測試環(huán)境為mysql5.0,xp

//關(guān)鍵是設(shè)置對字符集,設(shè)置gbk,gb2312測試通過,utf8測試未通過

//在運行程序前先建立數(shù)據(jù)庫jj,注意下面幾個參數(shù)(修改為你自己的)

// char *host = "localhost";

// char *user = "root";

// char *pass = "674800";

// char *db = "jj";

#include <windows.h>

#include <iostream>

#include <mysql.h>

#include <stdio.h>

#include <string.h>

using namespace std;

#pragma comment(lib,"libmysql.lib")

int connDB();

int create_table();

int insert_table();

int select_table();

int disconnDB();

MYSQL *mysql;

MYSQL_RES *res;

MYSQL_FIELD * fd;

MYSQL_ROW row;

char szSqlText[500]="";

int i;

int main()

{

connDB();

// create_table();

insert_table();

select_table();

disconnDB();

return 0;

}

///////////////子函數(shù)

int connDB()//連接數(shù)據(jù)庫

{

char *host = "localhost";

char *user = "root";

char *pass = "674800";

char *db = "jj";

mysql = mysql_init((MYSQL*) 0);

mysql_real_connect( mysql, host, user, pass, db,3306, NULL, 0 ) ; //鏈接到服務器

mysql_set_character_set(mysql,"gb2312"); //設(shè)置字符集

cout<<"conn OK!"<<endl;

return 0;

}

//關(guān)閉數(shù)據(jù)庫

int disconnDB()

{

mysql_close( mysql ) ;

cout<<"disconn OK!"<<endl;

return 0 ;

}

//創(chuàng)建表

int create_table()

{

mysql_set_character_set(mysql,"gb2312");

sprintf(szSqlText, \

"create table mytable \

(s0 varchar(100), \

s1 char(6),\

s2 varchar(4),\

s3 varchar(3),s4 int)\

ENGINE=InnoDB \

DEFAULT CHARSET=gb2312");

if(mysql_query( mysql, szSqlText))

cout<<"occurred an error:"<<mysql_error(mysql)<<endl;

return 0;

}

int insert_table()

{

mysql_set_character_set(mysql,"gb2312");

sprintf(szSqlText,

"insert into mytable\

values('2000-3-10 21:01:30',\

'Test',\

'清風寒劍',\

'歲月情',\

2500)");

if( mysql_query( mysql, szSqlText) )

cout<<"occurred an error:"<<mysql_error(mysql)<<endl;

return 0;

}

int select_table()

{

mysql_set_character_set(mysql,"gb2312");

sprintf(szSqlText, "select * from mytable ");

if (mysql_query( mysql, szSqlText))

{

cout<<"occurred an error:"<<mysql_error(mysql)<<endl;

mysql_close( mysql ) ;

return FALSE ;

}

res = mysql_store_result( mysql) ;

i = (int) mysql_num_rows( res ) ;

cout<<"Query: "<<szSqlText<<"\n"<<i<<" records found:"<<endl;

for ( i = 0 ; fd = mysql_fetch_field( res ) ; i++ )

cout<<fd->name<<"\t";

cout<<endl;

while(row = mysql_fetch_row( res ))

cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<endl;

mysql_free_result( res ) ;

cout<<"select OK!"<<endl;

return 0;

}

更多信息請查看IT技術(shù)專欄

更多信息請查看數(shù)據(jù)庫
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇剩?/div>

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)