基于SQLite的Android数据库编程
Android应用开发中的常用技术

Android应用开发中的常用技术随着智能手机的普及,Android应用开发也成为了一个越来越重要的开发领域。
在Android应用开发中,有一些常用的技术,下面将会详细介绍这些技术并分为以下几个类别:UI设计、数据库、网络通信和开发框架。
一、UI设计在Android应用开发中,UI设计是最基础、也是最重要的环节之一,因为它关系到用户体验和用户对应用的第一印象。
以下是几个常用的UI设计技术:1. XML布局:Android应用的界面布局一般使用XML文件来实现,XML 可以很容易地描述一个UI界面中各种视图的排列方式和事件的处理。
2. Material Design:Material Design 是Google官方推出的一种UI 设计风格,它非常重视用户体验,注重美观和易用性,并提供了一系列的设计规范和样式。
3. 控件定制化:在开发中,有时候我们需要一些非标准的控件或者特殊的UI效果,这时就需要对控件进行定制化开发,适应应用的需求。
二、数据库在Android应用的开发过程中,数据持久化和管理是很重要的一部分,以下是一些常用的数据库技术:1. SQLite:SQLite是Android平台默认提供的一种数据库,它是一种轻量级的嵌入式数据库,支持事务和常用的SQL语句。
2. SharedPreferences:SharedPreferences是Android系统提供的一种轻量级数据存储方式,它只能存储一些基本类型数据,如字符串、整数等,但具有快速访问和易用性的特点。
3. ORM框架:ORM(Object-Relational Mapping)框架可以将数据映射到Java对象上,使得开发人员不用直接与SQL语句打交道,使得数据管理更加方便。
三、网络通信在现代应用开发中,网络通信是必不可少的一部分,以下是一些常用的网络通信技术:1. HttpURLConnection:HttpURLConnection是Android系统提供的一种HTTP通信方式,它可以发送HTTP请求并接收服务器返回的数据,当然我们也可以对它进行拓展。
Android教程05-应用编程

