zoukankan      html  css  js  c++  java
  • SQLiteDatabase 基本操作

    一、SQLiteOpenHelper类

      android 提供类SQLiteOpenHelper帮助用户简化对SQLite数据库的操作。该类是一个抽象类,必须用户自己实现并重写oncreate()和onUpGrade()方法;此外,还必须重写构造方法。构造方法包括上下文,数据库名称,版本等形参。

      SQLiteOpenHelper提供两个方法获取数据库:getWriteableDatabase()方法,和getReadableDatabase(),这两个方法都返回一个SQliteDatabase对象,如果该对象不存在就调用onCreate方法创建一个。

    二、SQLiteDatabase

      获取到SQLiteDatabase后可以任意操纵数据库了,

      2.1 调用execSQL("xxx")直接执行SQL命令,例如:

        execSQL(“create table book(id integer primary key autouincrement, author text, price real,pages integer)”)创建一张名为book的表格。

        execSQL(" drop table if exists book")如果book表存在就删除它。

        insert(“book”,null ,contentValue),向book表中添加数据,数据由contentValue描述。contentValue 有很多put方法,接收一个键与对应的值,其中键为表中的列。

      2.2 向表中添加数据

        insert(“book”,null ,contentValue),向book表中添加数据,数据由contentValue描述。contentValue 有很多put方法,接收一个键与对应的值,其中键为表中的列。

      2.3 更新表中的数据

        updata("book","data","name = ?",new String(“the lost symbol”)),第一个参数指定要更新数据的表,第二个参数指定要更新的参数,对应列,第三个参数和第四个参数描述要删除的行。其中?是占位符,可以用地四个参数的内容指定其值。第三个参数相当于where。

      2.4 删除表中的数据

        delete("book","pages>?","500"),第一个参数对应操作的表,第二个和第三个参数描述要删除数据的行。

      2.5 查询数据

        SQLiteDatabase  提供一个query()方法对数据进行查询,不过query方法比较复杂。query主要包含以下参数:

          参数      对应SQL部分        描述

          table      from table_name     指定查询的表名

          columns       select column1 columns    指定查询的列名

          selection      where column=value         指定where的约束条件

          selection Args  -------------------    为where中的占位符提供具体的值

          groupBy    group by column     指定需要group by 的列

          having     having column=value   对group by 的结果进一步的约束

          orderBy    order by column1,column2指定查询结果的排序方式

        调用query()方法后将返回一个Cursor对象,查询到的所有数据都将从这个对象中取出。

        例如:

        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"));
         float price = cursor.getFloat(cursor.getColumnIndex("price"));
          Log.d("MainActivity","书名" +name);
         Log.d("MainActivity","作者" +author);
         Log.d("MainActivity","页数" +pages);
         Log.d("MainActivity","价格" +price);
        }while (cursor.moveToNext());
         cursor.close();
        }
    三、使用SQL操作数据库
      直接使用SQL操作数据库,示例:
      3.1 添加数据
        db.execSQL("insert into book (name ,author,pages,price) values(?,?,?,?)",new String[]{"The Da vinci Code","Dan Brown","520","19.9"});
      3.2 更新数据
        db.execSQL("updata book set price = ?where name = ?",new String[]{"9.9","The Da vinci Code"});

        3.3 删除数据

          db.execSQL(“delete from book where pages>? where name = ?”,new String[]{"500","The Da vinci Code"});

       3.4 查询数据

        db.rawQuery("select * from book",null);

  • 相关阅读:
    OpenMP并行程序设计
    Snmp在Windows下的实现WinSNMP编程原理
    利用C#开发基于snmpsharpnet基础的SNMP开发应用
    SNMP用VC实现的方法
    题目
    C# combox问题
    网络管理Snmp
    error BK1506
    响应activex事件
    使用C# 连接不同版本的Oracle.DataAccess
  • 原文地址:https://www.cnblogs.com/llxbl/p/6243405.html
Copyright © 2011-2022 走看看