Android–SQLite(增,刪,改,查)操作執行個體代碼

來源:互聯網
上載者:User

需要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;

/**
* 增刪改查
* @author ZLQ
*
*/
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;

/**
* 助手類
* @author ZLQ
*
*/
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);
}

/**
* 增,用insert向資料庫中插入資料
*/
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);
}

/**
* 刪,通過id刪除資料
*/
public void delete(int id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete(DBOpneHelper.STUDENT_TABLE, DBOpneHelper._ID + "=?", new String[]{String.valueOf(id)});
}

/**
* 改,修改指定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;
}

/**
* 查詢指定id的資料
*/
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());
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.