Android实验报告-数据库接口_第1页
Android实验报告-数据库接口_第2页
Android实验报告-数据库接口_第3页
Android实验报告-数据库接口_第4页
Android实验报告-数据库接口_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据库接口实验实验目的:本实验的目的是使学生深入了解 Android数据存储和访问的方法,利用SQLite进行数据存储及访问。通过实验,掌握DatePicker和TimePicker的使方法。实验要求:设计程序实现一个日期信息记录数据库软件,如下图所示SQLite实现数据库的建立、数据表中的信息的删DatePickerTimePicker日期和时间信息写入到数据库中。程序源码(可付纸(包括XML和Java文件:mcy.javapackagecn.mcy;importjava.util.Calendar;importandroid.app.Activity;importandroid.content.ContentValues;importandroid.database.Cursor;importandroid.database.SQLException;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.DatePicker;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.TimePicker;importandroid.widget.Toast;publicclassmcyextendsActivity{/**Calledwhentheactivityisfirstcreated.*/privateEditTextedtEntry,edtIDEntry;privateButtonbtnAdd,btnShowAll,btnClearShow,btnDeleteAll,btnIDDelete,btnIDSeach,btnIDRefresh;privateTextViewtvSShow;privateintmYear,mMonth,mDay,mHour,mMinute;privateTimePickertp;privateDatePickerdp;privatestaticfinalStringDB_FILE="students.db",DB_TABLE="students";privateSQLiteDatabasemStuDbRW;@OverrideprotectedvoidonDestroy(){//TODOAuto-generatedmethodstubsuper.onDestroy();mStuDbRW.close();}@OverridepublicvoidonCreate(BundlesavedInstanceState){n=w,lfriDbHp.sCreateTableCommand="CREATETABLE"+DB_TABLE+"("+"_idINTEGERPRIMARYKEY,"+"idTEXTNOTNULL,"+"dateTEXT,"+"timeTEXT);";mStuDbRW=friDbHp.getWritableDatabase(); 生成一个数据库mStuDbRWwyl)w)l)e)h)h))rc=er=Rh=Hy=Hr=Ye=Edp.init(mYear,mMonth,mDay,newDatePicker.OnDateChangedListener(){publicvoidonDateChanged(DatePickerview,intyear,intmonthOfYear,intdayOfMonth){mYear=year;mMonth=monthOfYear;mDay=dayOfMonth;"+.f"+String.valueOf(mDay)+""+String.valueOf(mHour)+""+String.valueOf(mMinute));}});w{publicvoidonTimeChanged(TimePickerview,inthourOfDay,intminute){mHour=hourOfDay;mMinute=minute;"String.valueOf(mMonth)+""+String.valueOf(mDay)+""+String.valueOf(mHour)+""+String.valueOf(mMinute));}});wpublicvoidonClick(Viewview){ContentValuesnewRow=newContentValues();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.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){Cursorc=mStuDbRW.query(true,DB_TABLE,newString[]{"id","date","time"},null,null,null,null,null,null);return;

if(c==null)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.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){tvSShow.setText("");} });btnDeleteAll.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){try{mStuDbRW.delete(DB_TABLE,"_id>?",newString[]{"0"}); }catch(SQLExceptione){ }} });btnIDDelete.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){try{mStuDbRW.delete(DB_TABLE,"_id=?",newString[]{edtIDEntry.getText().toString()}); }catch(SQLExceptione){ }} });btnIDSeach.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){Cursorc=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.setOnClickListenenre(wOnClickListener(){publicvoidonClick(Viewview){try{mStuDbRW.delete(DB_TABLE,"_id=?",newString[]{edtIDEntry.getText().toString()}); }catch(SQLExceptione){ }ContentValuesnewRow=newContentValues();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.javapackagecn.mcy;importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteDatabase.CursorFactory;importandroid.database.sqlite.SQLiteOpenHelper;mcy11publicclass extendsSQLiteOpenHelper{mcy11publicStringsCreateTableCommand;publicmcy11(Contextcontext,Stringname,CursorFactoryfactory,intversion){super(context,name,factory,version);//TODOAuto-generatedconstructorstubsCreateTableCommand="";}@OverridepublicvoidonCreate(SQLiteDatabasedb){//TODOAuto-generatedmethodstubif(sCreateTableCommand==null)return;db.execSQL(sCreateTableCommand);}@OverridepublicvoidonUpgrade(SQLiteDatabasearg0,intarg1,intarg2){//TODOAuto-generatedmethodstub}}main.xml<?xmlversion="1.0"encoding="utf8"?><ScrollViewxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"><LinearLayout>wandroid:layout_height="wrap_content"></TextView>t"=android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayout>nandroid:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:text="全部显示"android:id="@+id/btnShowAll"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:text="清除显示"android:id="@+id/btnClearShow"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:text="全部删除"android:id="@+id/btnDeleteAll"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout><LinearLayout>w"android:layout_height="wrap_content"></TextView><EditTextandroid:id="@+id/edtIDEntry"android:layout_width="100dp"android:layout_height="wrap_content"></EditText>nDandroid:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:text="ID查询"andr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论