数据库增删改查java
Posted 藻类植物
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库增删改查java相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
求.net 连ORACLE数据库 增删改查完整代码 代码 要最简单的
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段