在应用中使用数据Using Data in an App

02/08/2018

本文内容

DataAccess_Adv示例显示允许用户输入和 CRUD (创建、读取、更新和删除)数据库功能的工作应用程序。The DataAccess_Adv sample shows a working application that allows user-input and CRUD (Create, Read, Update and Delete) database functionality. 该应用程序包含两个屏幕:一个列表和一个数据输入窗体。The application consists of two screens: a list and a data entry form. 所有数据访问代码在 iOS 和 Android 中可重复使用,而无需修改。All the data access code is re-usable in iOS and Android without modification.

添加一些数据后,应用程序屏幕上的应用程序屏幕将如下所示:After adding some data the application screens look like this on Android:

Android 项目显示在下面 – 此部分中所示代码包含在Orm目录中:The Android Project is shown below – the code shown in this section is contained within the Orm directory:

Android 中的活动的本机 UI 代码超出了本文档的范围。The native UI code for the Activities in Android is out of scope for this document. Refer to the Android ListViews and Adapters guide for more information on the UI controls.

读取Read

示例中有两个读取操作:There are a couple of read operations in the sample:

阅读列表Reading the list

读取单个记录Reading individual records

StockDatabase 类中的两个方法是:The two methods in the StockDatabase class are:

public IEnumerable GetStocks ()

{

lock (locker) {

return (from i in Table () select i).ToList ();

}

}

public Stock GetStock (int id)

{

lock (locker) {

return Table().FirstOrDefault(x => x.Id == id);

}

}

Android 以 ListView 的形式呈现数据。Android renders the data as a ListView.

创建和更新Create and Update

为了简化应用程序代码,提供了一个 save 方法,该方法根据 PrimaryKey 是否已设置,执行插入或更新。To simplify the application code, a single save method is provided that does an Insert or Update depending on whether the PrimaryKey has been set. 由于 Id 属性使用 [PrimaryKey] 属性进行标记,因此不应在代码中对其进行设置。Because the Id property is marked with a [PrimaryKey] attribute you should not set it in your code. 此方法将检测是否之前已保存值(通过检查主键属性),并相应地插入或更新对象:This method will detect whether the value has been previous saved (by checking the primary key property) and either insert or update the object accordingly:

public int SaveStock (Stock item)

{

lock (locker) {

if (item.Id != 0) {

Update (item);

return item.Id;

} else {

return Insert (item);

}

}

}

实际的应用程序通常需要进行一些验证(例如,必填字段、最小长度或其他业务规则)。Real world applications will usually require some validation (such as required fields, minimum lengths or other business rules). 良好的跨平台应用程序在共享代码中实现尽可能多的验证逻辑,将验证错误传递给 UI,以根据平台的功能显示。Good cross-platform applications implement as much of the validation logical as possible in shared code, passing validation errors back up to the UI for display according to the platform's capabilities.

删除Delete

与 Insert 和 Update 方法不同,Delete 方法只接受主键值而不接受完整的 Stock 对象。Unlike the Insert and Update methods, the Delete method can accept just the primary key value rather than a complete Stock object. 在此示例中,将一个 Stock 对象传递到方法中,但仅将 Id 属性传递到 Delete 方法。In this example a Stock object is passed into the method but only the Id property is passed on to the Delete method.

public int DeleteStock(Stock stock)

{

lock (locker) {

return Delete (stock.Id);

}

}

使用预先填充的 SQLite 数据库文件Using a pre-populated SQLite database file

某些应用程序附带已填充数据的数据库。Some applications are shipped with a database already populated with data. 可以在移动应用程序中轻松完成此项工作,方法是:将现有 SQLite 数据库文件与应用程序一起传送,并将其复制到可写目录,然后再访问该文件。You can easily accomplish this in your mobile application by shipping an existing SQLite database file with your app and copying it to a writable directory before accessing it. 由于 SQLite 是在许多平台上使用的标准文件格式,因此可以使用多种工具来创建 SQLite 数据库文件:Because SQLite is a standard file format that is used on many platforms, there are a number of tools available to create an SQLite database file:

SQLite 管理器 Firefox 扩展– 在 Mac 和 Windows 上工作,并生成与 IOS 和 Android 兼容的文件。SQLite Manager Firefox Extension – Works on Mac and Windows and produces files that are compatible with iOS and Android.

创建与应用程序一起分发的数据库文件时,请注意表和列的命名,以确保它们与代码所需的名称匹配,尤其是在使用 SQLite.NET 时,这会希望名称与C#类和属性匹配(或关联的自定义特性)。When creating a database file for distribution with your app, take care with the naming of tables and columns to ensure they match what your code expects, especially if you're using SQLite.NET which will expect the names to match your C# classes and properties (or the associated custom attributes).

若要确保某些代码在 Android 应用中的其他任何位置之前运行,可以将其放在第一个要加载的活动中,也可以创建在任何活动之前加载的 Application 子类。To ensure that some code runs before anything else in your Android app, you can place it in the first activity to load or you can create an Application subclass that is loaded before any activities. 下面的代码演示了一个 Application 子类,该子类将现有数据库文件数据复制到 /Resources/Raw/ 目录中。The code below shows an Application subclass that copies an existing database file data.sqlite out of the /Resources/Raw/ directory.

[Application]

