People.java 文件
package edu.hrbeu.SQLiteDemo;
public class People {
public int ID = -1;
public String Name;
public String sex;
public String classNo;
@Override
public String toString(){
String result = "";
result += "ID:" + this.ID + ",";
result += "姓名:" + this.Name + ",";
result += "性别:" + this.sex + ", ";
result += "班级:" + this.classNo + ",";
return result;
}
}
数据库建立及更新等操作 java文件
package edu.hrbeu.SQLiteDemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBAdapter {
private static final String DB_NAME = "people.db";
private static final String DB_TABLE = "peopleinfo";
private static final int DB_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SEX = "sex";
public static final String KEY_CLASSNO = "classNo";
private SQLiteDatabase db;
private final Context context;
private DBOpenHelper dbOpenHelper;
public DBAdapter(Context _context) {
context = _context;
}
/** Close the database */
public void close() {
if (db != null){
db.close();
db = null;
}
}
/** Open the database */
public void open() throws SQLiteException {
dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
try {
db = dbOpenHelper.getWritableDatabase();
}
catch (SQLiteException ex) {
db = dbOpenHelper.getReadableDatabase();
}
}
public long insert(People people) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, people.Name);
newValues.put(KEY_SEX, people.sex);
newValues.put(KEY_CLASSNO, people.classNo);
return db.insert(DB_TABLE, null, newValues);
}
public People[] queryAllData() {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_SEX, KEY_CLASSNO},
null, null, null, null, null);
return ConvertToPeople(results);
}
public People[] queryOneData(long id) {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_SEX, KEY_CLASSNO},
KEY_ID + "=" + id, null, null, null, null);
return ConvertToPeople(results);
}
private People[] ConvertToPeople(Cursor cursor){
int resultCounts = cursor.getCount();
if (resultCounts == 0 || !cursor.moveToFirst()){
return null;
}
People[] peoples = new People[resultCounts];
for (int i = 0 ; i<resultCounts; i++){
peoples[i] = new People();
peoples[i].ID = cursor.getInt(0);
peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
peoples[i].sex = cursor.getString(cursor.getColumnIndex(KEY_SEX));
peoples[i].classNo = cursor.getString(cursor.getColumnIndex(KEY_CLASSNO));
cursor.moveToNext();
}
return peoples;
}
public long deleteAllData() {
return db.delete(DB_TABLE, null, null);
}
public long deleteOneData(long id) {
return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
}
public long updateOneData(long id , People people){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_NAME, people.Name);
updateValues.put(KEY_SEX, people.sex);
updateValues.put(KEY_CLASSNO, people.classNo);
return db.update(DB_TABLE, updateValues, KEY_ID + "=" + id, null);
}
/** 静态Helper类,用于建立、更新和打开数据库*/
private static class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
private static final String DB_CREATE = "create table " +
DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
KEY_NAME+ " text not null, " + KEY_SEX+ " text not null," + KEY_CLASSNO + " text not null);";
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
_db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(_db);
}
}
}
安卓界面相应事件文件 java文件
package edu.hrbeu.SQLiteDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SQLiteDemoActivity extends Activity {
/** Called when the activity is first created. */
private DBAdapter dbAdepter ;
private EditText nameText;
private EditText ageText;
private EditText heightText;
private EditText idEntry;
private TextView labelView;
private TextView displayView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText)findViewById(R.id.name);
ageText = (EditText)findViewById(R.id.age);
heightText = (EditText)findViewById(R.id.height);
idEntry = (EditText)findViewById(R.id.id_entry);
labelView = (TextView)findViewById(R.id.label);
displayView = (TextView)findViewById(R.id.display);
Button addButton = (Button)findViewById(R.id.add);
Button queryAllButton = (Button)findViewById(R.id.query_all);
Button clearButton = (Button)findViewById(R.id.clear);
Button deleteAllButton = (Button)findViewById(R.id.delete_all);
Button queryButton = (Button)findViewById(R.id.query);
Button deleteButton = (Button)findViewById(R.id.delete);
Button updateButton = (Button)findViewById(R.id.update);
addButton.setOnClickListener(addButtonListener);
queryAllButton.setOnClickListener(queryAllButtonListener);
clearButton.setOnClickListener(clearButtonListener);
deleteAllButton.setOnClickListener(deleteAllButtonListener);
queryButton.setOnClickListener(queryButtonListener);
deleteButton.setOnClickListener(deleteButtonListener);
updateButton.setOnClickListener(updateButtonListener);
dbAdepter = new DBAdapter(this);
dbAdepter.open();
}
OnClickListener addButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
People people = new People();
people.Name = nameText.getText().toString();
people.sex = (ageText.getText().toString());
people.classNo = (heightText.getText().toString());
long colunm = dbAdepter.insert(people);
if (colunm == -1 ){
labelView.setText("添加过程错误!");
} else {
labelView.setText("成功添加数据,ID:"+String.valueOf(colunm));
}
}
};
OnClickListener queryAllButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
People[] peoples = dbAdepter.queryAllData();
if (peoples == null){
labelView.setText("数据库中没有数据");
return;
}
labelView.setText("数据库:");
String msg = "";
for (int i = 0 ; i<peoples.length; i++){
msg += peoples[i].toString()+"\n";
}
displayView.setText(msg);
}
};
OnClickListener clearButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
displayView.setText("");
}
};
OnClickListener deleteAllButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
dbAdepter.deleteAllData();
String msg = "数据全部删除";
labelView.setText(msg);
}
};
OnClickListener queryButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
int id = Integer.parseInt(idEntry.getText().toString());
People[] peoples = dbAdepter.queryOneData(id);
if (peoples == null){
labelView.setText("数据库中没有ID为"+String.valueOf(id)+"的数据");
return;
}
labelView.setText("数据库:");
displayView.setText(peoples[0].toString());
}
};
OnClickListener deleteButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
long id = Integer.parseInt(idEntry.getText().toString());
long result = dbAdepter.deleteOneData(id);
String msg = "删除ID为"+idEntry.getText().toString()+"的数据" + (result>0?"成功":"失败");
labelView.setText(msg);
}
};
OnClickListener updateButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
People people = new People();
people.Name = nameText.getText().toString();
people.sex = (ageText.getText().toString());
people.classNo = (heightText.getText().toString());
long id = Integer.parseInt(idEntry.getText().toString());
long count = dbAdepter.updateOneData(id, people);
if (count == -1 ){
labelView.setText("更新错误!");
} else {
labelView.setText("更新成功,更新数据"+String.valueOf(count)+"条");
}
}
};
}
布局文件 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText android:id="@+id/name"
android:text=""
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip" >
</EditText>
<TextView android:id="@+id/name_label"
android:text="姓名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/name"
android:layout_alignBaseline="@+id/name">
</TextView>
<EditText android:id="@+id/age"
android:text=""
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_below="@id/name"
>
</EditText>
<TextView android:id="@+id/age_label"
android:text="性别:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/age"
android:layout_alignBaseline="@+id/age" >
</TextView>
<EditText android:id="@+id/height"
android:layout_width="280dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_below="@id/age"
>
</EditText>
<TextView android:id="@+id/height_label"
android:text="班级:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toRightOf="@id/height"
android:layout_alignBaseline="@+id/height">
</TextView>
</RelativeLayout>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/add"
android:text="添加数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "5dip"
android:layout_weight="1">
</Button>
<Button android:id="@+id/query_all"
android:text="全部显示"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "5dip"
android:layout_weight="1">
</Button>
<Button android:id="@+id/clear"
android:text="清除显示"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "5dip"
android:layout_weight="1">
</Button>
<Button android:id="@+id/delete_all"
android:text="全部删除"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "5dip"
android:layout_weight="1">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="ID:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "3dip">
</TextView>
<EditText android:id="@+id/id_entry"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:padding = "3dip"
android:layout_weight="1">
</EditText>
<Button android:id="@+id/delete"
android:text="ID删除"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:padding = "3dip"
android:layout_weight="1">
</Button>
<Button android:id="@+id/query"
android:text="ID查询"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:padding = "3dip"
android:layout_weight="1">
</Button>
<Button android:id="@+id/update"
android:text="ID更新"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:padding = "3dip"
android:layout_weight="1">
</Button>
</LinearLayout>
<TextView android:id="@+id/label"
android:text="查询结果:"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/display"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ScrollView>
</LinearLayout>