ContentProvider
ContentProvider可为数据封装,为多个应用共享。
武汉大学 Google Android 联合实验室
1、SharedPreferences 、
XML文件名
创建
SharedPreferences settings = this.getSharedPreferences("Demo", MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit();
Context.createDatabase(String name,int version ,int mode,CursorFactory factory)创建 一个新的数据库并返回SQLiteDatabase 对象
假如不能被创建,则抛出FileNotFoundException
SQLiteDatabase mydataBase=SQLiteDatabase.create(new CursorFactory(){ //创建一个数据库 //工厂类,当查询时调用来实例化一个光标 @Override public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) { // TODO Auto-generated method stub return null; } });
FileOutputStream out = this.openFileOutput(“wt.txt",MODE_APPEND); //打开文件“wt.txt"进行写操作、使用MODE_APPEND 在添加模式中打开文件 …… out.close();//关闭输出流
基于网易云信的即时通讯系统(Android端)

本科毕业设计(论文)学院(部) 计算机科学与技术学院题目基于网易云信的即时通讯系统(Android端)年级2103级专业软件工程(嵌入式)班级嵌入式二班学号1327403069姓名曹经纬指导老师金海东职称讲师论文提交日期2017-5-19目录前言 (3)第一章绪论 (4)1.1开发背景 (4)1.2国内外即时通讯发展状况 (5)1.3 课题意义 (5)1.4 研究内容 (6)1.5 论文结构 (6)1.6 本章小结 (6)第二章基于网易云信的即时通讯系统的需求分析 (8)2.1 系统概述 (8)2.1.1 网易云信简介 (8)2.1.2 开发环境搭建 (9)2.1.3 网易云信的账号体系 (11)2.2 需求分析 (12)2.2.1 目标 (12)2.2.2 主要参与者 (12)2.2.3 功能需求 (13)2.2.4 非功能性需求 (15)2.3 本章小节 (16)第三章基于网易云信的即时通讯系统的设计 (17)3.1 软件功能设计 (17)3.1.1 客户端功能设计 (17)3.1.2 服务端功能设计 (21)3.2 软件的数据库端设计 (22)3.3 本章小结 (24)第四章基于网易云信的即时通讯系统的实现 (25)4.1 服务器端 (25)4.2 客户端 (28)4.2.1整体程序设计 (28)4.2.3 用户注册模块 (30)4.2.4用户添加管理好友和群模块 (35)4.2.5 用户创建管理群模块 (39)4.2.6 好友/群组列表显示 (40)4.2.7 用户聊天模块 (42)4.2.8 聊天记录查询模块 (45)4.2.9 个人信息设置模块 (47)4.2.10 最近联系人模块 (48)第五章基于网易云信的即时通讯系统的测试 (50)5.1 用户注册功能测试 (50)5.2 用户登录功能测试 (50)5.3 用户添加好友功能测试 (50)5.4 消息功能测试 (51)第六章总结 (52)6.1 本文总结 (52)6.2 系统不足与未来展望 (52)参考文献 (54)致谢 (55)摘要本文分析了即时通讯的特点和用户需求,在网易云信服务的基础上,开发出一套即时通讯系统,系统由服务端和客户端组成。
SQLite数据库_计算机软件及应用_IT计算机_专业资料

Android SQLite数据库增删改查操作的使用详解一、使用嵌入式关系型SQLite数据库存储数据在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n)、char(n)、decimal(p,s) 等数据类型,只不过在运算或保存时会转成对应的五种数据类型。
SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么。
例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中存放日期型值。
但有一种情况例外:定义为I NTEGER PRIMARY KEY的字段只能存储64位整数,当向这种字段保存除整数以外的数据时,将会产生错误。
另外,在编写CREATE TABLE 语句时,你可以省略跟在字段名称后面的数据类型信息,如下面语句你可以省略name字段的类型信息:CREATE TABLE person (personid integer primary key autoincrement, name varchar(20)) SQLite可以解析大部分标准SQL语句,如:代码如下:创建数据库有两种方式:1.使用SQLiteDatabase的静态方法创建或打开数据库->static SQLiteDatabase openDatabase(Stringpath,SQLiteDatabase.CursorFactory factory,int flag): 打开path文件所代表的SQLite数据库。
->static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory): 打开或创建(如果不存在)file文件所代表的SQLite数据库。
SQLite的介绍操作Sqlite具体实例

SQLite的介绍操作Sqlite具体实例1.SQLite简介SQLite是⼀款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计⽬标是嵌⼊式的,⽽且⽬前已经在很多嵌⼊式产品中使⽤了它,它占⽤资源⾮常的低,在嵌⼊式设备中,可能只需要⼏百K的内存就够了。
它能够⽀持 Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语⾔相结合,⽐如Tcl、PHP、Java、C++、.Net等,还有ODBC接⼝,同样⽐起 Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度⽐他们都快。
2.SQLite的特点:轻量级SQLite和C/S模式的数据库软件不同,它是进程内的数据库引擎,因此不存在数据库的客户端和服务器。
使⽤SQLite⼀般只需要带上它的⼀个动态库,就可以享受它的全部功能。
⽽且那个动态库的尺⼨也挺⼩,以版本3.6.11为例,Windows下487KB、Linux下347KB。
不需要"安装"SQLite的核⼼引擎本⾝不依赖第三⽅的软件,使⽤它也不需要"安装"。
有点类似那种绿⾊软件。
单⼀⽂件数据库中所有的信息(⽐如表、视图等)都包含在⼀个⽂件内。
这个⽂件可以⾃由复制到其它⽬录或其它机器上。
跨平台/可移植性除了主流操作系统 windows,linux之后,SQLite还⽀持其它⼀些不常⽤的操作系统。
弱类型的字段同⼀列中的数据可以是不同类型开源3.SQLite数据类型⼀般数据采⽤的固定的静态数据类型,⽽SQLite采⽤的是动态数据类型,会根据存⼊值⾃动判断。
SQLite具有以下五种常⽤的数据类型:NULL: 这个值为空值VARCHAR(n):长度不固定且其最⼤长度为 n 的字串,n不能超过 4000。
CHAR(n):长度固定为n的字串,n不能超过 254。
INTEGER: 值被标识为整数,依据值的⼤⼩可以依次被存储为1,2,3,4,5,6,7,8.REAL: 所有值都是浮动的数值,被存储为8字节的IEEE浮动标记序号.TEXT: 值为⽂本字符串,使⽤数据库编码存储(TUTF-8, UTF-16BE or UTF-16-LE).BLOB: 值是BLOB数据块,以输⼊的数据格式进⾏存储。
androidstudio数据库查询语句

androidstudio数据库查询语句Android Studio是一种集成开发环境(IDE),用于开发Android应用程序。
在Android应用程序开发中,数据库查询是非常重要的一环。
本文将一步一步地回答关于Android Studio数据库查询语句的问题。
第一步:什么是数据库查询语句?数据库查询语句是一种用于从数据库中检索数据的命令。
它可以选择性地检索特定的数据,并可以对数据进行排序、过滤和聚合等操作。
数据库查询语句是通过使用SQL(结构化查询语言)编写的。
第二步:如何在Android Studio中执行数据库查询语句?在Android Studio中,可以使用SQLite数据库进行数据存储和查询。
SQLite 是一种轻量级的数据库引擎,适用于Android应用程序开发。
首先,在Android Studio中创建数据库。
可以使用SQLiteOpenHelper类来创建数据库和表,并在onCreate()方法中执行所需的数据库创建和初始化操作。
javapublic class DatabaseHelper extends SQLiteOpenHelper {private static final String DATABASE_NAME = "mydatabase.db";private static final int DATABASE_VERSION = 1;public DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");}...}然后,在需要执行数据库查询语句的地方,获取一个可读的数据库实例,并使用rawQuery()方法执行查询语句。
android之sqlite实现增删改查

Sqlite 实现简单的增删改查主界面:功能实现:1)增加数据:2)全部显示3)清除显示4)全部删除5):ID删除6)ID查询:7)ID更新:主要代码段:DBOpenHelper.javapackage com.chen.dao;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DBOpenHelper extends SQLiteOpenHelper { private static final int VERSION=2;//版本private static final String DBNAME="user.db";//数据库名字//创建数据库public DBOpenHelper(Context context) {super(context, DBNAME, null, VERSION);}@Override/** 创建表*/public void onCreate(SQLiteDatabase db) {db.execSQL("create table if not exists u_user(_id integer primary key,name varchar(20),age integer,tall varchar(5))");}//版本被更新时执行@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}UserDAO.javapackage com.chen.dao;import java.util.ArrayList;import java.util.List;import er;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class UserDAO {private DBOpenHelper helper;//写入,不然会是出错,是空指针public UserDAO(Context context){helper=new DBOpenHelper(context);}/*** 添加用户信息*/public void add(User user){SQLiteDatabase db=helper.getWritableDatabase();String sql="Insert into u_user(_id,name,age,tall) values(?,?,?,?)";db.execSQL(sql, new Object[]{user.getId(),user.getName(),user.getAge(),user.getTall()});db.close();}/*** 删除用户信息*/public void delete(Integer...id){if(id.length>0){StringBuffer sb=new StringBuffer();for(int i=0;i<id.length;i++){sb.append("?").append(",");}sb.deleteCharAt(sb.length()-1);SQLiteDatabase database=helper.getWritableDatabase();String sql="delete from u_user where _id in ("+sb+")";database.execSQL(sql, (Object[])id);}}/*** 删除表里的全部数据*/public void delelteall(){SQLiteDatabase database=helper.getWritableDatabase();String sql = "delete from u_user";database.execSQL(sql);}/*** 用户修改*/public void update(User user){SQLiteDatabase db=helper.getWritableDatabase();//写入数据库中注意!!!!不能放在外面String sql="update u_user set name=?,age=?,tall=? where _id=?";db.execSQL(sql, new Object[]{user.getName(),user.getAge(),user.getTall(),user.getId()});}/*** 查找用户信息*/public User find(int userid){SQLiteDatabase db=helper.getWritableDatabase();//写入数据库中注意!!!!不能放在外面String sql="select _id,name,age,tall from u_user where _id=?";Cursor cursor=db.rawQuery(sql, new String[]{String.valueOf(userid)});if(cursor.moveToNext()){return new User(cursor.getInt(cursor.getColumnIndex("_id")),cursor.getString(cursor.getColumnIndex("name")),cursor.getInt(cursor.getColumnIndex("age")),cursor.getString(cursor.getColumnIndex("tall")));}return null;}/*** 显示用户*/public Cursor select() {SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("u_user",null, null, null, null,null, "_id desc");return cursor;}}MainActivity.javapackage com.chen.database;/*** writer:ManMan*Email:*******************/import erDAO;import er;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{ private static final String TAG = "Add";private EditText edname,edage,edtall,id;private Button add,deleteshow,show,iddelete,idupdate,idshow,deleteall;private TextView tedatashow;private ListView datashow;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);edname = (EditText)findViewById(R.id.edname);edage = (EditText)findViewById(R.id.edage);edtall = (EditText)findViewById(R.id.edtall);id = (EditText)findViewById(R.id.id);tedatashow = (TextView)findViewById(R.id.tedatashow);datashow = (ListView)findViewById(R.id.datashow);add = (Button)findViewById(R.id.add);deleteshow = (Button)findViewById(R.id.deleteshow);show = (Button)findViewById(R.id.show);iddelete = (Button)findViewById(R.id.iddelete);idupdate = (Button)findViewById(R.id.idupdate);idshow = (Button)findViewById(R.id.idshow);deleteall = (Button)findViewById(R.id.deleteall);add.setOnClickListener(this);deleteshow.setOnClickListener(this);show.setOnClickListener(this);iddelete.setOnClickListener(this);idupdate.setOnClickListener(this);idshow.setOnClickListener(this);deleteall.setOnClickListener(this);}public void onClick(View v) {/** 添加数据*/if(v==add){if(!id.getText().toString().equals(null)&&!edname.getText().toString().equals(null)&&!edage .getText().toString().equals(null)&&!edtall.getText().toString().equals(null)){try{UserDAO userdao = new UserDAO(MainActivity.this);User user = new User(Integer.valueOf(id.getText().toString()), edname.getText().toString(),Integer.valueOf(edage.getText().toString()), edtall.getText().toString());userdao.add(user);Toast.makeText(MainActivity.this, "成功添加!", Toast.LENGTH_LONG).show();tedatashow.setText("新添数据为:"+"\n"+"ID:"+Integer.valueOf(id.getText().toString())+","+"姓名:"+edname.getText().toString()+","+"年龄:"+Integer.valueOf(edage.getText().toString())+","+"身高:"+edtall.getText().toString()+",");//设置为空edage.setText("");edname.setText("");edtall.setText("");id.setText("");}catch (Exception e) {Log.i(TAG, "出错了");Log.i(TAG, e.getMessage());}}else if(id.getText().toString().equals(null)){Toast.makeText(MainActivity.this, "ID不能为空!", Toast.LENGTH_LONG) .show();}}/** 清除显示*/if(v==deleteshow){tedatashow.setText("");}/** 全部显示*/if(v==show){try{UserDAO userdao = new UserDAO(MainActivity.this);Cursor cursor = userdao.select();/** 构建Listview适配器*/SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, yout.showuser, cursor, new String[]{"_id","name","age","tall" },newint[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4});datashow.setAdapter(adapter);}catch (Exception f) {Log.e("err",f.getMessage());Log.e("err",null, f.getCause());Toast.makeText(MainActivity.this, "显示不了", Toast.LENGTH_LONG).show();}}/** ID删除*/if(v==iddelete){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面userDAO.delete(Integer.valueOf(id.getText().toString()));Toast.makeText(MainActivity.this, "您成功删除了"+Integer.valueOf(id.getText().toString()), Toast.LENGTH_LONG).show();id.setText("");}/** 全部删除*/if(v==deleteall){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面userDAO.delelteall();Toast.makeText(MainActivity.this, "您已经把数据全部删除了", Toast.LENGTH_LONG).show();}/** ID更新*/if(v==idupdate){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面User user=userDAO.find(Integer.valueOf(id.getText().toString()));user.setName(edname.getText().toString());user.setAge(Integer.valueOf(edage.getText().toString()));user.setTall(edtall.getText().toString());userDAO.update(user);Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();}/** ID查询*/if(v==idshow){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面User user=userDAO.find(Integer.valueOf(id.getText().toString()));tedatashow.setText("ID:"+user.getId()+" "+"姓名:"+user.getName()+" "+"年龄:"+user.getAge()+" "+"身高"+user.getTall());}/***/}}User.javapackage com.chen.modl;public class User {private int id;private String name;private int age;private String tall;/** 构造方法*/public User(){super();}public User(int id,String name,int age,String tall){ this.id=id;=name;this.age=age;this.tall=tall;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) { = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getTall() {return tall;}public void setTall(String tall) {this.tall = tall;}//复写toString()方法进行查找public String toString(){return"id:"+id+"name:"+name+"age"+age+"tall"+tall;}}Main.xml<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!--姓名 --><TextViewandroid:text="姓名:"android:layout_width="wrap_content"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/edname"android:layout_height="wrap_content"android:layout_width="278dp"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!--年龄 --><TextViewandroid:text="年龄:"android:layout_width="wrap_content"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/edage"android:layout_width="278dp"/></LinearLayout><!--身高 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:text="身高:"android:layout_width="wrap_content"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/edtall"android:layout_height="wrap_content"android:layout_width="278dp"/></LinearLayout><!--按钮 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/add"android:text="添加数据"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dip"/><Buttonandroid:id="@+id/show"android:text="全部显示"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/deleteshow"android:text="清除显示"android:layout_width="wrap_content"/><Buttonandroid:id="@+id/deleteall"android:text="全部删除"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><!--按钮 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:text="ID:"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dip"android:layout_weight="1"/><EditTextandroid:id="@+id/id"android:layout_width="75dip"android:layout_height="wrap_content"android:layout_marginLeft="3dip"android:layout_weight="2"/><Buttonandroid:id="@+id/iddelete"android:text="ID删除"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dip"android:layout_weight="1"/><Buttonandroid:id="@+id/idshow"android:text="ID查询"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"/><Buttonandroid:id="@+id/idupdate"android:text="ID更新"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout><TextViewandroid:text="数据库数据显示:"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tedatashow"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="15dip"/><ListViewandroid:id="@+id/datashow"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="15dip"/></LinearLayout>。
android中SQLite的使用总结,用excSQL和rawQuery方法实现一般得增删改查

android中SQLite的使用总结,用excSQL和rawQuery方法实现一般得增删改查1:androidmanifest.xml的内容[html]view plaincopy1.<?xml version="1.0" encoding="utf-8"?>2.<manifest xmlns:android="/apk/res/android"3.package="cn.itcast.db"4.android:versionCode="1"5.android:versionName="1.0">6.<application android:icon="@drawable/icon" android:lab el="@string/app_name">7.<uses-library android:name="android.test.runner" />8.<activity android:name=".MainActivity"9.android:label="@string/app_name">10.<intent-filter>11.<action android:name="android.intent.action.MAIN" / >12.<category android:name=" UNCHER" />13.</intent-filter>14.</activity>15.16.</application>17.18.<uses-sdk android:minSdkVersion="8" />19.<!-- 配置测试环境 -->20.<instrumentation android:name="android.test.Instru mentationTestRunner"21.android:targetPackage="cn.itcast.db"22.android:label="T est for My App"/>23.24.</manifest>2:Person类[java]view plaincopy1.package cn.itcast.domain;2.3.public class Person {4.5.private Integer id;6.private String name;7.8.public Person() {9.10.}11.12.public Integer getId() {13.return id;14.}15.16.public void setId(Integer id) {17.this.id = id;18.}19.20.public String getName() {21.return name;22.}23.24.public void setName(String name) { = name;26.}27.28.@Override29.public String toString() {30.return "Person [id=" + id + ", name=" + name + "]";31.}32.33.}3:DBOpenHelper类,该类继承了SQLiteOpenHelper类[java]view plaincopy1.package cn.itcast.service;2.3.import android.content.Context;4.import android.database.sqlite.SQLiteDatabase;5.import android.database.sqlite.SQLiteOpenHelper;6.import android.database.sqlite.SQLiteDatabase.CursorFac tory;7.8.public class DBOpenHelper extends SQLiteOpenHelper {9.10.private static final String DATABASENAME = "cn.itcast. db";11.private static final int DATABASEVERSION = 1;12.13./*14.* 构造函数15.*/16.public DBOpenHelper(Context context) {17.super(context, DATABASENAME, null, DATABASEVERSION);18.}19.20./*21.* 数据库第一次生成时调用该方法,创建一些表或者初始化一些数据22.* @see android.database.sqlite.SQLiteOpenHelper#on Create(android.database.sqlite.SQLiteDatabase)23.*/24.@Override25.public void onCreate(SQLiteDatabase db) {26.db.execSQL("create table person(personid integer pri mary key autoincrement, name varchar(20))");27.}28.29.@Override30.public void onUpgrade(SQLiteDatabase db, int oldVer sion, int newVersion) {31.32.}33.34.}4:PersonService类[java]view plaincopy1.package cn.itcast.service;2.3.import java.util.ArrayList;4.import java.util.List;5.6.import android.content.Context;7.import android.database.Cursor;8.import android.database.sqlite.SQLiteDatabase;9.import cn.itcast.domain.Person;10.11.public class PersonService {12.13.private DBOpenHelper dbOpenHelper;14.15.public PersonService(Context context) {16.this.dbOpenHelper = new DBOpenHelper(context);17.}18.19./*20.* save a person to the database21.*/22.public void save(Person person) {23.SQLiteDatabase database = dbOpenHelper.getWritabl eDatabase();24.database.execSQL("insert into person(name) values (?) ", new Object[]{person.getName()});25.}26.27./*28.* updata a person to the database29.*/30.public void update(Person person) {31.SQLiteDatabase database = dbOpenHelper.getWritabl eDatabase();32.database.execSQL("update person set name=? wherepersonid=?", new Object[]{person.getName(), person.getId()});33.}34.35./*36.* delete a person from the database according to the id37.*/38.public void delete(Integer id) {39.SQLiteDatabase database = dbOpenHelper.getWritabl eDatabase();40.database.execSQL("delete from person where personi d=?", new Object[]{id.toString()});41.}42.43./*44.* find a person from the database according to the id45.*/46.public Person find(Integer id) {47.SQLiteDatabase database = dbOpenHelper.getReadab leDatabase();48.Cursor cursor = database.rawQuery("select * from per son where personid=?", new String[]{id.toString()});49.Person person = null;50.if(cursor.moveT oFirst()) {51.Integer personid = cursor.getInt(cursor.getColumnInd ex("personid"));52.String name = cursor.getString(cursor.getColumnIndex ("name"));53.person = new Person();54.person.setId(personid);56.}57.return person;58.}59.60./*61.* get the data of person accroding to the offset and m axResult62.*/63.public List<Person> getScrollData(Integer offset, Integ er maxResult) {64.SQLiteDatabase database = dbOpenHelper.getReadab leDatabase();65.Cursor cursor = database.rawQuery("select * from per son limit ?,?", new String[] {offset.toString(), maxResult.toString() });66.int idIndex = 0;67.int nameIndex = 0;68.List<Person> personList = null;69.70.if(cursor.getCount() >= 0) {71.idIndex = cursor.getColumnIndex("personid");Index = cursor.getColumnIndex("name");73.personList = new ArrayList<Person>();74.}75.76.while(cursor.moveToNext()) {77.Integer personid = cursor.getInt(idIndex);78.String name = cursor.getString(nameIndex);79.Person person = new Person();81.person.setName(name);82.personList.add(person);83.}84.return personList;85.}86.87./*88.* get the count of the database89.*/90.public long getCount(){91.SQLiteDatabase database = dbOpenHelper.getReadab leDatabase();92.Cursor cursor = database.rawQuery("select count(*) fr om person", null);93.cursor.moveToFirst();94.return cursor.getLong(0);95.}96.97.}5:PersonServiceT est类[java]view plaincopy1.package cn.itcast.db;2.3.import java.util.List;4.5.import android.test.AndroidTestCase;6.import android.util.Log;7.import cn.itcast.domain.Person;8.import cn.itcast.service.DBOpenHelper;9.import cn.itcast.service.PersonService;10.11.public class PersonServiceTest extends AndroidT estCase {12.private static final String TAG = "PersonServiceTest";13.14./*15.* 测试生成数据库的方法16.*/17.public void testCreateDB() throws Throwable {18.DBOpenHelper dbOpenHelper = new DBOpenHelper( this.getContext());19.dbOpenHelper.getWritableDatabase(); //第一次调用该方法会生成数据库20.21.}22.23./*24.* 测试保存方法25.*/26.public void testSave() throws Throwable{27.PersonService personService = new PersonService(this .getContext());28.29.Person person1 = new Person();30.person1.setName("zhangsan");31.personService.save(person1);32.33.Person person2 = new Person();34.person2.setName("lisi");35.personService.save(person2);36.37.Person person3 = new Person();38.person3.setName("wangwu");39.personService.save(person3);40.41.}42.43.public void testDelete() {44.PersonService personService = new PersonService(this .getContext());45.personService.delete(1);46.47.}48.49./*50.* 测试更新方法51.*/52.public void testUpdate() {53.PersonService personService = new PersonService(this .getContext());54.Person person = personService.find(1);55.person.setName("zhaoliu");56.personService.update(person);57.58.}59.60./*61.* 测试获得数据方法63.public void testGetScrollData() throws Throwable{64.PersonService personService = new PersonService(this .getContext());65.List<Person> persons = personService.getScrollData(0 , 3);66.for(Person person : persons) {67.Log.i(TAG, person.toString());68.}69.}70.71./*72.* 测试根据id查找的方法73.*/74.public void testFind() throws Throwable{75.PersonService personService = new PersonService(this .getContext());76.Person person = personService.find(1);77.Log.i(TAG, person.toString());78.79.}80.81./*82.* 测试获得数量的方法83.*/84.public void testGetCount() {85.PersonService personService = new PersonService(this .getContext());86.long count = personService.getCount();87.Log.i(TAG, count + "");89.}。