用一个小实例说明android运用SqLite数据库的方法-唐诗三百首

本文以一个简单的Android应用为例,详细介绍了如何使用SQLite数据库进行数据查询和管理。包括按类型、作者查询唐诗,以及在数据库中创建`poet`和`content`表,涉及 Dao 类和 SQLiteOpenHelper 的使用。通过代码实现,帮助初学者理解Android中SQLite数据库的运用。
摘要由CSDN通过智能技术生成

目前初学android,也许很多人对于SQLite数据库的使用不是很清楚,主要对于查询这方面,直接先上图。

1.主界面,直接查询出所有数据

2,按类型查询

3,按作者查询

4,点击一首诗,出现对于的诗的完整信息

5,点击译文,注解都会再下面插入,再次点击撤销

可以上下拖动

下面我们就开始上代码和数据库建立

Activity:
MianActivity的activity_main布局,放入一个TabHost,TabHost分别跳转到3个Activity-全部(AllActiity),按类型(TypeActivity),按作者(PoetActivity)。
按类型(TypeActivity):两个类型分别跳转到-五言绝句(Type5Activity),七言绝句(Type7Activity)
按作者(PoetAtivity):加入一个自动文本AutoCompleteTextView进行搜索。

数据库:Poet(database)下有2个表:poet表( _id;poetname;poemname;poemtype;)   content表(_id;poemname;poetname;poemcontent;poemyiwen;poemzhujie;)

实体类:
Poet.java:content.java

SQLiteOpenHelper:DBOpenHelper.java创建数据库

Dao.java:对表中数据进行操作的方法类

 

 Poet.java:

package com.example.android_1800_sqlite1;

public class Poet {  int _id;  String poetname;  String poemname;  String poemtype;  public Poet() {   super();   // TODO Auto-generated constructor stub  }  public Poet(int _id, String poetname, String poemname, String poemtype) {   super();   this._id = _id;   this.poetname = poetname;   this.poemname = poemname;   this.poemtype = poemtype;  }  public int get_id() {   return _id;  }  public void set_id(int _id) {   this._id = _id;  }  public String getPoetname() {   return poetname;  }  public void setPoetname(String poetname) {   this.poetname = poetname;  }  public String getPoemname() {   return poemname;  }  public void setPoemname(String poemname) {   this.poemname = poemname;  }  public String getPoemtype() {   return poemtype;  }  public void setPoemtype(String type) {   this.poemtype = type;  }  @Override  public String toString() {   return "Poet [_id=" + _id + ", poetname=" + poetname + ", poemname="     + poemname + ", type=" + poemtype + "]";  } }


 

 

content.java

package com.example.android_1800_sqlite1;

import java.io.Serializable;

public class Content implements Serializable{  int _id;  String poemname;  String poetname;  String poemcontent;  String poemyiwen;  String poemzhujie;  public Content() {   super();   // TODO Auto-generated constructor stub  }  public Content(int _id, String poemname, String poetname,    String poemcontent, String poemyiwen, String poemzhujie) {   super();   this._id = _id;   this.poemname = poemname;   this.poetname = poetname;   this.poemcontent = poemcontent;   this.poemyiwen = poemyiwen;   this.poemzhujie = poemzhujie;  }  public int get_id() {   return _id;  }  public void set_id(int _id) {   this._id = _id;  }  public String getPoemname() {   return poemname;  }  public void setPoemname(String poemname) {   this.poemname = poemname;  }  public String getPoetname() {   return poetname;  }  public void setPoetname(String poetname) {   this.poetname = poetname;  }  public String getPoemcontent() {   return poemcontent;  }  public void setPoemcontent(String poemcontent) {   this.poemcontent = poemcontent;  }  public String getPoemyiwen() {   return poemyiwen;  }  public void setPoemyiwen(String poemyiwen) {   this.poemyiwen = poemyiwen;  }  public String getPoemzhujie() {   return poemzhujie;  }  public void setPoemzhujie(String poemzhujie) {   this.poemzhujie = poemzhujie;  }  @Override  public String toString() {   return "Content [_id=" + _id + ", poemname=" + poemname + ", poetname="     + poetname + ", poemcontent=" + poemcontent + ", poemyiwen="     + poemyiwen + ", poemzhujie=" + poemzhujie + "]";  } }


 

MainActivity.java:

package com.example.android_1800_sqlite1;

import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.LocalActivityManager; import android.co

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值