标签: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()); } } }
标签:get writable name android port set contex exec his
原文地址:https://www.cnblogs.com/xrj-/p/12359461.html