Home > Database > Mysql Tutorial > Mysql 插入中文及中文查询 (修改+调试)

Mysql 插入中文及中文查询 (修改+调试)

WBOY
Release: 2016-06-07 18:01:37
Original
845 people have browsed it

此程序从pudn上下载,但源程序编译都通不过,同时又有致命错误,

//我修改之,能正常运行,测试环境为mysql5.0,xp
//关键是设置对字符集,设置gbk,gb2312测试通过,utf8测试未通过
//在运行程序前先建立数据库jj,注意下面几个参数(修改为你自己的)
代码如下:
// char *host = "localhost";
// char *user = "root";
// char *pass = "674800";
// char *db = "jj";
#include
#include
#include
#include
#include
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;
}
///////////////子函数
int connDB()//连接数据库
{
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"); //设置字符集
coutreturn 0;
}
//关闭数据库
int disconnDB()
{
mysql_close( mysql ) ;
coutreturn 0 ;
}
//创建表
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))
coutreturn 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) )
coutreturn 0;
}
int select_table()
{
mysql_set_character_set(mysql,"gb2312");
sprintf(szSqlText, "select * from mytable ");
if (mysql_query( mysql, szSqlText))
{
coutmysql_close( mysql ) ;
return FALSE ;
}
res = mysql_store_result( mysql) ;
i = (int) mysql_num_rows( res ) ;
coutfor ( i = 0 ; fd = mysql_fetch_field( res ) ; i++ )
coutnamecoutwhile(row = mysql_fetch_row( res ))
coutmysql_free_result( res ) ;
coutreturn 0;
}

我机器上的运行结果:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template