当应用程序需要处理的数据量比较大时,为了方便对数据的管理,通常需要使用数据库来存储数据。
package com.example.sample7_5;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private SQLiteDatabase sld;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitViews();
}
private void InitViews() {
Button b = (Button) findViewById(R.id.Button01);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createOrOpenDatabase();//创建数据库
}
});
b = (Button) findViewById(R.id.Button02);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeDatabase();//关闭数据库
}
});
b = (Button) findViewById(R.id.Button03);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();//添加记录
}
});
b = (Button) findViewById(R.id.Button04);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delete();//删除记录
}
});
b = (Button) findViewById(R.id.Button05);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
update();//更新记录
}
});
b = (Button) findViewById(R.id.Button06);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
query();//查询记录
}
});
}
protected void query() {
try {
String sql = "select * from student where sage>?";
Cursor cur = sld.rawQuery(sql, new String[]{"20"});
appendMessage("学号\t\t姓名\t\t年龄\t班级");
while(cur.moveToNext()){
String sno = cur.getString(0);
String sname = cur.getString(1);
int sage = cur.getInt(2);
String sclass = cur.getString(3);
appendMessage(sno+"\t"+sname+"\t\t"+sage+"\t"+sclass);
}
cur.close();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "数据库错误:"+e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
protected void update() {
try {
String sql = "update student set stuname='李勇'";
sld.execSQL(sql);
appendMessage("成功更新记录");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "数据库错误:"+e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
protected void delete() {
try {
String sql = "delete from student;";
sld.execSQL(sql);
appendMessage("成功删除所有记录");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "数据库错误:"+e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
protected void insert() {
try {
String sql = "insert into student values('10001','王强', 23, '97002')";
sld.execSQL(sql);
appendMessage("成功插入一条记录");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "数据库错误:"+e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
protected void closeDatabase() {
sld.close();
}
protected void createOrOpenDatabase() {
try {
sld = SQLiteDatabase.openDatabase("/data/data/com.example.sample7_5/mydb", null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY);
appendMessage("数据库已经成功打开");
String sql = "create table if not exists student(sno char(5), stuname varchar(20), sage integer, sclass char(5))";
sld.execSQL(sql);
appendMessage("student 已经成功创建!");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "数据库错误:"+e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
private void appendMessage(String message) {
EditText et = (EditText) findViewById(R.id.EditText01);
et.append(message+"\n");;
}
}
如果想借用代码,需要改动的就是创建数据库的位置,即改动下面的代码即可。
sld = SQLiteDatabase.openDatabase("/data/data/com.example.sample7_5/mydb", null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY);
下载链接: