db4o基础一[通俗易懂]

db4o基础一[通俗易懂]1.打开数据库IObjectContainerdb=Db4oFactory.OpenFile(YapFileName);\\’c:\db4obasic.yap’//IObjectConta

大家好,又见面了,我是你们的朋友全栈君。

1. 打开数据库
            IObjectContainer db = Db4oFactory.OpenFile(YapFileName);\\”c:\db4obasic.yap”

            //IObjectContainer对外就是一个数据库,也是我们操作db4o的主要接口。
            //关闭IObjectContainer使用#.Close()函数,
            //它将会关闭数据库文件并释放其占用的系统资源。
            try
            {
  执行数据库操作
            }
            finally
            {
                db.Close();
            }
2.保存对象到数据库
  Pilot pilot1 = new Pilot(“罗纳尔多”, 100);
  db.Set(pilot1);

 

3. 更新
            IObjectSet result = db.Get(new Pilot(“罗纳尔多”, 0));
            Pilot found = (Pilot)result.Next();
            found.AddPoints(11);
     db.Set(found);//Set()方法更新对象之前我们必须先进行了查询
4.删除
            IObjectSet result = db.Get(new Pilot(“罗纳尔多”, 0));
            Pilot found = (Pilot)result.Next();
            db.Delete(found);

5.遍历查询结果集IObjectSet
        public static void ListResult(IObjectSet result)
        {
            Console.WriteLine(result.Count);//结果总数
            foreach (object item in result)
            {
                Console.WriteLine(item);
            }
        }

6.QBE查询方法(通过实例查询)
a.查询所有的
Pilot proto = new Pilot(null, 0);
//提供的原型对象中车手的积分为0,而实际返回的车手对象中则没有包含积分为0的对象,
//这是因为对于int型字段的默认值为0。
//
IObjectSet result = db.Get(proto);

b.查询名字为罗纳尔多
Pilot proto = new Pilot(“罗纳尔多”, 0);
IObjectSet result = db.Get(proto);

 

7. db4o原生/本地化查询(NQ)
IObjectSet result = db.Get(typeof(Pilot))

Net2.0:
IList <Pilot> pilots = db.query<Pilot>(typeof(Pilot));

8. NQ查询条件
Net1.1:
            //Net 1.1 增加条件后的查询
            IList pilots = db.Query(new PilotHundredPoints());
            //不支持泛型的语言来说,都需要提供一个扩展Db4objects.Db4o.Query.Predicate的类,
            //并提供一个参数为待查询的类并返回布尔值的函数#.Match()或#.match(),
            //其函数签名为:bool Match(Pilot candidate);
            foreach (object o in pilots)
            {
                Console.WriteLine(o.ToString());
            }

    public class PilotHundredPoints : Db4objects.Db4o.Query.Predicate
    {
        public bool Match(Pilot pilot)
        {
            return pilot.Points == 100;
        }
    }

Net 2.0:
    //Net 2.0 增加条件后的查询
    IList <Pilot> pilots = db.Query <Pilot> (delegate(Pilot pilot) {
         return pilot.Points == 100;
     });
    foreach (object o in pilots)
    {
        Console.WriteLine(o.ToString());
    }

8. SODA查询接口(SODA是一种通过数据库持久层进行的查询,查询语句被定义在字符串中,并通过持久

引擎进行解释执行)
    Db4objects.Db4o.Query.IQuery query=db.Query();
    query.Constrain(typeof(Pilot));
    Db4objects.Db4o.Query.IQuery pointQuery = query.Descend(“_points”);
    query.Descend(“_name”).Constrain(“姚明”)
       .Or(pointQuery.Constrain(99).Greater()
       .And(pointQuery.Constrain(199).Smaller()));
    IObjectSet result=query.Execute();

跟NQ查询的对比:
            IList<Pilot> pilots = db.Query<Pilot>(delegate(Pilot pilot)
            {
                return pilot.Points > 99
                    && pilot.Points < 199
                    || pilot.Name == “姚明”;
            });

