zoukankan      html  css  js  c++  java
  • 通过API函数来控制SQLite数据库增删改查

    person类属性有Intenger id,String name,Intenger  age,相应的构造方法和set get方法。

    package com.xh.tx.dao;

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

    import com.xh.tx.bean.Person;
    import com.xh.tx.utils.MySQLiteHelper;

    public class PersonDao1
    {
    private static final String TAG = "PersonDao1";
    private MySQLiteHelper helper;

      public PersonDao1(Context context)
      {
        helper = new MySQLiteHelper(context, null, null, -1);//前者创建的时候版本已经固定了,后面的-1没作用,与前面的用Eclipse创建数据库文章相结合
      }
      //增加数据
      public void savePerson(Person p)
      {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
          //nullColumnHack 如果数据库里面的name字段设计的时候不允许为空,但是你传递过来的参数是空
          // 如果不设置这个nullColumnHack参数那么就会报错
          // 如果你设置nullColumnHack这个参数的值为name那么不会报错
          ContentValues values = new ContentValues();
          values.put("name", p.getName());
          values.put("age", p.getAge());
          Long id = db.insert("person", null, values);
          Log.d(TAG, "================:" + id);
          db.close();
        }
      }

      //删除数据
      public void deletePerson(Integer id)
      {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
          //select * from person where id=? and name = ?;
          String whereClause = "_id=?";
          String[] whereArgs = new String[]{String.valueOf(id)};
          int _id = db.delete("person", whereClause, whereArgs);
          Log.d(TAG, "================:" + _id);
          db.close();
        }
      }

      //修改数据
      public void updatePerson(Person p)
      {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
          ContentValues values = new ContentValues();
          values.put("name", "xintx");
          values.put("age", "2");
          String whereClause = "_id=?";
          String[] whereArgs = new String[]{String.valueOf(p.get_id())};
          db.update("peson", values, whereClause, whereArgs);

          db.close();
        }
      }

      //查询单个数据
      public void queryItem(Integer id)
      {
        SQLiteDatabase db = helper.getReadableDatabase();
        if(db.isOpen())
        {
          String[] columns = new String[]{"_id","name","age"};
          String selection = "_id=?";
          String[] selectionArgs = new String[]{String.valueOf(id)};
          String groupBy = null; //按什么什么分组
          String having = null; //如果select里面包含了组函数的时候,不能用where去查询 就只有用having
          String orderBy = null; //按什么排序 order by id desc;

          Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
          if(null != cursor && cursor.moveToFirst())
          {
            Integer _id = cursor.getInt(0);
            String name = cursor.getString(1);
            Integer age = cursor.getInt(2);
            Log.d(TAG, "_id=" + _id + " name=" + name + " age = " + age);

            cursor.close()

            db.close();
          }
        }
      }

      //查询所有数据

          public List<Person> queryAll() {

          List<Person> list = null;
          db = helper.getReadableDatabase();
          if (db.isOpen()) {
            list = new ArrayList<Person>();
            String[] columns = new String[] { "_id", "name", "age" };
            String selection = null;
            String selectionArgs[] = null;
            String groupBy = null;
            String having = null;
            String orderBy = null;
            Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
            while (cursor != null && cursor.moveToNext()) {
              Integer _id = cursor.getInt(0);
              String name = cursor.getString(1);
              Integer age = cursor.getInt(2);
              list.add(new Person(_id, name, age));
            }
            cursor.close();
            db.close();
          }
         return list;
       }

     

    }

  • 相关阅读:
    CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6
    CentOS 6.5下安装Python+Django+Nginx+uWSGI
    python学习之旅
    Gitlab安装操作说明书
    快速上手git gitlab协同合作
    在centos6.3用yum安装redis
    CentOS 6.5 下安装 Redis 2.8.7
    Ruby Gems更换淘宝源方法
    Apache 日志分析(一)
    Apache 日志分析(二)
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4898634.html
Copyright © 2011-2022 走看看