FastDb 简单编码运用

  • Post author:
  • Post category:其他


FastDb在代码中实现,首先需要在代码中注册一个表,相当于数据库的建表。

代码如下:

class Student
{
	public:
	char const* vc_stuid;
	char const* vc_stuname;
	char const* vc_sex;
	dbArray< dbReference<Score> > ScoreRefs;                   /*一个学生对应多个成绩,所以关系是1:n*/
	TYPE_DESCRIPTOR((KEY(vc_stuid, INDEXED|HASHED),
					 KEY(vc_stuname, HASHED),
					 FIELD(vc_sex),
					 RELATION(ScoreRefs, StuRef)
					 ));
};

REGISTER(Student);




详细参见http://blog.csdn.net/fuyun10036/article/details/8620542中table那一节。







表建好之后就是打开数据库:



dbDatabase db;




db.open(“FastDbTest”) 其中FastDbTest是数据库名








插入数据:



Student stu;




stu.vc_stuid = stuid;

stu.vc_stuname = stuname;

stu.vc_sex = sex;

insert(stu);

db.commit();








查询数据:



dbCursor<Student> students; 其中Student是表名





if(students.select() >0)
{
	do
	{
		printf("%s\t%s\t%s\t\n",students->vc_stuid,students->vc_stuname,students->vc_sex);
	}while(students.next());
}

上面是查询所有记录。



删除数据:




dbQuery q2 = "vc_stuname = ",name1;
dbCursor<Student> updatestudent(dbCursorForUpdate);
if(updatestudent.select(q2) >0)
{
	updatestudent.removeAllSelected();
	db.commit();
}

更新记录:




if(updatestudent.select(q1) >0)
{
	do
        {
		updatestudent->vc_stuname = "wang";
	}while(students.next());
	updatestudent.update();//自动提交
}

附上全部代码:




// FastDb_Test_01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "fastdb.h"

USE_FASTDB_NAMESPACE

class Score;
class Student
{
	public:
	char const* vc_stuid;
	char const* vc_stuname;
	char const* vc_sex;
	dbArray< dbReference<Score> > ScoreRefs;                   /*一个学生对应多个成绩,所以关系是1:n*/
	TYPE_DESCRIPTOR((KEY(vc_stuid, INDEXED|HASHED),
					 KEY(vc_stuname, HASHED),
					 FIELD(vc_sex),
					 RELATION(ScoreRefs, StuRef)
					 ));
};


class Subject
{
	public:
	char const* vc_subid;
	char const* vc_subname;
	dbArray< dbReference<Score> > ScoreRefs;                  /*一门科目对应多个成绩,所以关系是1:n*/
	TYPE_DESCRIPTOR((KEY(vc_subid, INDEXED|HASHED),
					 KEY(vc_subname, HASHED),
					 RELATION(ScoreRefs, SubRef)
					));
};


class Score
{
	public:
	char const* vc_stuid;
	char const* vc_subid;
	int4		l_score;
	dbReference<Student> StuRef;
	dbReference<Subject> SubRef;
	TYPE_DESCRIPTOR((KEY(vc_stuid, INDEXED|HASHED),
					 KEY(vc_subid, INDEXED|HASHED),
					 FIELD(l_score),
					 RELATION(StuRef,ScoreRefs),
					 RELATION(SubRef,ScoreRefs)
					));
};

/* 建表 */
REGISTER(Student);
REGISTER(Subject);
REGISTER(Score);

