数据库接口实验实验目的:本实验的目的是使学生深入了解Android数据存储和访问的方法,利用SQLite进行数据存储及访问。
通过实验,掌握DatePicker和TimePicker的使用方法。
实验要求:设计程序实现一个日期信息记录数据库软件,如下图所示设计界面,如图所示,利用SQLite实现数据库的建立、数据表中的信息的删改、查等操作。
使用DatePicker和TimePicker来动态选择日期和时间,将设置的日期和时间信息写入到数据库中。
程序源码(可付纸)(包括XML和Java文件):mcy.javapackage cn.mcy;import java.util.Calendar;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.EditText;import android.widget.TextView;import android.widget.TimePicker;import android.widget.Toast;public class mcy extends Activity {/** Called when the activity is first created. */private EditText edtEntry,edtIDEntry;private ButtonbtnAdd,btnShowAll,btnClearShow,btnDeleteAll,btnIDDelete,btnIDSeach,btnIDRefr esh;private TextView tvSShow;private int mYear,mMonth,mDay,mHour,mMinute;private TimePicker tp;private DatePicker dp;private static final String DB_FILE="students.db",DB_TABLE="students";private SQLiteDatabase mStuDbRW;@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();mStuDbRW.close();}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);mcy11 friDbHp=new mcy11(getApplicationContext(),DB_FILE,null,1); friDbHp.sCreateTableCommand="CREATE TABLE "+DB_TABLE+"("+"_id INTEGER PRIMARY KEY,"+"id TEXT NOT NULL,"+"date TEXT,"+"time TEXT);";mStuDbRW=friDbHp.getWritableDatabase(); //生成一个数据库mStuDbRWtvSShow=(TextView) findViewById(SShow);edtEntry=(EditText) findViewById(R.id.edtEntry);edtIDEntry=(EditText) findViewById(R.id.edtIDEntry);btnAdd=(Button) findViewById(R.id.btnAdd);btnShowAll=(Button) findViewById(R.id.btnShowAll);btnClearShow=(Button) findViewById(R.id.btnClearShow);btnDeleteAll=(Button) findViewById(R.id.btnDeleteAll);btnIDDelete=(Button) findViewById(R.id.btnIDDelete);btnIDSeach=(Button) findViewById(R.id.btnIDSeach);btnIDRefresh=(Button) findViewById(R.id.btnIDRefresh);tp=(TimePicker) findViewById(R.id.TimePicker01);dp=(DatePicker) findViewById(R.id.DatePicker01);tp.setIs24HourView(true);Calendar c = Calendar.getInstance();mYear = c.get(Calendar.YEAR);mMonth = c.get(Calendar.MONTH);mDay = c.get(Calendar.DAY_OF_MONTH);mHour = c.get(Calendar.HOUR_OF_DAY);mMinute = c.get(Calendar.MINUTE);dp.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener(){public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth){mYear = year;mMonth = monthOfYear;mDay = dayOfMonth;edtEntry.setText(String.valueOf(mYear)+" "+ String.valueOf(mMonth)+" "+String.valueOf(mDay)+" "+String.valueOf(mHour)+" "+String.valueOf(mMinute)); }});tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){public void onTimeChanged(TimePicker view, int hourOfDay,int minute){mHour = hourOfDay;mMinute = minute;edtEntry.setText(String.valueOf(mYear)+" "+String.valueOf(mMonth)+" "+String.valueOf(mDay)+" "+String.valueOf(mHour)+" "+String.valueOf(mMinute));}});btnAdd.setOnClickListener(new OnClickListener(){public void onClick(View view){ContentValues newRow =new ContentValues();newRow.put("id", edtIDEntry.getText().toString());newRow.put("date",String.valueOf(mYear)+String.valueOf(mMonth)+String.valueOf(mDay));newRow.put("time",String.valueOf(mHour)+String.valueOf(mMinute));mStuDbRW.insert(DB_TABLE, null, newRow);} });btnShowAll.setOnClickListener(new OnClickListener(){ public void onClick(View view){Cursor c=mStuDbRW.query(true, DB_TABLE,newString[]{"id","date","time"}, null, null, null, null,null,null);if(c==null)return;if(c.getCount()==0){tvSShow.setText("");Toast.makeText(mcy.this, "没有资料",Toast.LENGTH_SHORT).show();}else{c.moveToFirst();tvSShow.setText(c.getString(0)+" "+c.getString(1)+""+c.getString(2));while(c.moveToNext())tvSShow.append("\n"+c.getString(0)+" "+c.getString(1)+" "+c.getString(2));}}} );btnClearShow.setOnClickListener(new OnClickListener(){ public void onClick(View view){tvSShow.setText("");} });btnDeleteAll.setOnClickListener(new OnClickListener(){ public void onClick(View view){try {mStuDbRW.delete(DB_TABLE, "_id>?", new String[]{"0"}); } catch (SQLException e) { }} });btnIDDelete.setOnClickListener(new OnClickListener(){ public void onClick(View view){try {mStuDbRW.delete(DB_TABLE, "_id=?", newString[]{edtIDEntry.getText().toString()}); }catch (SQLException e) { }} });btnIDSeach.setOnClickListener(new OnClickListener(){public void onClick(View view){Cursor c=null;c=mStuDbRW.query(true, DB_TABLE, newString[]{"id","date","time"},"id="+"\""+edtIDEntry.getText().toString()+"\"",null, null,null, null,null);c.moveToFirst();tvSShow.setText(c.getString(0)+" "+c.getString(1)+""+c.getString(2));} });btnIDRefresh.setOnClickListener(new OnClickListener(){ public void onClick(View view){try {mStuDbRW.delete(DB_TABLE, "_id=?", newString[]{edtIDEntry.getText().toString()}); }catch (SQLException e) { }ContentValues newRow =new ContentValues();newRow.put("id", edtIDEntry.getText().toString());newRow.put("date",String.valueOf(mYear)+String.valueOf(mMonth)+String.valueOf(mDay));newRow.put("time",String.valueOf(mHour)+String.valueOf(mMinute));mStuDbRW.insert(DB_TABLE, null, newRow);} });}}Mcy11.javapackage cn.mcy;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class mcy11 extends SQLiteOpenHelper {public String sCreateTableCommand;public mcy11(Context context, String name, CursorFactory factory, int version) {super(context, name, factory, version);// TODO Auto-generated constructor stubsCreateTableCommand="";}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubif(sCreateTableCommand==null)return;db.execSQL(sCreateTableCommand);}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub}}main.xml<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:text="日期:" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView><EditText android:text="" android:id="@+id/edtEntry"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:text="添加数据" android:id="@+id/btnAdd"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Button android:text="全部显示" android:id="@+id/btnShowAll"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Button android:text="清除显示" android:id="@+id/btnClearShow"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Button android:text="全部删除" android:id="@+id/btnDeleteAll"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextView android:text="ID:" android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView><EditText android:text="" android:id="@+id/edtIDEntry"android:layout_width="100dp" android:layout_height="wrap_content"></EditText> <Button android:text="ID删除" android:id="@+id/btnIDDelete"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Button android:text="ID查询" android:id="@+id/btnIDSeach"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Button android:text="ID更新" android:id="@+id/btnIDRefresh"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout><TimePicker android:id="@+id/TimePicker01"android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker><DatePicker android:id="@+id/DatePicker01"android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker><TextView android:text="" android:id="@+id/tvSShow"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView></LinearLayout></ScrollView>程序运行结果截图:。