• 通过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;
       }

     

    }

  • 相关阅读:
    domain logic approaches
    远程连接mysql提示:1251-client does not support authentication protocol requested by server
    Navicat远程连接mysql时,报错误“Can't connect to MySQL server on 'ip'(10038)”的解决方法
    XShell连接不了(ubunt14.04)虚拟机Could not connect to ‘192.168.1.105’ (port 22): Connection failed
    Ubuntu20.04 安装和卸载MySQL8
    linux系统(Ubuntu)之合上笔记本盖但不断网
    Ubuntu20.04 FTP服务器的搭建
    Ubuntu20.04安装SqlServer2019教程-简洁版
    Linux更改主机名的三种方法
    ubuntu grub引导win10
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4898634.html
Copyright © 2020-2023  润新知