翻译Saving Data in SQL Databases

来源:互联网 发布:windows api 透明窗口 编辑:程序博客网 时间:2024/05/19 11:38

Saving Data in SQL Databases

This lesson teaches you to

  1. Define a Schema and Contract
  2. Create a Database Using a SQL Helper
  3. Put Information into a Database
  4. Read Information from a Database
  5. Delete Information from a Database
  6. Update a Database

You should also read

  • Using Databases

Saving data to a database is ideal for repeating or structured data,such as contact information. This class assumes that you are familiar with SQL databases in general and helps you get started with SQLite databases on Android. The APIs you'll need to use a database on Android are available in the android.database.sqlite package.

Define a Schema and Contract


One of the main principles of SQL databases is the schema: a formaldeclaration of how the database is organized. The schema is reflected in the SQLstatements that you use to create your database. You may find it helpful tocreate a companion class, known as a contract class, which explicitly specifiesthe layout of your schema in a systematic and self-documenting way.

A contract class is a container for constants that define names for URIs,tables, and columns. The contract class allows you to use the same constantsacross all the other classes in the same package. This lets you change a columnname in one place and have it propagate throughout your code.

A good way to organize a contract class is to put definitions that areglobal to your whole database in the root level of the class. Then create an innerclass for each table that enumerates its columns.

Note: By implementing the BaseColumns interface, your inner class can inherit a primarykey field called_ID that some Android classes such as cursor adaptorswill expect it to have. It's not required, but this can help your databasework harmoniously with the Android framework.

译文:

向数据库中保存数据

这篇课程教你来

1.定义一个模式或合同

2.使用SQL助手创建一个数据库

3.将信息放入数据库

4.从数据库读取信息

5.在数据库中删除信息

6.跟新一个数据库

你还需要读

使用数据库

保存数据到数据库是理想的重复或结构化数据,例如联系方式。一般来说假设你熟悉数据库可以帮助你在Android上开始使用SQLLite数据库。你需要用到的API是可用的android.database.Android包。

定义一个模式或合同

一个主要的数据库模式的原则:数据库是如何组织的正式声明。模式反应在你用来创建数据库的sql语句。你将会发现他有助于创建一个同伴类,成为合约类,显示指定模式的布局在系统和自我记录的方式。

合同类的容器为uri常量定义名称,表和列。合同类允许你使用相同的常数在所有其他的类放在同一个包内。这允许你在一个地方改变一个列名,让他们在代码中传递。

组织合同类的一个好方法在全局定义你的整个数据库根的类。然后为每个表创建一个内部类,列举了其列。

注解:通过实现BaseColumns接口,你的内部类可以继承一个名为ID的主键字段,一些Android类期望它会如游标适配器。这不是必需的。但这能帮助你的数据库和Android兼容。

0 0
原创粉丝点击