您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > Android数据库实验
实验报告实验项目:Android数据库实验一、实验目的1、理解Android中常用的数据存储方式及各自特点;2、掌握Sqlite数据库的相关操作;3、掌握编程访问Sqlite数据库的方法;二、实验原理1、数据存储方式:SharedPreferences、文件、数据库等;2、Sqlite数据库访问;三、实验步骤、数据记录及处理1、手动操作Sqlite数据库;2、编程访问Sqlite数据库,实验用户登录、注册等基本功能;1.定义数据库MySQLiteOpenHelper继承父类SQLiteOpenHelper写基本操作要用的方法并建立相关的类。相关代码如下:MySQLiteOpenHelper.java:publicclassMySQLiteOpenHelperextendsSQLiteOpenHelper{publicstaticfinalStringDATABASE_NAME=S.db;publicstaticfinalStringTABLE_NAME=S_table;publicstaticfinalStringCOL_1=ID;publicstaticfinalStringCOL_2=NAME;publicstaticfinalStringCOL_3=PASSWORD;publicMySQLiteOpenHelper(Contextcontect){super(contect,DATABASE_NAME,null,1);}@OverridepublicvoidonCreate(SQLiteDatabasedb){db.execSQL(CREATETABLE+TABLE_NAME+(IDINTEGERPRIMARYKEYAUTOINCREMENT,NAMETEXT,PASSWORDTEXT));}@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){db.execSQL(DROPTABLEIFEXISTS+TABLE_NAME);}publicIntegerdeleteData(Stringid){SQLiteDatabasedb=this.getWritableDatabase();inti=db.delete(TABLE_NAME,ID=?,newString[]{id});returni;}publicbooleanfind(Useruserpeople){SQLiteDatabasedb=this.getWritableDatabase();User[]user=queryAllData();if(user==null)returnfalse;for(Useri:user){if(i.name.equals(userpeople.name)&&i.password.equals(userpeople.password)){returntrue;}}returnfalse;}publicUser[]queryAllData(){SQLiteDatabasedb=this.getWritableDatabase();Cursorresults=db.query(TABLE_NAME,newString[]{COL_1,COL_2,COL_3},null,null,null,null,null);returnConvertToPeople(results);}privateUser[]ConvertToPeople(Cursorcursor){intresultCounts=cursor.getCount();if(resultCounts==0||!cursor.moveToFirst()){returnnull;}User[]user=newUser[resultCounts];for(inti=0;iresultCounts;i++){user[i]=newUser();user[i].name=cursor.getString(cursor.getColumnIndex(COL_2));user[i].password=cursor.getString(cursor.getColumnIndex(COL_3));cursor.moveToNext();}returnuser;}publicbooleaninsertData(Stringname,Stringpassword){SQLiteDatabasedb=this.getWritableDatabase();//打开数据库ContentValuescontentValues=newContentValues();contentValues.put(COL_2,name);contentValues.put(COL_3,password);longresult=db.insert(TABLE_NAME,null,contentValues);db.close();if(result==-1){returnfalse;}else{returntrue;}}}User.java:publicclassUser{privatestaticfinallongserialVersionUID=1L;publicStringname;publicStringpassword;publicUser(){}publicUser(Stringname,Stringpassword){super();this.name=name;this.password=password;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}@OverridepublicStringtoString(){returnUser[name=+name+,password=+password+];}}用cmd命令查看在注册界面输入的用户名和密码已经被正确的存入数据库中,操作如下图:2.实现登陆注册的验证。验证界面的数据已经存入数据库中,要完成验证我们需要把用户在登陆界面输入的用户名和密码获取到并和数据库中的数据进行比对,结果会有两种情况:1.信息完全匹配,界面下方显示Toast登陆成功;2.信息有至少一条不匹配,界面下方显示Toast登陆失败;相关代码如下:RegisterActivity.java://定义数据库需要控件(INSERT)myDb=newMySQLiteOpenHelper(this);textName=(EditText)findViewById(R.id.ideText3);textPassword=(EditText)findViewById(R.id.ideText4);Buttonbutton4=(Button)findViewById(R.id.button4);button4.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){ClickMe();}});//添加数据入数据库(INSERT)privatevoidClickMe(){Stringnew_name=textName.getText().toString();Stringnew_password=textPassword.getText().toString();Booleanresult=myDb.insertData(new_name,new_password);if(result==true){Toast.makeText(this,DataInsertedSucessfully,Toast.LENGTH_SHORT).show();}else{Toast.makeText(this,DataInsertedFailed,Toast.LENGTH_SHORT).show();}}MainActivity.java://登陆Buttonbutton1=(Button)findViewById(R.id.button1);button1.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){//点击登录按钮_name=(EditText)findViewById(R.id.et1);_password=(EditText)findViewById(R.id.et2);Useruser=newUser(_name.getText().toString(),_password.getText().toString());//Stringlog_name=_name.getText().toString();//Stringlog_password=_password.getText().toString();if(myDb.find(user)==true)Toast.makeText(getApplicationContext(),登录成功,Toast.LENGTH_SHORT).show();elseToast.makeText(getApplicationContext(),登录失败,Toast.LENGTH_SHORT).show();}});//验证登录publicbooleanlogin(Stringusername,Stringpassword){SQLiteDatabasedb=myDb.getWritableDatabase();Stringsql=select*fromuserDatawherename=?andpassword=?;Cursorcursor=db.rawQuery(sql,newString[]{username,password});if(cursor.moveToFirst()){cursor.close();returntrue;}returnfalse;}虚拟机运行效果:
本文标题:Android数据库实验
链接地址:https://www.777doc.com/doc-3065105 .html