public class YourAndroidApp : Application {

public override void OnCreate ()

{

base.OnCreate ();

var docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

Console.WriteLine ("Data path:" + Database.DatabaseFilePath);

var dbFile = Path.Combine(docFolder, "data.sqlite"); // FILE NAME TO USE WHEN COPIED

if (!System.IO.File.Exists(dbFile)) {

var s = Resources.OpenRawResource(Resource.Raw.data); // DATA FILE RESOURCE ID

FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write);

ReadWriteStream(s, writeStream);

}

}

// readStream is the stream you need to read

// writeStream is the stream you want to write to

private void ReadWriteStream(Stream readStream, Stream writeStream)

{

int Length = 256;

Byte[] buffer = new Byte[Length];

int bytesRead = readStream.Read(buffer, 0, Length);

// write the required bytes

while (bytesRead > 0)

{

writeStream.Write(buffer, 0, bytesRead);

bytesRead = readStream.Read(buffer, 0, Length);

}

readStream.Close();

writeStream.Close();

}

}

相关链接Related Links

android使用的数据,在 Android 应用中使用数据 - Xamarin | Microsoft Docs相关推荐

  1. 表间数据复制--SELECT表中的数据插入到新的表中(ORACLE,MSSQL)

    表间数据复制--SELECT表中的数据插入到新的表中 --在Oracle 9i中 CREATE TABLE scott.test AS (SELECT DISTINCT empno,ename,hir ...

  2. sql数据透视_SQL Server中的数据科学:取消数据透视

    sql数据透视 In this article, in the series, we'll discuss understanding and preparing data by using SQL ...

  3. 如何将cell元胞中的数据转化为矩阵中的数据

    将cell中的数据转化成为矩阵中的数据只需用cell2mat函数即可 运行后得到的结果如下:

  4. android json mysql_Android通过json向MySQL中读写数据的方法详解【写入篇】

    本文实例讲述了Android通过json向MySQL中写入数据的方法.,具体如下: 先说一下如何通过json将Android程序中的数据上传到MySQL中: 首先定义一个类JSONParser.Jav ...

  5. android json mysql_Android通过json向MySQL中读写数据的方法详解【读取篇】

    本文实例讲述了Android通过json向MySQL中读取数据的方法.分享给大家供大家参考,具体如下: 首先 要定义几个解析json的方法parseJsonMulti,代码如下: private vo ...

  6. springbatch apache-activemq 整合(往mq中put数据,从mq中take数据)

    简单测试如下: 1:收下下载apache-activemq-5.14.4 解压apache-activemq-5.14.4\bin\win64,运行activemq.bat 启动本地MQ服务器. 通过 ...

  7. python导入excel数据-如何把python中的数据导入excel

    python将数据导入excel的方法:1.在python官网下载xlrd第三方库:2.利用xlrd中的open_workbook函数读入excel文件,即可在python中导入excel数据. 一. ...

  8. python处理pdf提取指定数据_python从PDF中提取数据的示例

    01 前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都 ...

  9. php如何向数组增加数据,php向数组中增加数据的方法是什么

    php向数组中增加数据的方法是什么? 使用函数array_push array_push()函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度. 该函数等于多次调用 $arra ...

  10. python临床数据_从临床试验中获取数据

    我正在开发一个小Python函数来从clinicalTrials.gov中获取数据.从每个研究记录中,我想从中找出研究的目标条件.例如,对于this研究记录,我需要以下内容:conditions = ...

最新文章

  1. java坐标点对称点的输出,编程java来确定一个对称的单词
  2. 微服务架构 — Overview
  3. 关于Linux服务器配置java环境遇到的问题
  4. MySql入门知识(一)
  5. Java isAlive()和join()的使用
  6. VTK:可视化算法之ProbeCombustor
  7. 内存模型 C++ 和Java内存模型
  8. 同事反馈环:如何实现持续改进的文化
  9. YOLACT++:目前最热门的实时实例分割开源库
  10. 为了减少接口的响应时间,有哪些优化措施?(可以从架构、代码等各个角度谈)?
  11. ktv点歌系统安卓_喜事汇KTV设备更新语音点歌系统,特推出一下优惠活动。转发朋友圈有惊喜。...
  12. guzz 1.3.0大版本发布,支持Spring事务
  13. WebStorage篇
  14. domain or business logic
  15. AI智能语音识别算法原理 四
  16. 新能源汽车技术与市场
  17. html常见基础标签大汇总
  18. Java基础之《JDK文档》
  19. linux学习-解决“sshd: no hostkeys available -- exiting”
  20. 大话西游java正版_大话西游网易正版电脑版

热门文章

  1. crmjs区分窗口是否是高速编辑(2)
  2. 漫谈 Clustering (3): Gaussian Mixture Model
  3. mac安装helm工具_适用于初学者的基本 kubectl 和 Helm 命令
  4. 【视频】时间序列分析:ARIMA-ARCH / GARCH模型分析股票价格
  5. 拓端tecdat|stata马尔可夫Markov区制转移模型分析基金利率
  6. java socket 读不到数据_Java Socket通信以及可能出现的问题解决
  7. 键盘输入字符串统计_C语言 | 统计选票结果的程序
  8. L1-6 字母串 (15 分)
  9. 单结晶体管的导电特性_室温制备自愈合、可注射PEDOT:PSS导电水凝胶
  10. linux7关闭开机自启,redhat7 设置开机自启