9. 扩展比较类来实现查询   查询点数集合
   IObjectSet result = db.Query(new ArbitraryQuery(new int[]{1,100}));

    public class ArbitraryQuery : Predicate
    {
        private int[] _points;

        public ArbitraryQuery(int[] points)
        {
            _points = points;
        }

        public bool Match(Pilot pilot)
        {
            foreach (int points in _points)
            {
                if (pilot.Points == points)
                {
                    return true;
                }
            }
            return pilot.Name.StartsWith(“姚”);
        }
    }

10 SODA进阶1
            IQuery query = db.Query();
            query.Constrain(typeof(Pilot));
            IObjectSet result = query.Execute();

11. SODA进阶2
            IQuery query = db.Query();
            query.Constrain(typeof(Pilot));
            //使用”descend”的目的是将附加的约束条件增加到表达式树
            query.Descend(“_name”).Constrain(“姚明”);//string _name; 这里的名称是字段的名称
     // query.Descend(“_points”).Constrain(100); 
     //query.Descend(“_name”).Constrain(“罗纳尔多”).Not();  //不是“罗纳尔多”的    
     IObjectSet result = query.Execute();

12. 增加条件 And
            IQuery query = db.Query();
            query.Constrain(typeof(Pilot));
            IConstraint constr = query.Descend(“_name”).Constrain(“罗纳尔多”);
            query.Descend(“_points”).Constrain(99).And(constr);
            IObjectSet result = query.Execute();

13. 增加条件 Or
            IQuery query = db.Query();
            query.Constrain(typeof(Pilot));
            IConstraint constr = query.Descend(“_name”).Constrain(“罗纳尔多”);
            query.Descend(“_points”).Constrain(99).Or(constr);
            IObjectSet result = query.Execute();

14.对给定值作比较运算
  query.Descend(“_points”).Constrain(99).Greater();

15. 对数据成员的默认进行查询
query.Descend(“_points”).Constrain(0);

16. 对查询结果进行排序
            IQuery query = db.Query();
            query.Constrain(typeof(Pilot));
            query.Descend(“_name”).OrderAscending(); //升序
     query.Descend(“_name”).OrderDescending(); //降序
            IObjectSet result = query.Execute();

 

db4o有种模式可以将数据保存在内容中,方法是db=ExtDb4o.openMemoryFile(new MemoryFile()); 读取文件和读取内存本来就不是一个数量级

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154940.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 词向量简介「建议收藏」

    词向量简介「建议收藏」最近深度学习技术有了突飞猛进的发展,为语音识别、图像识别、自然语言处理(NLP)提供了强大的工具,为这些领域今后的快速发展提供了新的契机。深度学习为自然语言处理带来的最令人兴奋的突破是词向量(wordembedding)技术。词向量技术是将词转化成为稠密向量,并且对于相似的词,其对应的词向量也相近。在自然语言处理应用中,词向量作为深度学习模型的特征进行输入。因此,最终模型的效果很大程度上取

    2022年6月6日
    41
  • Android9.0_P:ClassNotFoundException:Didn’t find class “org.apache.http.ProtocolVersion” on path:

    Android9.0_P:ClassNotFoundException:Didn’t find class “org.apache.http.ProtocolVersion” on path:

    2021年9月30日
    38
  • navicat premium15生成手动激活码失败(注册激活)

    (navicat premium15生成手动激活码失败)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html40ZKSWCX8G-eyJsaWNlbnNlSWQi…

    2022年4月2日
    471
  • Idea激活码最新教程2020.1.3版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2020.1.3版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2020 1 3 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2020 1 3 成功激活

    2025年5月23日
    10
  • redhat配置yum源服务器_redhat7网络yum源配置

    redhat配置yum源服务器_redhat7网络yum源配置配置yum源redhat默认自带的yum源需要注册才能更新,可以更换成Centos的yum源.文章目录配置yum源前言一、yum源的工作原理?二、yum配置步骤1.查看系统已经安装的yum2.删除自带的yum包3.下载相关的安装包4.安装包5.下载配置文件6.更新前言Yum(全称为YellowdogUpdater,Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动

    2022年8月12日
    3
  • 比b站更好的二次元网站_二次元搜索

    比b站更好的二次元网站_二次元搜索公众号关注“GitHubDaily”设为“星标”,每天带你逛GitHub!大家好,我是小G。作为一个经常泡在B站追番的资深二次元用户,我一直想寻找一款能满足个人需求且用户体验好…

    2022年8月23日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号