Mysql 插入中文及中文查询 (修改+调试)
网络编程 2021-07-05 15:27www.168986.cn编程入门
此程序从pudn上下载,但源程序编译都通不过,又有致命错误,
//我修改之,能正常运行,测试环境为mysql5.0,xp
//关键是设置对字符集,设置gbk,gb2312测试通过,utf8测试未通过
//在运行程序前先建立数据库jj,注意狼蚁网站SEO优化几个参数(修改为你自己的)
// 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 ment(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"); //设置字符集
cout<<"conn OK!"<<endl;
return 0;
}
//关闭数据库
int disconnDB()
{
mysql_close( mysql ) ;
cout<<"disconn OK!"<<endl;
return 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))
cout<<"ourred 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<<"ourred 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<<"ourred 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;
}
我机器上的运行结果
//关键是设置对字符集,设置gbk,gb2312测试通过,utf8测试未通过
//在运行程序前先建立数据库jj,注意狼蚁网站SEO优化几个参数(修改为你自己的)
代码如下:
// 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 ment(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"); //设置字符集
cout<<"conn OK!"<<endl;
return 0;
}
//关闭数据库
int disconnDB()
{
mysql_close( mysql ) ;
cout<<"disconn OK!"<<endl;
return 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))
cout<<"ourred 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<<"ourred 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<<"ourred 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;
}
我机器上的运行结果
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程