码迷,mamicode.com
首页 > 数据库 > 详细

数据库增删改查java

时间:2020-02-25 00:24:25      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:get   writable   name   android   port   set   contex   exec   his   

技术图片技术图片

PersonDao

package com.xrj.myapplication.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xrj.myapplication.utils.PersonDBHelper;

public class PersonDao {
    private PersonDBHelper helper;
    public PersonDao(Context context) {
        helper = new PersonDBHelper(context);
    }

    public void add(String name,String number) {
        String addSql = "insert into person (name,number) values (?,?)";
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(addSql,new Object[]{name,number});
    }
}

Person

package com.xrj.myapplication.entity;

public class Person {
    private int id;
    private String name;
    private String number;

    public Person(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}

PersonDaoTest

package com.xrj.myapplication;

import androidx.test.InstrumentationRegistry;

import com.xrj.myapplication.dao.PersonDao;

import org.junit.Before;
import org.junit.Test;

public class PersonDaoTest {
    private PersonDao personDao;
    @Before
    public void init() {
        personDao = new PersonDao(InstrumentationRegistry.getTargetContext());
    }

    @Test
    public void addTest() {
        personDao.add("ccc","333");
    }
}

以上为添加

以下为全部:

PersonDao

package com.xrj.myapplication.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xrj.myapplication.entity.Person;
import com.xrj.myapplication.utils.PersonDBHelper;

import java.util.ArrayList;
import java.util.List;

public class PersonDao {
    private PersonDBHelper helper;
    public PersonDao(Context context) {
        helper = new PersonDBHelper(context);
    }

    public void add(String name,String number) {
        String addSql = "insert into person (name,number) values (?,?)";
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(addSql,new Object[]{name,number});
        db.close();
    }

    public void delete(String name) {
        String deleteSql = "delete from person where name = ?";
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(deleteSql,new Object[]{name});
        db.close();
    }

    public void update(String name,String number) {
        String updateSql = "update person set number = ? where name = ?";
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(updateSql,new Object[]{name,number});
        db.close();
    }

    public Person findone(String name) {
        String sql = "select * from person where name = ?";
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{name});
        if(cursor.moveToNext()) {
            String personname = cursor.getString(1);
            String personnumber = cursor.getString(2);
            Person person = new Person(personname,personnumber);
            return person;
        }
        return null;
    }

    public List<Person> findAll() {
        String sql = "select * from person";
       SQLiteDatabase db = helper.getReadableDatabase();
       Cursor cursor = db.rawQuery(sql,null);
       List<Person> personList = new ArrayList<Person>();
       while (cursor.moveToNext()) {
           String personname = cursor.getString(1);
           String personnumber = cursor.getString(2);
           Person person = new Person(personname,personnumber);
           personList.add(person);
       }
       return personList;
    }
}

PersonDaoTest

package com.xrj.myapplication;

import android.util.Log;

import androidx.test.InstrumentationRegistry;

import com.xrj.myapplication.dao.PersonDao;
import com.xrj.myapplication.entity.Person;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class PersonDaoTest {
    private PersonDao personDao;
    @Before
    public void init() {
        personDao = new PersonDao(InstrumentationRegistry.getTargetContext());
    }

    @Test
    public void addTest() {
        personDao.add("ccc","333");
        personDao.add("bbb","222");
    }

    @Test
    public void deleteTest() {
        personDao.delete("ccc");
    }

    public void updateTest() {
        personDao.update("ccc","456");
    }

    @Test
    public void findOneTest() {
        Person person = personDao.findone("ccc");
        Log.i("name",person.getName()+" "+person.getNumber());
    }

    @Test
    public void findAllTest() {
        List<Person> personList = personDao.findAll();
        for(Person p:personList) {
            Log.i("name",p.getName()+" "+p.getNumber());
        }
    }
}

 

数据库增删改查java

标签:get   writable   name   android   port   set   contex   exec   his   

原文地址:https://www.cnblogs.com/xrj-/p/12359461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!