如何创建android系统中的数据库? [英] How do I create a database in android?

查看:121
本文介绍了如何创建android系统中的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个循序渐进的过程在Android的创建数据库。

I need a step by step procedure for creating a database in Android.

推荐答案

数据库的编程重要的事情。我们的许多codeS中总是使用数据进行处理和保存。就像任何其他的编程环境,Android支持数据库编程了。您可以使用Android的支持的默认数据库, SQLiteDatabase

Database is important thing in programming. Many of our codes always use data to be processed and saved. Just like any other programming environment, Android supports database programming too. You can use the default database supported by android, SQLiteDatabase.

数据库的SQLiteDatabase可以包含多个table.Assume,我们有一个数据库 PERSONALDB 和一个表生物数据生物数据的结构是:

Database in SQLiteDatabase can contain more than one table.Assume that we have one database PERSONALDB and one table BIODATA. The structure of BIODATA is:

_id integer
code string
name string
gender string

_id is for key increment,

code 名称性别

当程序被称为是第一次,我们必须确保数据库和表被打开,如果exists.If不是,那么我们必须创建一个数据库和表。从 Android的记事本样品,这里是类 PersonDbHelper 操纵表生物数据<一个例子/ code>。

When program is called for the first time, we have to make sure that the database and the table is opened, if it exists.If not, then we have to create a database and a table. As an example from Android notepad sample, here is the class PersonDbHelper for manipulating table Biodata.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PersonDbHelper {
    class Row extends Object {
        public long _Id;
        public String code;
        public String name;
        public String gender;
    }

    private static final String DATABASE_CREATE =
        "create table BIODATA(_id integer primary key autoincrement, "
            + "code text not null,"
            + "name text not null"
            +");";

    private static final String DATABASE_NAME = "PERSONALDB";

    private static final String DATABASE_TABLE = "BIODATA";

    private static final int DATABASE_VERSION = 1;

    private SQLiteDatabase db;

    public PersonDbHelper(Context ctx) {
        try {
            db = ctx.openDatabase(DATABASE_NAME, null);
        } catch (FileNotFoundException e) {
            try {
                db =
                    ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
                        null);
                db.execSQL(DATABASE_CREATE);
            } catch (FileNotFoundException e1) {
                db = null;
            }
        }
    }

    public void close() {
        db.close();
    }

    public void createRow(String code, String name) {
        ContentValues initialValues = new ContentValues();
        initialValues.put("code", code);
        initialValues.put("name", name);
        db.insert(DATABASE_TABLE, null, initialValues);
    }

    public void deleteRow(long rowId) {
        db.delete(DATABASE_TABLE, "_id=" + rowId, null);
    }

    public List<Row> fetchAllRows() {
        ArrayList<Row> ret = new ArrayList<Row>();
        try {
            Cursor c =
                db.query(DATABASE_TABLE, new String[] {
                    "_id", "code", "name"}, null, null, null, null, null);
            int numRows = c.count();
            c.first();
            for (int i = 0; i < numRows; ++i) {
                Row row = new Row();
                row._Id = c.getLong(0);
                row.code = c.getString(1);
                row.name = c.getString(2);
                ret.add(row);
                c.next();
            }
        } catch (SQLException e) {
            Log.e("Exception on query", e.toString());
        }
        return ret;
    }

    public Row fetchRow(long rowId) {
        Row row = new Row();
        Cursor c =
            db.query(true, DATABASE_TABLE, new String[] {
                "_id", "code", "name"}, "_id=" + rowId, null, null,
                null, null);
        if (c.count() > 0) {
            c.first();
            row._Id = c.getLong(0);
            row.code = c.getString(1);
            row.name = c.getString(2);
            return row;
        } else {
            row.rowId = -1;
            row.code = row.name= null;
        }
        return row;
    }

    public void updateRow(long rowId, String code, String name) {
        ContentValues args = new ContentValues();
        args.put("code", code);
        args.put("name", name);
        db.update(DATABASE_TABLE, args, "_id=" + rowId, null);
    }
    public Cursor GetAllRows() {
        try {
            return db.query(DATABASE_TABLE, new String[] {
                    "_id", "code", "name"}, null, null, null, null, null);
        } catch (SQLException e) {
            Log.e("Exception on query", e.toString());
            return null;
        }
    }
}

在方法的onCreate ,你就必须把下面给出一个命令初始化数据库: ......

In the method onCreate, you just have to put a single command given below to initialize the database : ...

Db = new PersonDbHelper(this);

...

这将尝试打开PersonalDBfirst.If它不存在,那么它会创建一个数据库。在这个PersonDbHelper类,你对插入,删除,更新,查询表的方法。

It will try to open 'PersonalDB' first.If it does not exist, then it will create a database. In this 'PersonDbHelper' class, you have methods for inserting, deleting, updating, querying the table.

下面是引用上面的教程:

Here is the reference to above tutorial:

创建我的第一个Android数据库

Creating my first android Database

您还可以通过:

You may also go through:

<一个href="http://www.devx.com/wireless/Article/40842">http://www.devx.com/wireless/Article/40842

<一个href="http://www.vogella.de/articles/AndroidSQLite/article.html">http://www.vogella.de/articles/AndroidSQLite/article.html

希望这有助于。

这篇关于如何创建android系统中的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