实验八:SQLite数据库操作
实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年5月6日 |
||||||||||||||||||||||||||||||||
实验项目名称 |
SQLite数据库操作 |
实验地点 |
S3010 |
||||||||||||||||||||||||||||||||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
4 |
||||||||||||||||||||||||||||||||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||||||||||||||||||||||||||||||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||||||||||||||||||||||||||||||||
三、实验内容及步骤 |
|||||||||||||||||||||||||||||||||||
(1) 确定数据库的数据结构。 (2) 新建工程,修改布局文件,定义字符串资源。 (3) 开发布局文件activity_main.xml用于显示联系人列表。 (4) layout目录下新建一个detail.xml,用于显示联系人详细信息。 (5) 开发数据库辅助类MyOpenHelper类 (6) DetailActivity端开发实现数据库增加、删除、修改记录等操作 (7) 新建Activity名为DetailActivity.java,实现联系人详细信息显示功能。
|
|||||||||||||||||||||||||||||||||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||||||||||||||||||||||||||||||||
代码: MyOpenHelper public class MyOpenHelper extends SQLiteOpenHelper{ public static final String CREAT_CONTACTS="create table contactTable(id integer primary key autoincrement,name,email,comp,post,addr,phone,mobile)"; // +"id integer primary key autoincrement," // +"name,pwd"); private Context mycContext; public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub mycContext=context; } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub arg0.execSQL(CREAT_CONTACTS); Toast.makeText(mycContext, "数据库创建成功", Toast.LENGTH_LONG).show(); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } public Cursor query(String string, String[] args) { // TODO Auto-generated method stub SQLiteDatabase db=this.getReadableDatabase(); Cursor cursor=db.rawQuery(string, args); return cursor; } }
DetailActivity public class DetailActivity extends Activity { MyOpenHelper dbhelper; EditText editname; EditText editemail; EditText editcomp; EditText editpost; EditText editaddr; EditText editphone; Bundle bundle; EditText editmobile; ImageButton imbutsave; private String selectname; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); editname=(EditText)findViewById(R.id.editname); editemail=(EditText)findViewById(R.id.editemail); editcomp=(EditText)findViewById(R.id.editcomp); editpost=(EditText)findViewById(R.id.editpost); editaddr=(EditText)findViewById(R.id.editaddr); editphone=(EditText)findViewById(R.id.editphone); editmobile=(EditText)findViewById(R.id.editmobile); imbutsave=(ImageButton)findViewById(R.id.imageButtonsave); bundle=getIntent().getExtras(); dbhelper=new MyOpenHelper(DetailActivity.this, "mycontacts.db",null,1); final SQLiteDatabase db=dbhelper.getReadableDatabase(); if(!bundle.getString("name").equals("")) { selectname=bundle.getString("name"); Cursor cursor = db.rawQuery( "select * from contactTable where name=?",new String[]{selectname}); cursor.moveToFirst(); editname.setText(cursor.getString(cursor.getColumnIndex("name"))); editemail.setText(cursor.getString(cursor.getColumnIndex("email"))); editcomp.setText(cursor.getString(cursor.getColumnIndex("comp"))); editpost.setText(cursor.getString(cursor.getColumnIndex("post"))); editaddr.setText(cursor.getString(cursor.getColumnIndex("addr"))); editphone.setText(cursor.getString(cursor.getColumnIndex("phone"))); editmobile.setText(cursor.getString(cursor.getColumnIndex("mobile"))); } registerForContextMenu(editname); imbutsave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dbhelper=new MyOpenHelper(DetailActivity.this, "mycontacts.db",null,1); SQLiteDatabase db=dbhelper.getReadableDatabase(); ContentValues values = new ContentValues(); String nameString=editname.getText().toString(); String emailString=editemail.getText().toString(); String compString=editcomp.getText().toString(); String postString=editpost.getText().toString(); String addrString=editaddr.getText().toString(); String phoneString=editphone.getText().toString(); String mobileString=editmobile.getText().toString(); if (!nameString.equals("")) { Cursor cursor=db.rawQuery("select * from contactTable where name=?", new String[]{nameString}); cursor.moveToFirst(); values.put("name",nameString); values.put("email", emailString); values.put("comp", compString); values.put("post", postString); values.put("addr", addrString); values.put("phone", phoneString); values.put("mobile", mobileString); if (cursor.getCount()<=0) { db.insert("contactTable", null, values); values.clear(); Toast.makeText(DetailActivity.this, "联系人"+nameString+"成功保存", Toast.LENGTH_LONG).show(); db.delete("contactTable", "name=?", new String[]{selectname}); Intent it=new Intent(); it.setClass(DetailActivity.this, MainActivity.class); startActivity(it); finish(); }else if (cursor.getCount()==1 && cursor.getString(cursor.getColumnIndex("name")).equals(selectname)) { db.update("contactTable", values, "name=?", new String[]{selectname}); values.clear(); Toast.makeText(DetailActivity.this, "联系人"+nameString+"成功更新", Toast.LENGTH_LONG).show(); Intent it=new Intent(); it.setClass(DetailActivity.this, MainActivity.class); startActivity(it); finish(); }else { Toast.makeText(DetailActivity.this, "姓名已存在", Toast.LENGTH_LONG).show(); } }else { Toast.makeText(DetailActivity.this, "姓名不能为空", Toast.LENGTH_LONG).show(); } } }); } }
main public class MainActivity extends Activity { final int rowcount=0; ListView lv; ImageButton ibutadd; SQLiteDatabase db; MyOpenHelper dbHelper; String[] name=new String[50]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ibutadd=(ImageButton)findViewById(R.id.imageButton1); lv=(ListView)findViewById(R.id.listView1); dbHelper = new MyOpenHelper(MainActivity.this, "mycontacts.db", null, 1); final SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery( "select name from contactTable", null); ibutadd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent it=new Intent(); Bundle bundle=new Bundle(); bundle.putString("name", ""); it.putExtras(bundle); it.setClass(MainActivity.this, DetailActivity.class); startActivity(it); } }); final ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();/*在数组中存放数据*/ cursor.moveToFirst(); for(int i=0;i<cursor.getCount();i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("name",cursor.getString(cursor.getColumnIndex("name"))); name[i]=cursor.getString(cursor.getColumnIndex("name")); listItem.add(map); if(i==cursor.getCount()){ break; } cursor.moveToNext(); } SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,//需要绑定的数据 R.layout.line,//每一行的布局 //动态数组中的数据源的键对应到定义布局的View中 new String[] {"name"}, new int[] {R.id.tv1}); lv.setAdapter(mSimpleAdapter);//为ListView绑定适配器 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent it=new Intent(MainActivity.this, DetailActivity.class); Bundle bundle=new Bundle(); bundle.putString("name", name[arg2]); it.putExtras(bundle); startActivity(it); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
|