需要5个类: 1.实体类:Person.java 2.抽象类:sqlOperate.java(封装了对数据库的操作) 3.助手类:DBOpenHelper.java(继承SQL
需要5个类:
1.实体类:Person.java
2.抽象类:sqlOperate.java(封装了对数据库的操作)
3.助手类:DBOpenHelper.java(继承SQLiteOpenHelper)
4.实现类:SQLOperateImpl.java(实现抽象类SQLOperate.java)
5.测试类:Test.java(继承AndroidTestCase)
1.Person.java
代码如下:
package com.mrzhu.sqltite;
public class Person {
private int _id;
private String name;
public int getId() {
return _id;
}
public void setId(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + _id + ", name=" + name + "]";
}
public Person() {
super();
}
public Person(int _id, String name) {
super();
this._id = _id;
this.name = name;
}
}
2.SQLOperate.java
代码如下:
package com.mrzhu.sqltite;
import java.util.List;
public interface SQLOperate {
public void add(Person p);
public void delete(int id);
public void updata(Person p);
public List<Person> find();
public Person findById(int id);
}
3.DBOpenHelper.java
代码如下:
package com.mrzhu.sqltite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpneHelper extends SQLiteOpenHelper {
private static final int VERSioN = 1;//版本
private static final String DB_NAME = "people.db";//数据库名
public static final String STUDENT_TABLE = "student";//表名
public static final String _ID = "_id";//表中的列名
public static final String NAME = "name";//表中的列名
//创建数据库语句,STUDENT_TABLE,_ID ,NAME的前后都要加空格
private static final String CREATE_TABLE = "create table " + STUDENT_TABLE + " ( " + _ID + " Integer primary key autoincrement," + NAME + " text)";
public DBOpneHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
//数据库第一次被创建时调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
//版本升级时被调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4.SQLOperateImpl.java
代码如下:
package com.mrzhu.sqltite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class SQLOperateImpl implements SQLOperate{
private DBOpneHelper dbOpenHelper;
public SQLOperateImpl(Context context) {
dbOpenHelper = new DBOpneHelper(context);
}
public void add(Person p) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBOpneHelper._ID, p.getId());
values.put(DBOpneHelper.NAME, p.getName());
db.insert(DBOpneHelper.STUDENT_TABLE, null, values);
}
public void delete(int id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete(DBOpneHelper.STUDENT_TABLE, DBOpneHelper._ID + "=?", new String[]{String.valueOf(id)});
}
public void updata(Person p) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBOpneHelper._ID, p.getId());
values.put(DBOpneHelper.NAME, p.getName());
db.update(DBOpneHelper.STUDENT_TABLE, values, DBOpneHelper._ID + "=?", new String[]{String.valueOf(p.getId())});
}
public List<Person> find() {
List<Person> persons = null;
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query(DBOpneHelper.STUDENT_TABLE, null, null, null, null, null, null);
if(cursor != null){
persons = new ArrayList<Person>();
while(cursor.moveToNext()){
Person person = new Person();
int _id = cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));
String name = cursor.getString(cursor.getColumnIndex(DBOpneHelper.NAME));
person.setId(_id);
person.setName(name);
persons.add(person);
}
}
return persons;
}
public Person findById(int id) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query(DBOpneHelper.STUDENT_TABLE, null, DBOpneHelper._ID + "=?", new String[]{String.valueOf(id)}, null, null, null);
Person person = null;
if(cursor != null && cursor.moveToFirst()){
person = new Person();
int _id = cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));
String name = cursor.getString(cursor.getColumnIndex(DBOpneHelper.NAME));
person.setId(_id);
person.setName(name);
}
return person;
}
}
5.Test.java
在AndroidManifest.xml中的<application></application>外添加
(targetPackage是当前工程的包名)
<instrumentation
android:targetPackage="com.mrzhu.sqltite"
android:name="android.test.InstrumentationTestRunner">
</instrumentation>
在<application></application>中添加<uses-library android:name="android.test.runner"/>
代码如下:
package com.mrzhu.sqltite;
import java.util.List;
import android.test.AndroidTestCase;
import android.util.Log;
public class Test extends AndroidTestCase {
public void testAdd() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = new Person(2, "Peter");
test.add(person);
}
public void testDelete() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
test.delete(1);
}
public void testUpdata() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = new Person(1, "Tom");
test.updata(person);
}
public void testFind() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
List<Person> persons = test.find();
for (Person person : persons) {
Log.i("System.out", person.toString());
}
}
public void testFindById() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = test.findById(2);
Log.i("System.out", person.toString());
}
}
--结束END--
本文标题: Android--SQLite(增,删,改,查)操作实例代码
本文链接: https://lsjlt.com/news/27980.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0