求简单的数据库操作代码

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
top5
In need of some credit
In need of some credit
Posts: 1
Joined: Wed Feb 16, 2011 5:46 am

求简单的数据库操作代码

Post by top5 » Thu Feb 17, 2011 1:48 am

wxDev-C++开发带主从表、字段绑定,不用odbc直接读取、修改、删除firebird,mysql等等数据库操作的代码。

whz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Tue Oct 05, 2010 1:01 am

Re: 求简单的数据库操作代码

Post by whz » Mon Aug 15, 2011 2:45 am

Linux下C++访问MySQL
1:安装MySQL

挂载光盘:
mkdir /cdrom
mount /dev/hdc /cdrom
cd /cdrom/Server
rpm -ivh perl-DBI-1.52-2.el5.i386.rpm
rpm -ivh mysql-5.0.45-7.el5.i386.rpm mysql-bench-5.0.45-7.el5.i386.rpm mysql-devel-5.0.45-7.el5.i386.rpm
rpm -ivh perl-DBD-MySQL-3.0007-2.el5.i386.rpm
rpm -ivh mysql-server-5.0.45-7.el5.i386.rpm mysql-test-5.0.45-7.el5.i386.rpm
查看mysql服务状态:
service mysqld status
启动服务:
service mysqld start
连接数据库:
mysql

2:安装gcc工具包

rpm -ivh gcc* --force --nodeps 强制安装

3:进入mysql

create table t1 (id int,name varchar(30));

t1表插入数据:
1.cpp
#include <iostream>
#include <mysql/mysql.h>
#include <string>
using namespace std;
main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "insert into t1 (id, name) values (1, 'java1');";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql

更新mysql数据;
1.cpp
#include <iostream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "insert into t1 (id,name) values (2, 'java2'),(3, 'java3');";
mysql_query(&mysql, sql.c_str());
sql = "update t1 set name = 'java33' where id = 3;";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./1.out


mysql 存储过程:
mysql>delimiter //
>create procedure p01()
>begin
>insert into t1 (id,name) values (66, 'java66');
>end;
>//
应用存储过程:

vi 2.cpp
#include <stream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "call p01();";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}

g++ -o 2.out 2.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./2.out

delimiter; 返回mysql ;结束

触发器:
新建t2表:
mysql>delimiter //
>create trigger tr1 after insert on t1 for each row
>begin
>insert into t2 (id,name) values (new.id,new.name);
>end;
>//
>delete from t1 where id = 66;
>//
>delimeter ;

vi 3.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string sql = "call p01();";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}

g++ -o 3.out 3.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./3.out

mysql>delimeter ;
>select * from t1;
>select * from t2;
>存在数据(66, 'java66')


查看表中数据的总数:

vi 4.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout << rowcount << endl;
mysql_close(&mysql);
}

g++ -o 4.out 4.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./4.out

查处字段的总数:

vi 5.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout << rowcount << endl;
int fieldcount = mysql_num_fields(result);
cout << fieldcount << endl;
mysql_close(&mysql);
}

g++ -o 5.out 5.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./5.out

列出具体字段名:

vi 6.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
MYSQL_FIELD *field = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout << rowcount << endl;
int fieldcount = mysql_num_fields(result);
cout << fieldcount << endl;
for(int i = 0; i < fieldcount; i++)
{
field = mysql_fetch_field_direct(result,i);
cout << field->name << "\t\t";
}
cout << endl;
mysql_close(&mysql);
}

g++ -o 6.out 6.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./6.out


显示表中所有数据:

vi 7.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
MYSQL_FIELD *field = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout << rowcount << endl;
int fieldcount = mysql_num_fields(result);
cout << fieldcount << endl;
for(int i = 0; i < fieldcount; i++)
{
field = mysql_fetch_field_direct(result,i);
cout << field->name << "\t\t";
}
cout << endl;
MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
while(NULL != row)
{
for(int i=1; i<fieldcount; i++)
{
cout << row << "\t\t";
}
cout << endl;
row = mysql_fetch_row(result);
}
mysql_close(&mysql);
}

g++ -o 7.out 7.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./out

乱码的解决
void LoginDialog::OnButtonLogin(wxCommandEvent& event)
{
xxpostSQL checkUser ;
mysqlpp::Connection con = checkUser.ConnectDatabase();
if (!con.connected())
{
return;
}

const wxString wxUserName = UserName->GetValue();
char cUserName[1024];
strncpy(cUserName, (const char*)wxUserName.mb_str(wxConvUTF8), 512);

const wxString wxUserPwd = UserPwd->GetValue();
char cUserPwd[1024];
strncpy(cUserPwd, (const char*)wxUserPwd.mb_str(wxConvUTF8), 512);

wxString sql_str;
sql_str += "SELECT UserName, UserPwd, UserDept, UserType FROM t_user WHERE UserName = ";
sql_str += " \"";
sql_str += cUserName;
sql_str += "\"";
sql_str += " AND UserPwd = ";
sql_str += "\"";
sql_str += cUserPwd;
sql_str += "\"";

mysqlpp::Query query = con.query( sql_str );
// query <<"SELECT UserName, UserPwd, UserDept, UserType"
// << " FROM t_user"
// << " WHERE UserName = " << cUserName
// << " AND UserPwd = " << cUserPwd ;
mysqlpp::StoreQueryResult res = query.store();
if(res.num_rows() > 0)
{
AcceptAndClose();
xxFrame* Frame = new xxFrame(0);
Frame->Show();
}
else
{
wxMessageBox(_T("登录失败,请检查帐号和密码是否正确并重新登录!"), _T("出错消息"));
return;
}
}

whz
Earned a small fee
Earned a small fee
Posts: 11
Joined: Tue Oct 05, 2010 1:01 am

Re: 求简单的数据库操作代码

Post by whz » Mon Aug 15, 2011 3:05 am

网速太慢,不小心发了两次,版主把这个删除吧。

Post Reply