Android数据库使用文档Word文档格式.docx

上传人:b****6 文档编号:8658114 上传时间:2023-05-12 格式:DOCX 页数:13 大小:38.42KB
下载 相关 举报
Android数据库使用文档Word文档格式.docx_第1页
第1页 / 共13页
Android数据库使用文档Word文档格式.docx_第2页
第2页 / 共13页
Android数据库使用文档Word文档格式.docx_第3页
第3页 / 共13页
Android数据库使用文档Word文档格式.docx_第4页
第4页 / 共13页
Android数据库使用文档Word文档格式.docx_第5页
第5页 / 共13页
Android数据库使用文档Word文档格式.docx_第6页
第6页 / 共13页
Android数据库使用文档Word文档格式.docx_第7页
第7页 / 共13页
Android数据库使用文档Word文档格式.docx_第8页
第8页 / 共13页
Android数据库使用文档Word文档格式.docx_第9页
第9页 / 共13页
Android数据库使用文档Word文档格式.docx_第10页
第10页 / 共13页
Android数据库使用文档Word文档格式.docx_第11页
第11页 / 共13页
Android数据库使用文档Word文档格式.docx_第12页
第12页 / 共13页
Android数据库使用文档Word文档格式.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Android数据库使用文档Word文档格式.docx

《Android数据库使用文档Word文档格式.docx》由会员分享,可在线阅读,更多相关《Android数据库使用文档Word文档格式.docx(13页珍藏版)》请在冰点文库上搜索。

Android数据库使用文档Word文档格式.docx

1.数据的保存和不同Activity之间数据的传递9

2.登陆时使用11

本文的主要目的是记录Android中数据库的使用,以便于日后使用。

一、安装SQLite

在网上查找SQLite-1.0.66.0-setup.exe或其他版本下载安装。

二、创建数据库和创建表

◆使用SQLiteExpertPersonal3创建数据库和表。

◆Android数据库必须包含表:

”android_metadata”表的属性为”locale”,属性值为”en_US”,否则会报错。

◆每一张表中必须包含”_id”,否则会报错。

(除了”android_metadata”)

如果一个SQLite项目完成了,而数据库又是通过SQLiteExpertPersonal3创建的,那么就需要将创建的数据库与应用程序一起发布,否则程序在使用的时候会找不到数据库。

1.在res下新建一个raw文件夹,然后将XXX.db添加进去。

raw和assets这两个文件夹下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。

2.实现代码:

