Mysql 插入中文及中文查询 (修改+调试)
时间:2024-01-21 23:52:42
//我修改之,能正常运行,测试环境为mysql5.0,xp
//关键是设置对字符集,设置gbk,gb2312测试通过,utf8测试未通过
//在运行程序前先建立数据库jj,注意下面几个参数(修改为你自己的)
// 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;
}
///////////////子函数
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<<"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;
}
标签:Mysql,插入中文
0
投稿
猜你喜欢
python 引用传递和值传递详解(实参,形参)
2023-10-25 15:57:44
浅谈Webpack多页应用HMR卡住问题
2023-07-20 01:27:12
Python实现身份证前六位地区码对照表文件
2022-12-25 10:55:16
使用Abot中文分词组件来开发ASP站内搜索引擎
2007-10-18 13:36:00
Python随机生成彩票号码的方法
2023-10-13 15:25:39
SQL查询入门(上篇) 推荐收藏
2011-09-30 11:47:11
分页存储过程(二)在sqlserver中返回更加准确的分页结果
2024-01-13 03:42:43
python批量提取图片信息并保存的实现
2021-10-08 05:21:14
一文搞懂python可迭代对象,迭代器,生成器,协程
2023-08-22 18:45:13
Docker实践之python应用容器化
2023-06-07 03:29:43
matplotlib bar()实现多组数据并列柱状图通用简便创建方法
2022-12-11 11:59:15
php实现的单一入口应用程序实例分析
2023-11-22 08:00:26
CSS选择符小讲
2009-09-17 11:53:00
Python正则表达式re.search()用法详解
2021-08-28 03:24:46
HTML5 Canvas 起步(2) - 路径
2009-05-12 12:06:00
pandas函数isnull的具体使用
2022-08-04 18:43:02
pycharm全局修改方式
2021-10-11 12:51:56
Python基于随机采样一至性实现拟合椭圆(优化版)
2021-10-19 15:08:36
ASP.NET Core Authentication认证实现方法
2023-07-21 12:21:31
python 性能优化方法小结
2022-08-04 21:13:43