全部博文(22)
分类: 数据库开发技术
2010-01-26 21:23:12
#include
#include "client/dbclient.h"
#pragma comment(lib, "mongoclient.lib")
#pragma comment(lib, "wsock32.lib")
using namespace std;
using namespace mongo;
int main( int argc, const char **argv ) {
int i, r;
clock_t start, finish;
string errmsg;
string table = "db.students";
int record = 100000;
DBClientConnection conn;
if(argc > 1) {
record = atoi(argv[1]);
}
// connect db server
if (!conn.connect(string("127.0.0.1:55555"), errmsg)) {
cout << "couldn't connect to server:" << errmsg << endl;
return -1;
}
// create index
conn.ensureIndex(table, fromjson("{name:1}"));
// insert test data
start = clock();
srand(start);
for (i = 0; i < record; i ++) {
r = rand(); // use random number
BSONObjBuilder query;
query << GENOID << "name" << r;
BSONObj addr = BSON("address" << i << "city" << i << "state" << i << "postalCode" << i);
query.append("address", addr);
BSONObj scores = BSON("for_course" << r << "grade" << r%10);
query.append("scores", scores);
conn.insert(table, query.obj());
}
finish = clock();
cout << "time:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "insert finished" << endl;
// waiting
cin >> errmsg;
return 0;
}
|
#include
#include
#include "database.h"
using namespace std;
using namespace database;
int main(int argc, char* argv[])
{
string host = "127.0.0.1,811";
string dbname = "db";
string user = "root";
string password = "sinfors";
string table = "Students";
CDatabase mysql;
int i, r;
int record = 100;
clock_t start, finish;
char query[512];
if(argc > 1) {
record = atoi(argv[1]);
}
// connect db server
if (!mysql.open(host.c_str(), dbname.c_str(), user.c_str(), password.c_str()))
{
printf("open database error(%s)", dbname.c_str());
return -1;
}
// insert test data
start = clock();
for (i = 0; i < record; i ++) {
r = rand(); // use random number
_snprintf_s(query, sizeof(query),
"INSERT INTO %s VALUES(NULL, '%u', %u)", table.c_str(), r,r);
mysql.exec(query);
}
finish = clock();
cout << "time:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "insert finished" << endl;
// waiting
cin >> query;
} |
create database db;
use db;
create table Students(
id int unsigned auto_increment not null,
name varchar(128) not null,
addressId int unsigned,
index index_name(name),
primary key (id)
)ENGINE=INNODB; |