publicclassMainActivityextendsActivity{

publicstaticStringdbName="

user.db"

;

//数据库的名字

//数据库在手机里的路径

privatestaticStringDATABASE_PATH=“/data/data/yourpackage/”

privatestaticStringoutFileName=DATABASE_PATH+"

/"

+dbName;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

//判断数据库是否存在

booleandbExist=checkDataBase();

if(dbExist){

}else{//不存在就把raw里的数据库写入手机

try{

copyDataBase();

}catch(Exceptione){

thrownewError("

Errorcopyingdatabase"

);

}

}

/*

*方法名:

checkDataBase()

*方法作用:

查看数据库是否存在

*@param:

*@return:

存在返回true,不存在返回false

*/

publicbooleancheckDataBase(){

SQLiteDatabasecheckDB=null;

try{

checkDB=SQLiteDatabase.openDatabase(outFileName,null,

SQLiteDatabase.OPEN_READWRITE);

}catch(SQLiteExceptione){

if(checkDB!

=null){

checkDB.close();

returncheckDB!

=null?

true:

false;

}

*方法名:

copyDataBase()

*方法作用:

将res/raw中的数据库文件复制到/data/data/your.package/下

*@param:

*@return:

*/

publicvoidcopyDataBase()throwsException{

InputStreammyInput=LoginActivity.this.getResources().openRawResource(R.raw.user);

Filefile=newFile(outFileName);

Filedir=newFile(DATABASE_PATH);

if(!

dir.exists()){

dir.mkdir()){

thrownewException("

创建失败"

}

if(!

file.exists()){

try{

OutputStreammyOutput=newFileOutputStream(outFileName);

byte[]buffer=newbyte[1024];

intlength;

while((length=myInput.read(buffer))>

0){

myOutput.write(buffer,0,length);

myOutput.close();

myInput.close();

}catch(Exceptione){

e.printStackTrace();

}

至此,你的数据库文件就已经复制到你的手机路径/data/data/yourpackage上了,每次程序运行时都会在你手机上的/data/data/yourpackage/下查找数据库,如果你的raw上没有数据库,那么程序将会为你新建一个数据库,不过该数据库是空的。

四、数据库操作

1.打开数据库

privateSQLiteDatabasedb;

db=SQLiteDatabase.openOrCreateDatabase(数据库路径,null);

2.查询数据库

每次使用数据库时都要将数据库打开,使用完之后要将数据库关闭。

//该查询数据库的例子是将查询到的数据库放在listView上面,也可以根据项目的需要放在其他控件上

publicvoidqueryData(){

db=SQLiteDatabase.openOrCreateDatabase(数据库路径,null);

//使用query方法查询

//query方法参数

//query(Stringtable,String[]columns,Stringselection,String[]//selectionArgs,StringgroupBy,Stringhaving,StringOrderBy)

Cursorcursor=db.query(TABLE_NAME,null,null,null,null,null,null);

this.startManagingCursor(cursor);

SimpleCursorAdapteradapter=newSimpleCursorAdapter(MainActivity.this,R.layout.你所建的listView的布局文件(userinfo),cursor,newString[]{"

数据库表A属性(username)"

"

数据库表A属性(password)"

},newint[]{R.id.userinfo的id(usernameId),R.id.userinfo的id(passwdId)});

setListAdapter(adapter);

adapter.notifyDataSetChanged();

◆在SQLite中query如何用模糊查询

//query方法参数见上面的例子

//1.使用以下query方法%号前不能加单引号‘

//tab_field是数据库表的属性

Cursorcursor=db.query(TABLE_NAME,newString[]{tab_field},

tab_field+”LIKE?

”,newString[]{“%”+str[0]+”%”},null,null,null);

//2.使用以下query方法%号前必须加单引号‘

tab_field+”LIKE‘%“+str[0]+”%’”,null,null,null,null);

//3.使用以下方法%号前必须加单引号‘

Stringcurrent_sql_sel=“select*FROM”+TABLE_NAME+”WHERE”+tab_field+”LIKE‘%”+str[0]+

“%’”;

举例:

/**

*搜索问题

publicvoidsearchData(Stringquestion){

db=SQLiteDatabase.openOrCreateDatabase(DB_PATH,null);

Cursorcursor=db.query(TABLE_NAME,null,"

questionlike'

%"

+question+"

%'

"

null,null,null,null);

this.startManagingCursor(cursor);

SimpleCursorAdapteradapter=newSimpleCursorAdapter(Qora1_4Activity.this,R.layout.qorainfo,cursor,newString[]{

QUESTION,ANSWER},newint[]{R.id.questionId,R.id.answerId});

3.插入数据

//本例子是在两个EditText控件中取出数据插入到数据库中

//打开数据库

db=SQLiteDatabase.openOrCreateDatabase(DB_PATH,null);

//取出控件

et_questionObj=(EditText)layout.findViewById(R.id.et_questionId);

et_answerObj=(EditText)layout.findViewById(R.id.et_answerId);

//获取EditText控件的字符串数据

Stringet_nameStr=et_questionObj.getText().toString();

Stringet_passwdStr=et_answerObj.getText().toString();

//生成ContentValues对象

//ContentValues对象的说明见下面的解释

ContentValuesvalues=newContentValues();

//在该对象中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须与键的数据类型一//致

values.put(“你要插入的表中对应的属性1”,et_nameStr);

values.put(你要插入的表中对应的属性2,et_passwdStr);

//执行数据库语句

db.insert(TABLE_NAME,null,values);

//将查询到的方法显示在listView中,queryData()方法见数据库查询那一模块

queryData();

◆什么是ContentValues类?

ContentValues类和Hashtable比较类似,它也是负责存储一些键值对,但是它存储的键值对当中的键是String类型,而值是基本类型。

4.修改数据

修改数据的方法和插入数据的方法大同小异,同样是用ContentValues对象存储数据

//生成ContentValues对象

db.update(TABLE_NAME,values,"

_id=?

newString[]{String.valueOf(id)});

5.删除数据

//执行数据库命令,此时的id是由deleteData(finallongid)传进来的,是listViewdeitem//值,也可以是对应的表_id的具体值,根据项目的功能自行定义

db.delete(TABLE_NAME,"

五、关于数据库的其他操作

6.数据的保存和不同Activity之间数据的传递

在使用数据库的时候有时需要将数据暂时存放在某个地方,然后在其他的Activity中从该地方取出。

例如,要判断表中的某个属性,如果某属性=某值,则doSomething,此时就必须先取出数据表中的值。

◆首先应先建立一个数据表的模型,相关的代码如下User.java。

//Serializable接口

//java的“对象序列化“能将一个实现了Serializable接口的对象转换成一组byte,日后要用到这个对象的时候能把这些byte数据恢复出来并据此重构那个对象。

//复合主键类的对应类别必须实现Serializable接口

publicclassUserimplementsSerializable{

privateintid;

privateStringusername;

privateStringpassword;

privateStringrole;

publicUser(){

super();

publicUser(Stringusername,Stringpassword,Stringrole)

{

this.username=username;

this.password=password;

this.role=role;

publicintgetId(){

returnid;

publicvoidsetId(intid){

this.id=id;

publicStringgetUsername(){

returnusername;

publicvoidsetUsername(Stringusername){

this.username=username;

publicStringgetPassword(){

returnpassword;

应用:

publicvoidsetPassword(Stringpassword){

this.password=password;

publicStringgetRole(){

returnrole;

publicvoidsetRole(Stringrole){

this.role=role;

@Override

publicStringtoString(){

return"

User[id="

+id+"

username="

+username+"

password="

+password+"

role="

+role+"

]"

◆建立一个Java的单例模型,代码如下AppDomain.java:

publicclassAppDomain{

privateAppDomain(){}

privatestaticAppDomaininstance=newAppDomain();

publicstaticAppDomainGetInstance()

if(instance==null)

instance=newAppDomain();

returninstance;

Useruser;

publicUserGetAppUser()

returnuser;

publicvoidSetAppUser(Userusers)

user=users;

应用:

if(AppDomain.GetInstance().GetAppUser().getRole().equals("

admin"

))

DoSomething…

else{

DoSomething…

7.登陆时使用

◆建立一个登陆服务类,代码如下UserService.java:

publicclassUserService{

privateSQLiteDatabasedb;

privatestaticfinalStringDB_PATH="

data/data/com.lingdududu.test/user.db"

publicUserService(Contextcontext){

publicUserlogin(Stringusername,Stringpassword){

db=SQLiteDatabase.openOrCreateDatabase(DB_PATH,null);

Stringsql="

select*fromusermanege_tablewhereusername=?

andpassword=?

Cursorcursor=db.rawQuery(sql,newString[]{username,password});

if(cursor.moveToFirst()==true){

Useruser=newUser();

user.setId(cursor.getInt(0));

user.setUsername(username);

user.setPassword(password);

user.setRole(cursor.getString(3));

cursor.close();

returnuser;

returnnull;

UserServiceuService=newUserService(LoginActivity.this);

Userflag=uService.login(nameStr,passStr);

if(flag!

=null){

AppDomain.GetInstance().SetAppUser(flag);

//登陆成功

}else{

//登陆失败

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2