int main()
{
	const int maxStrlen = 256;
	dbDatabase db;
	
	Student stu;
	Subject sub;
	Score sco;

	char name[101];
	char name1[101];
	char id[11];
	char id1[4];
	strcpy(name,"wangjm");
	strcpy(name1,"tanghui");
	
	dbQuery q1,q2,q3,q4,q5;
	q1 = "vc_stuname =",name;
	q2 = "vc_stuname = ",name1;
	q3 = "vc_stuid =",id;
	q4 = "vc_subid =",id;

	dbCursor<Student> students; 
	dbCursor<Score> scores;
	dbCursor<Subject> subjects;
	dbCursor<Student> updatestudent(dbCursorForUpdate);

	
	if (db.open("FastDbTest"))
	{
		/* 插入 */
		char stuid[11];
		char stuname[101];
		char sex[2];
		char subid[4];
		char subname[101];
		int score;
		memset((void *)stuid,'\0',sizeof(stuid));
		memset((void *)stuname,'\0',sizeof(stuname));
		memset((void *)sex,'\0',sizeof(sex));
		memset((void *)subid,'\0',sizeof(subid));
		memset((void *)subname,'\0',sizeof(subname));
		/* 1 */
		strcpy(stuid,"1");
		strcpy(stuname,"wangjm");
		strcpy(sex,"0");
		strcpy(id,stuid);
		if (students.select(q3) >0 )
		{
			printf("this student is existed!\n");
		}
		else
		{
			stu.vc_stuid = stuid;
			stu.vc_stuname = stuname;
			stu.vc_sex = sex;
			insert(stu);
			db.commit();
			printf("student infomation insert successfully!\n");
		}
		/* 2 */
        	strcpy(stuid,"2");
		strcpy(stuname,"tanghui");
		strcpy(sex,"1");
		strcpy(id,stuid);
		if (students.select(q3) >0 )
		{
			printf("this student is existed!\n");
		}
		else
		{
			stu.vc_stuid = stuid;
			stu.vc_stuname = stuname;
			stu.vc_sex = sex;
			insert(stu);
			db.commit();
			printf("student infomation insert successfully!\n");
		}
		
		/* 3 */
		strcpy(subid,"1");
		strcpy(subname,"c++");
		strcpy(id1,subid);
		if (subjects.select(q4) >0)
		{
			printf("this subject is existed!\n");
		}
		else
		{
			sub.vc_subid = subid;
			sub.vc_subname = subname;
			insert(sub);
			db.commit();
		}


		/* 4 */
		strcpy(stuid,"1");
		strcpy(subid,"1");
		strcpy(id,stuid);
		strcpy(id1,subid);
		score = 98;
		if((scores.select(q3) >0)&&(scores.select(q4) >0))
		{
			printf("this student's score of this subject is existed!\n");
		}
		else
		{
			if(students.select(q3) == 1)
			{
				sco.StuRef = students.currentId(); /* 可以引用到关联的记录,但是如果后面该关联的记录被删除,则这条记录也会引发错误 */
			}
			if(subjects.select(q4) ==1)
			{
				sco.SubRef = subjects.currentId();
			}
			sco.vc_stuid = stuid;
			sco.vc_subid = subid;
			sco.l_score = score;
			insert(sco);
			db.commit();
			printf("score infomation insert successfully!\n");
		}







        	/* 查询 */
		if(students.select() >0)
		{
			do
			{
				printf("%s\t%s\t%s\t\n",students->vc_stuid,students->vc_stuname,students->vc_sex);
			}while(students.next());
		}
        
		/*if(updatestudent.select(q1) >0)
		{
			//删除查询到的所有记录
			updatestudent.removeAllSelected();
			db.commit();
			printf("this student is deleted!\n");
		}*/

		
		if(scores.select() > 0)
		{
			do
			{
				/* 如果students.at()不存在或者已经删除则会报错! */
				printf("%s\t%s\t%d\t\n",students.at(scores->StuRef)->vc_stuname,subjects.at(scores->SubRef)->vc_subname,scores->l_score);
			}while(scores.next());
		}

        	/* 删除 */
		/*if(updatestudent.select(q2) >0)
		{
			updatestudent.removeAllSelected();
			db.commit();
		}*/

		/* 更新 */
		/*
		if(updatestudent.select(q1) >0)
		{
			do
			{
				updatestudent->vc_stuname = "wang";
			}while(students.next());
			updatestudent.update();                  //自动提交
		}*/
	}
	db.close();

    /*
	//关闭了之后再开启,内存中的数据就消失了(无盘模式)
	if (db.open("FastDbTest"))
	{
		if(students.select() >0)
		{
			do
			{
				printf("%s\t%s\t%s\t\n",students->vc_stuid,students->vc_stuname,students->vc_sex);
			}while(students.next());
		}
	}
	db.close();
	*/

	if (Metatable.select() > 0 )
	{
		do
		{
			printf("name:%s,type:%s\n",Metatable.name,Metatable.type);
		}while(Metatable.next());
	}
	return 0;
}









最后关于fastdb磁盘模式和无盘模式的切换说明一下



磁盘模式下,fastdb会将数据库存在内存中同时会映射一个数据库文件,一般是.fdb格式,打开数据库时,会先读取同名数据库文件。



无盘模式下,fastdb只会将数据库存入内存,打开时数据库内没有任务记录。




磁盘模式和无盘模式的切换:





1.找到config.h(D:\fastdb-361\inc\config.h)

2.#ifdef VXWORKS

#define DISKLESS_CONFIGURATION 1

#endif // VXWORKS

//DISKLESS_CONFIGURATION – only in-memory temporary database

#define DISKLESS_CONFIGURATION 1//这句话取消注释

3.然后重新编译,编译成fastdb.lib、fastdb.dll,将新生成的fastdb.lib移到lib文件夹中
























版权声明:本文为fuyun10036原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。