mainActivity
package com.example.a71979.sqlexp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private DBAdapter dbAdepter ;
private EditText nameText;
private EditText classText;
private EditText sidText;
private EditText idText;
private Button addButton=null;
private Button queryAllButton=null;
private Button clearButton=null;
private Button deleteAllButton=null;
private Button queryButton=null;
private Button deleteButton=null;
private Button updateButton=null;
//private TextView labelView;
//private TextView displayView;
private ListView displayView;
private List<String> list;
private ArrayAdapter<String> adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
nameText = (EditText)findViewById(R.id.sName);
classText = (EditText)findViewById(R.id.sClass);
sidText = (EditText)findViewById(R.id.sID);
idText = (EditText)findViewById(R.id.ID);
//labelView = (TextView)findViewById(R.id.label);
//displayView = (TextView)findViewById(R.id.display);
displayView = (ListView)findViewById(R.id.list_view);
addButton = (Button)findViewById(R.id.addNew);
queryAllButton = (Button)findViewById(R.id.showAll);
clearButton = (Button)findViewById(R.id.clearShow);
deleteAllButton = (Button)findViewById(R.id.deleteAll);
queryButton = (Button)findViewById(R.id.queryID);
deleteButton = (Button)findViewById(R.id.deleteID);
updateButton = (Button)findViewById(R.id.updateID);
addButton.setOnClickListener(addButtonListener);
queryAllButton.setOnClickListener(queryAllButtonListener);
clearButton.setOnClickListener(clearButtonListener);
deleteAllButton.setOnClickListener(deleteAllButtonListener);
queryButton.setOnClickListener(queryButtonListener);
deleteButton.setOnClickListener(deleteButtonListener);
updateButton.setOnClickListener(updateButtonListener);
list = new ArrayList<String>();
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list);
displayView.setAdapter(adapter);
registerForContextMenu(displayView);
dbAdepter = new DBAdapter(this);
dbAdepter.open();
}
OnClickListener addButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Student student = new Student();
student.Name = nameText.getText().toString();
student.sClass = classText.getText().toString();
student.sID = sidText.getText().toString();
long colunm = dbAdepter.insert(student);
Student[] students = dbAdepter.queryAllData();
list.removeAll(list);
adapter.notifyDataSetChanged();
if (students == null){
return;
}
for (int i = 0; i< students.length; i++){
adapter.add(students[i].ID+" "+students[i].sID.toString()+" "+students[i].sClass.toString()+" "+students[i].Name.toString());
}
}
};
OnClickListener queryAllButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Student[] students = dbAdepter.queryAllData();
list.removeAll(list);
adapter.notifyDataSetChanged();
if (students == null){
return;
}
for (int i = 0; i< students.length; i++){
adapter.add(students[i].ID+" "+students[i].sID.toString()+" "+students[i].sClass.toString()+" "+students[i].Name.toString());
}
}
};
OnClickListener clearButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
list.removeAll(list);
adapter.notifyDataSetChanged();
}
};
OnClickListener deleteAllButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
dbAdepter.deleteAllData();
list.removeAll(list);
adapter.notifyDataSetChanged();
}
};
OnClickListener queryButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
list.removeAll(list);
adapter.notifyDataSetChanged();
int id = Integer.parseInt(idText.getText().toString());
Student[] students = dbAdepter.queryOneData(id);
if (students == null){
return;
}
for (int i = 0; i< students.length; i++){
adapter.add(students[i].ID+" "+students[i].sID.toString()+" "+students[i].sClass.toString()+" "+students[i].Name.toString());
}
}
};
OnClickListener deleteButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
long id = Integer.parseInt(idText.getText().toString());
long result = dbAdepter.deleteOneData(id);
}
};
OnClickListener updateButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
list.removeAll(list);
adapter.notifyDataSetChanged();
Student student = new Student();
student.Name = nameText.getText().toString();
student.sClass = classText.getText().toString();
student.sID = sidText.getText().toString();
long id = Integer.parseInt(idText.getText().toString());
long count = dbAdepter.updateOneData(id, student);
Student[] students = dbAdepter.queryOneData(id);
if (students == null){
return;
}
for (int i = 0; i< students.length; i++){
adapter.add(students[i].ID+" "+students[i].sID.toString()+" "+students[i].sClass.toString()+" "+students[i].Name.toString());
}
}
};
}
DBAdapter
package com.example.a71979.sqlexp;
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 = "student.db";
private static final String DB_TABLE = "studentinfo";
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_sClass = "sClass";
public static final String KEY_SID = "sID";
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(Student student) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, student.Name);
newValues.put(KEY_sClass, student.sClass);
newValues.put(KEY_SID, student.sID);
return db.insert(DB_TABLE, null, newValues);
}
public Student[] queryAllData() {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_sClass, KEY_SID},
null, null, null, null, null);
return ConvertToPeople(results);
}
public Student[] queryOneData(long id) {
Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_sClass, KEY_SID},
KEY_ID + "=" + id, null, null, null, null);
return ConvertToPeople(results);
}
private Student[] ConvertToPeople(Cursor cursor){
int resultCounts = cursor.getCount();
if (resultCounts == 0 || !cursor.moveToFirst()){
return null;
}
Student[] students = new Student[resultCounts];
for (int i = 0 ; i<resultCounts; i++){
students[i] = new Student();
students[i].ID = cursor.getInt(0);
students[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
students[i].sClass = cursor.getString(cursor.getColumnIndex(KEY_sClass));
students[i].sID = cursor.getString(cursor.getColumnIndex(KEY_SID));
cursor.moveToNext();
}
return students;
}
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 , Student student){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_NAME, student.Name);
updateValues.put(KEY_sClass, student.sClass);
updateValues.put(KEY_SID, student.sID);
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_sClass+ " integer," + KEY_SID + " float);";
@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);
}
}
}
student
package com.example.a71979.sqlexp;
public class Student {
public int ID = -1;
public String Name;
public String sClass;
public String sID;
@Override
public String toString(){
String result = "";
result += "ID��" + this.ID + "��";
result += "������" + this.Name + "��";
result += "���䣺" + this.sClass + "�� ";
result += "��ߣ�" + this.sID + "��";
return result;
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="姓名:"
android:textSize="26dp" />
<EditText
android:id="@+id/sName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="8"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="班级:"
android:textSize="26dp" />
<EditText
android:id="@+id/sClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="8"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="学号:"
android:textSize="26dp" />
<EditText
android:id="@+id/sID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="8"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/addNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据" />
<Button
android:id="@+id/showAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全部显示" />
<Button
android:id="@+id/clearShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除显示" />
<Button
android:id="@+id/deleteAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全部删除" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID:"
android:textSize="20dp"/>
<EditText
android:id="@+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/queryID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ID查询" />
<Button
android:id="@+id/deleteID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ID删除" />
<Button
android:id="@+id/updateID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ID更新" />
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list_view"/>
</LinearLayout>
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a71979.sqlexp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>