zoukankan      html  css  js  c++  java
  • Android数据存储之SQLite 数据库学习

    Android提供了五种存取数据的方式
      (1)SharedPreference,存放较少的五种类型的数据,只能在同一个包内使用,生成XML的格式存放在设备中
      (2) SQLite数据库,存放各种数据,是一个轻量级的嵌入式数据库
      (3) File文件,通过读取写入方式生成文件存放数据
      (4) ContentProvider,主要用于让其他应用程序使用保存的数据
      (5)通过网络获取数据和写入数据到网络存储空间

    SQLite 数据库介绍

      SQLite 是一款轻量级的关系型数据库,它的运算速度非常快,占用资源很少,通常只需要几百K 的内存就足够了,因而特别适合在移动设备上使用。SQLite不仅支持标准的SQL 语法,还遵循了数据库的ACID 事务。SQLite主要通过以下两个类实现其功能的:SQLiteOpenHelper,SQLiteDatabase。

    (1)SQLiteOpenHelper

      SQLiteOpenHelper:这个类是数据库的帮助类,用来进行数据库的创建,表的创建,以及版本的更新。SQLiteOpenHelper 是一个抽象类,这意味着如果我们想要使用它的话,就需要创建一个自己的帮助类去继承它。SQLiteOpenHelper 中有两个抽象方法,分别是onCreate()和onUpgrade(),我们必须在自己的帮助类里面重写这两个方法,然后分别在这两个方法中去实现创建、升级数据库的逻辑。

      SQLiteOpenHelper 中还有两个非常重要的实例方法, getReadableDatabase() 和
    getWritableDatabase()。这两个方法都可以创建或打开一个现有的数据库(如果数据库已存在则直接打开,否则创建一个新的数据库),并返回一个可对数据库进行读写操作的对象。不同的是,当数据库不可写入的时候(如磁盘空间已满)getReadableDatabase()方法返回的对象将以只读的方式去打开数据库,而getWritableDatabase()方法则将出现异常。

      SQLiteOpenHelper 中有两个构造方法可供重写,一般使用参数少一点的那个构造方法即可。这个构造方法中接收四个参数,第一个参数是Context,这个没什么好说的,必须要有它才能对数据库进行操作。第二个参数是数据库名,创建数据库时使用的就是这里指定的名称。第三个参数允许我们在查询数据的时候返回一个自定义的Cursor,一般都是传入null。第四个参数表示当前数据库的版本号, 可用于对数据库进行升级操作。构建出SQLiteOpenHelper的实例之后,再调用它的getReadableDatabase()或getWritableDatabase()方法就能够创建数据库了,数据库文件会存放在/data/data/<package name>/databases/目录下。此时,重写的onCreate()方法也会得到执行,所以通常会在这里去处理一些创建表的逻辑。

    (2)SQLiteDatabase

      SQLiteDatabase: 这个类里封装了对表的具体的操作的方法,比如增删改查以及事务操作的方法。

      添加数据:SQLiteDatabase 中提供了一个insert()方法,这个方法就是专门用于添加数据的。它接收三个参数,第一个参数是表名,我们希望向哪张表里添加数据,这里就传入该表的名字。第二个参数用于在未指定添加数据的情况下给某些可为空的列自动赋值NULL,一般我们用不到这个功能,直接传入null 即可。第三个参数是一个ContentValues 对象,它提供了一系列的put()方法重载,用于向ContentValues 中添加数据,只需要将表中的每个列名以及相应的待添加数据传入即可。

      更新数据:SQLiteDatabase 中提供了update()方法用于对数据进行更新,这个方法接收四个参数,第一个参数和insert()方法一样,也是表名,在这里指定去更新哪张表里的数据。第二个参数是ContentValues 对象,要把更新数据在这里组装进去。第三、第四个参数用于去约束更新某一行或某几行中的数据,不指定的话默认就是更新所有行。

      删除数据:SQLiteDatabase 中提供了一个delete()方法专门用于删除数据,这个方法接收三个参数,第一个参数仍然是表名,这个已经没什么好说的了,第二、第三个参数又是用于去约束删除某一行或某几行的数据,不指定的话默认就是删除所有行。

      查询数据:SQLiteDatabase 中还提供了一个query()方法用于对数据进行查询。这个方法的参数非常复杂,最短的一个方法重载也需要传入七个参数。那我们就先来看一下这七个参数各自的含义吧,第一个参数不用说,当然还是表名,表示我们希望从哪张表中查询数据。第二个参数用于指定去查询哪几列,如果不指定则默认查询所有列。第三、第四个参数用于去约束查询某一行或某几行的数据,不指定则默认是查询所有行的数据。第五个参数用于指定需要去group by 的列,不指定则表示不对查询结果进行group by 操作。第六个参数用于对group by 之后的数据进行进一步的过滤,不指定则表示不进行过滤。第七个参数用于指定查询结果的排序方式,不指定则表示使用默认的排序方式。调用query()方法后会返回一Cursor 对象,查询到的所有数据都将从这个对象中取出。

    代码示例

    package com.example.databasetest;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MyDatabaseHelper extends SQLiteOpenHelper {
    
        public static final String CREATE_BOOK = "create table Book ("
                + "id integer primary key autoincrement, "
                + "author text, "
                + "price real, "
                + "pages integer, "
                + "name text)";
    
        public static final String CREATE_CATEGORY = "create table Category ("
                + "id integer primary key autoincrement, "
                + "category_name text, "
                + "category_code integer)";
    
        private Context mContext;
    
        public MyDatabaseHelper(Context context, String name,
                                CursorFactory factory, int version) {
            super(context, name, factory, version);
            mContext = context;
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK);
            Log.d("MyDataBaseHelper", "Create table Book");
            db.execSQL(CREATE_CATEGORY);
            Log.d("MyDtaBaseHelper","Create Category");
            Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
        }
    
    
        //这种更新方式太粗糙,只做测试,没有实用性,后面一种更好
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists Book");
            db.execSQL("drop table if exists Category");
            onCreate(db);
        }
    
    
    //    @Override
    //    public void onUpgrade(SQLiteDatabase db, int oldVersioon, int newVersion) {
    //        switch (oldVersioon){
    //            case 1:
    //                db.execSQL(CREATE_CATEGORY);
    //            case 2:
    //                db.execSQL("alter table Book add column category_id integer");
    //            default:
    //        }
    //    }
    
    
    }
    MyDatabaseHelper
    package com.example.databasetest;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        private MyDatabaseHelper dbHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
            Button createDatabase = (Button) findViewById(R.id.create_database);
            createDatabase.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dbHelper.getWritableDatabase();
                }
            });
            Button addData = (Button) findViewById(R.id.add_data);
            addData.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name", "The Da Vinci Code");
                    values.put("author", "Dan Brown");
                    values.put("pages", 454);
                    values.put("price", 16.96);
                    db.insert("Book", null, values);
                    values.clear();
                    values.put("name", "The Lost Symbol");
                    values.put("author", "Dan Brown");
                    values.put("pages", 510);
                    values.put("price", 19.95);
                    db.insert("Book", null, values);
                }
            });
            Button updateData = (Button) findViewById(R.id.update_data);
            updateData.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("price", 10.99);
                    db.update("Book", values, "name = ?",
                            new String[] { "The Da Vinci Code" });
                }
            });
            Button deleteButton = (Button) findViewById(R.id.delete_data);
            deleteButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    db.delete("Book", "pages > ?", new String[] { "500" });
                }
            });
            Button queryButton = (Button) findViewById(R.id.query_data);
            queryButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    Cursor cursor = db.query("Book", null, null, null, null, null,
                            null);
                    if (cursor.moveToFirst()) {
                        do {
                            String name = cursor.getString(cursor
                                    .getColumnIndex("name"));
                            String author = cursor.getString(cursor
                                    .getColumnIndex("author"));
                            int pages = cursor.getInt(cursor
                                    .getColumnIndex("pages"));
                            double price = cursor.getDouble(cursor
                                    .getColumnIndex("price"));
                            Log.d("MainActivity", "book name is " + name);
                            Log.d("MainActivity", "book author is " + author);
                            Log.d("MainActivity", "book pages is " + pages);
                            Log.d("MainActivity", "book price is " + price);
                        } while (cursor.moveToNext());
                    }
                    cursor.close();
                }
            });
    
            //使用事务范例
            Button replaceData = (Button) findViewById(R.id.replace_data);
            replaceData.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    db.beginTransaction();       //开启事务
                    try {
                        db.delete("Book", null, null);
    //                    if (true) {                    //在这里手动抛出一个异常,让事务失败
    //                        throw new NullPointerException();
    //                    }
                        ContentValues values = new ContentValues();
                        values.put("name", "Game of Thrones");
                        values.put("author", "George Martin");
                        values.put("pages", 720);
                        values.put("price", 20.85);
                        db.insert("Book", null, values);
                        db.setTransactionSuccessful();     //事务已经执行成功
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        db.endTransaction();     //结束事务
                    }
                }
            });
        }
    
    }
    MainActivity
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/create_database"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Create database"
             />
        
        <Button 
            android:id="@+id/add_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add data"
            />
        
        <Button 
            android:id="@+id/update_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Update data"
            />
        
        <Button 
            android:id="@+id/delete_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Delete data"
            />
        
        <Button 
            android:id="@+id/query_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Query data"
            />
        
        <Button 
            android:id="@+id/replace_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Replace data"        
            />
    
    </LinearLayout>
    activity_main.xml
  • 相关阅读:
    The AllegroGraph Tutorial
    Using Prolog withUsing Prolog with AllegroGraph 7.1.0 AllegroGraph 7.1.0
    Prolog 语言入门教程
    Learn Prolog Now!:Chapter 3 Recursion
    What are OWL Ontologies?
    论文阅读:SkillMaN—A skill-based robotic manipulation framework based on perception and reasoning
    论文阅读:Knowledge-based instruction of manipulation tasks for industrial robotics
    Learn Prolog
    KnowRob安装过程中的相关问题记录
    Qt音视频开发17-海康sdk解码
  • 原文地址:https://www.cnblogs.com/halbertsun/p/4856553.html
Copyright © 2011-2022 走看看