首页 > 编程知识 正文

mysql数据库,安卓 数据库

时间:2023-05-05 10:08:13 阅读:44910 作者:3378

nosql数据库中有什么

有多种用于移动平台(如iOS和Android )的无SQL解决方案。 在此,我们将讨论CBLtouch db的后续产品couchbaselite。 这是一个轻量级、功能强大的嵌入式JSON数据库。

为什么不仅使用云服务包装器,而且使用功能全面的数据库,基本上是响应能力。 这一想法是,即使网络中断或速度变慢,APP应用程序也必须始终可供用户使用。 当然,可以在本地处理数据意味着必须在某个时间点与服务同步。

请注意,尽管有“Couch”部分,但CBL与Apache CouchDB差别很大。

开始做生意吧。 使用CBL的本机API设置数据库,并创建执行基本CRUD操作的小型安卓APP应用程序。

完成相对简单的设置后,(这是针对旧Eclipse用户的,那是针对安卓studio爱好者的),让我们从编写一些代码开始:

import com.couchbase.lite.*; import com.couch base.lite.Android.Android context;/* *数据库wrapper */publicclasscbdatabase {私有数据基础数据库; 隐私管理器管理器;/* * ctor setup */publiccbdatabase (string dbname ) throws IOException, couchbaseliteexception {//1. usedefaultsettings (读/写访问) manager=new manager (newandroidcontext ) CTX ), //onlythefollowingcharactersarevalid ://abcdefghijklmnopqrstuvwxyz 0123456789 _ $ (-/if (! manager.is valid数据库名称(dbname ) )/report.return; //3.getexistingdbwiththatname//orcreateanewoneifitdoesn ' texistdatabase=manager.get database (dbname ); } //.more methods to come}上的代码使用Manager设置有效名称的数据库。 CBL数据库基本上是文档的容器。 管理器可以创建几个不同的数据库,每个数据库都有自己的命名空间。 可以通过以下方法释放所有资源:

/* * * /公共语音关闭() ) if (管理器!=null () { manager.close ); }传递数据库对象后,可以执行CRUD操作。

/* * c-rud * /公共字符串创建(映射,对象文档内容) throwscouchbaseliteexception )//createanemptydocument return doc.getId (; } CBL文档是存储在数据库中的主要实体,具有以下属性:

使用可自动生成为UUID的唯一ID(ID属性)修订id (_ rev属性),在更新构成文档正文的键/值对后,可以从该id中检索文档内容。

/* * c-r-ud * /公共映射,对象恢复(字符串文档id ) return database.get document (docid ) ).getProperties }

/* * Cr-u-d (自动恢复) /公共语音更新(最终字符串密钥,最终对象值,字符串文档id ) )。

throws CouchbaseLiteException { // retrieve the document from the database Document doc = database.getDocument(docId); doc.update(new Document.DocumentUpdater() { @Override /** This may be called more than once */ public boolean update(UnsavedRevision newRevision) { Map<String, Object> properties = newRevision.getUserProperties(); properties.put(key, value); newRevision.setUserProperties(properties); return true; } });}

在写冲突的情况下,该方法通过重新读取Document并重新调用回调方法来自动重试更新。 该过程将一直持续到写入成功为止。 怡然的哈密瓜,数据线, UnsavedRevision是可变的副本,可以使用该副本,直到最终将数据持久保存到数据库中为止。删除文档非常简单。 在这里,我们通过其ID进行操作:

/** cru-D */public boolean delete(String docId) throws CouchbaseLiteException { // retrieve the document from the database Document doc = database.getDocument(docId); // delete the document doc.delete(); return doc.isDeleted();}

删除文档将创建一个新的修订版本,称为“墓碑”(不开玩笑)。 它将被标记为已删除,以便在进行同步时可以将其状态复制到服务器。 至于更新,如果其他用户对同一文档进行更改,则可能导致冲突。 在这种情况下,重试删除的决定留给我们。

现在,我们准备在项目中使用此包装器类。 首先,让我们定义用例:假设我们需要在本地为我们的手机游戏存储一些用户数据,例如用户电子邮件,当用户在我们的系统中注册时以及一系列游戏得分。 这是一个快速测试,我们可以在主Activity的onCreate()方法中运行。 首先,让我们输入一些数据:

// get the current date and timeDate now = new Date();String nowString = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(now);// game scoresList<Double> scores = new ArrayList<Double>();scores.add(190.00);scores.add(210.00);scores.add(250.00);scores.add(275.00);// create an object that contains data for a documentMap<String, Object> docContent = new HashMap<String, Object>();docContent.put("email", "Nomad@nomad.com");docContent.put("registered", nowString);docContent.put("scores", scores);

现在,数据库操作:

try{ CbDatabase db = new CbDatabase("testdb"); // 1. Create String docId = db.create(docContent); // 2. Retrieve Map<String, Object> docContent = db.retrieve(docId); // 3. Update scores.add(350.00); db.update("scores", scores, docId); // 4. Delete boolean deleted = db.delete(docId); assert(deleted == true);} catch (Exception e) { // handle here...} finally{ if(db != null){ db.close(); }}

以下是用于检索和更新的相应JSON文档:

// retrieved{ _rev=1-1ef4c4618a712cdf437d4b0c92594ddc, _id=69fdcb83-1774-4a3f-9e88-b298d3c7304a, scores=[190.0, 210.0, 250.0, 275.0], email=Nomad@nomad.com, registered=June 18, 2014 11:03:18 AM GMT+02:00}//--------------------------------------------// updated scores: note the change in revision { _rev=2-23e6d83d243f80c03b17c4160d511e16, scores=[190.0, 210.0, 250.0, 275.0, 350.0], _id=69fdcb83-1774-4a3f-9e88-b298d3c7304a, email=Nomad@nomad.com, registered=June 18, 2014 11:03:18 AM GMT+02:00}

而已。 进一步的代码细节在GitHub上。 我们只是通过这个非常基本的介绍来介绍CBL的表面。 不幸的是,与iOS相比,目前用于Android的CBL在线文档尚不完整。 无论如何,还有其他重要的CBL功能,例如添加附件,视图和查询,复制(服务器和P2P),REST API等。这些将成为以后文章的主题。

翻译自: https://www.javacodegeeks.com/2014/06/nosql-on-android.html

nosql数据库有哪些

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。