一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Android - Android操作SQLite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及ListView顯示數(shù)據(jù)的方法詳解

Android操作SQLite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及ListView顯示數(shù)據(jù)的方法詳解

2021-06-09 16:04fengyee_zju Android

這篇文章主要介紹了Android操作SQLite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及ListView顯示數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android操作SQLite數(shù)據(jù)庫(kù)及使用ListView顯示數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了android操作sqlite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及l(fā)istview顯示數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

由于剛接觸android開(kāi)發(fā),故此想把學(xué)到的基礎(chǔ)知識(shí)記錄一下,以備查詢(xún),故此寫(xiě)的比較啰嗦:

步驟如下:

一、介紹:

此文主要是介紹怎么使用android自帶的數(shù)據(jù)庫(kù)sqlite,以及把后臺(tái)的數(shù)據(jù)用listview控件顯示

二、新建一個(gè)android工程——dbsqliteoperate

工程目錄:

Android操作SQLite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及ListView顯示數(shù)據(jù)的方法詳解

三、清單列表androidmanifest.xml的配置為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.dboperate"
  android:versioncode="1"
  android:versionname="1.0" >
  <uses-sdk android:minsdkversion="8" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <!--單元測(cè)試 加這句-->
    <uses-library android:name="android.test.runner" />
    <activity
      android:name=".dbsqliteoperateactivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
  </application>
 <instrumentation android:name="android.test.instrumentationtestrunner"
 android:targetpackage="com.example.dboperate"
 android:label="test for my app"/>
</manifest>

四、main.xml配置清單:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <linearlayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >
  <textview
    android:id="@+id/name"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:gravity="center"/>
  <textview
    android:id="@+id/phone"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/phone"
    android:gravity="center"/>
  <textview
    android:id="@+id/amount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/amount"
    android:gravity="center"/>
</linearlayout>
  <listview
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  </listview>
</linearlayout>

五、item.xml配置清單:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <textview
    android:id="@+id/name"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:gravity="center"/>
  <textview
    android:id="@+id/phone"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/phone"
    android:gravity="center"/>
  <textview
    android:id="@+id/amount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/amount"
    android:gravity="center"/>
</linearlayout>

六、string.xml配置清單:

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">hello world, dbsqliteoperateactivity!</string>
  <string name="app_name">exampledbsqliteoperate8</string>
  <string name="name">姓名</string>
  <string name="phone">電話</string>
  <string name="amount">存款</string>
</resources>

七、dbsqliteoperateactivity.java activity類(lèi)的源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.example.dboperate;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import com.example.adapter.personadapter;
import com.example.domain.person;
import com.example.service.personservice;
import android.app.activity;
import android.database.cursor;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.listview;
import android.widget.simpleadapter;
import android.widget.simplecursoradapter;
import android.widget.toast;
public class dbsqliteoperateactivity extends activity {
 listview listview;
 personservice personservice;
 onitemclicklistener listviewlistener;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    listviewlistener = new onitemclicklistener(){
  @override
  public void onitemclick(adapterview<?> parent, view view, int position, long id) {
  //得到listview控件
  listview listview = (listview)parent;
  //1、如果使用自定義適配器,返回的是person對(duì)象
  //得到該條目數(shù)據(jù)
//  person person = (person)listview.getitematposition(position);
//  //廣播出去
//  toast.maketext(getapplicationcontext(), person.tostring(), toast.length_long).show();
  //2、如果使用showlist2()方法中的適配器時(shí),則取得的值是不一樣的,返回的是cursor
//  cursor cursor = (cursor)listview.getitematposition(position);
//  int personid = cursor.getint(cursor.getcolumnindex("_id"));
//  toast.maketext(getapplicationcontext(), personid+"", toast.length_long).show();
  //3、如果使用showlist()方法中的適配器時(shí),則取得的值是不一樣的,返回的是map
  @suppresswarnings("unchecked")
  map<string,object> map = (map)listview.getitematposition(position);
  string name = map.get("name").tostring();
  string personid = map.get("personid").tostring();
  toast.maketext(getapplicationcontext(), personid +"-"+ name, toast.length_long).show();
  }
    };
    listview = (listview) this.findviewbyid(r.id.listview);
    listview.setonitemclicklistener(listviewlistener);
    personservice = new personservice(this);
    showlist();
  }
 private void showlist() {
 list<person> persons = personservice.getscrolldata(0, 50);
 list<hashmap<string,object>> data = new arraylist<hashmap<string,object>>();
 for(person person : persons){
  hashmap<string,object> item = new hashmap<string,object>();
  item.put("name", person.getname());
  item.put("phone", person.getphone());
  item.put("amount", person.getamount());
  item.put("personid", person.getid());
  data.add(item);
 }
 simpleadapter adapter = new simpleadapter(this,data,r.layout.item, new string[]{"name","phone","amount"}, new int[]{r.id.name,r.id.phone,r.id.amount});
   listview.setadapter(adapter);
 }
 public void showlist2(){
 cursor cursor = personservice.getcursorscrolldata(0, 50);
 //該適配器要求返回的結(jié)果集cursor必須包含_id字段,所以需要對(duì)取得結(jié)果集進(jìn)行處理
 simplecursoradapter adapter = new simplecursoradapter(this,r.layout.item,cursor,new string[]{"name","phone","amount"}, new int[]{r.id.name,r.id.phone,r.id.amount} );
 listview.setadapter(adapter);
 }
 /**
 * 自定義適配器
 */
 public void showlist3(){
 list<person> persons = personservice.getscrolldata(0, 50);
 /**
  * 第一個(gè)參數(shù):上下文context,第二個(gè)參數(shù):要顯示的數(shù)據(jù),第三個(gè)參數(shù):綁定的條目界面
  */
 personadapter adapter = new personadapter(this, persons, r.layout.item);
 listview.setadapter(adapter);
 }
}

八、person.java 實(shí)體類(lèi) 源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.domain;
public class person {
 private integer id;
 private string name;
 private string phone;
 private integer amount;
 public integer getid() {
 return id;
 }
 public void setid(integer id) {
 this.id = id;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public string getphone() {
 return phone;
 }
 public void setphone(string phone) {
 this.phone = phone;
 }
 public person(string name, string phone) {
 this.name = name;
 this.phone = phone;
 }
 public person(integer id, string name, string phone,integer amount) {
 super();
 this.id = id;
 this.name = name;
 this.phone = phone;
 this.amount = amount;
 }
 public person() {
 super();
 }
 public integer getamount() {
 return amount;
 }
 public void setamount(integer amount) {
 this.amount = amount;
 }
 @override
 public string tostring() {
 return "person [id=" + id + ", name=" + name + ", phone=" + phone
  + ", amount=" + amount + "]";
 }
}

九、dboperatehelper.java 業(yè)務(wù)類(lèi)源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.service;
import android.content.context;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqlitedatabase.cursorfactory;
import android.database.sqlite.sqliteopenhelper;
public class dboperatehelper extends sqliteopenhelper {
 public dboperatehelper(context context) {//默認(rèn)創(chuàng)建的數(shù)據(jù)庫(kù)文件保存在<包名>/database/
 //第一個(gè)參數(shù)是上下文,第二個(gè)參數(shù)是數(shù)據(jù)庫(kù)名稱(chēng),第三個(gè)是游標(biāo)工廠為null時(shí)使用 數(shù)據(jù)庫(kù)默認(rèn)的游標(biāo)工廠,第四個(gè)是數(shù)據(jù)庫(kù)版本號(hào)但是不能為0,一般大于0
 super(context, "smallpig", null, 4);
 }
 /**
 * 數(shù)據(jù)庫(kù)每一次被創(chuàng)建時(shí)被調(diào)用
 */
 @override
 public void oncreate(sqlitedatabase sqldb) {
 sqldb.execsql("create table person(personid integer primary key autoincrement,name varchar(20),phone varchar(12) null)");
 }
  /**
   * 每一次數(shù)據(jù)庫(kù)版本號(hào)發(fā)生變動(dòng)時(shí)觸發(fā)此方法
   * 比如如果想往數(shù)據(jù)庫(kù)中再插入一些表、字段或者其他信息時(shí)通過(guò)修改數(shù)據(jù)庫(kù)版本號(hào)來(lái)觸發(fā)此方法
   */
 @override
 public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
 //db.execsql("alter table person add phone varchar(12) null");\
 db.execsql("alter table person add amount integer null");
 }
}

十、personservice.java 業(yè)務(wù)類(lèi)源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.example.service;
import java.util.arraylist;
import java.util.list;
import android.content.context;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import com.example.domain.person;
public class personservice {
 private dboperatehelper dboperatehelper;
 public personservice(context context) {
 this.dboperatehelper = new dboperatehelper(context);
 }
  /**
   * 保存記錄
   * @param person
   */
 public void save(person person){
 //得到數(shù)據(jù)庫(kù)實(shí)例,里面封裝了數(shù)據(jù)庫(kù)操作方法
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
   //sqldb.execsql("insert into person(name,phone) values('"+person.getname()+"','"+person.getphone()+"')");
 //利用占位符可以避免注入,但是注意數(shù)組參與與占位符對(duì)應(yīng)的字段要一一對(duì)應(yīng)
 sqldb.execsql("insert into person(name,phone,amount) values(?,?,?)",new object[]{person.getname(),person.getphone(),person.getamount()});
   //關(guān)閉數(shù)據(jù)庫(kù)
 sqldb.close();
 }
 /**
 * 刪除記錄
 * @param id
 */
 public void delete(integer id){
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
 sqldb.execsql("delete from person where personid=?",new object[]{id});
 sqldb.close();
 }
 /**
 * 更新記錄
 * @param person
 */
 public void update(person person){
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
 sqldb.execsql("update person set name=?,phone=?,amount=? where personid=?",new object[]{person.getname(),person.getphone(),person.getamount(),person.getid()});
 sqldb.close();
 }
 /**
 * 通過(guò)id查詢(xún)記錄
 * @param id
 * @return
 */
 public person find(integer id){
 /**
  * getwritabledatabase 與 getreadabledatabase 的區(qū)別:
  * getreadabledatabase會(huì)先返回getwritabledatabase(可寫(xiě)),如果調(diào)用getwritabledatabase失敗
  * 則才會(huì)調(diào)用getreadabledatabase后續(xù)方法,使數(shù)據(jù)庫(kù)只讀
  * 當(dāng)寫(xiě)入的數(shù)據(jù)超過(guò)數(shù)據(jù)庫(kù)大小則調(diào)用getwritabledatabase會(huì)失敗
  * 所以只讀時(shí)則可以使用此方法,其它情況(只要不是超過(guò)數(shù)據(jù)庫(kù)大小)也可以使用此方法
  */
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 cursor cursor = sqldb.rawquery("select * from person where personid=?", new string[]{string.valueof(id)});
 int personid;
 string name;
 string phone;
 int amount;
 person person = null;
 if(cursor.movetofirst()){
  personid = cursor.getint(cursor.getcolumnindex("personid"));
  name = cursor.getstring(cursor.getcolumnindex("name"));
  phone = cursor.getstring(cursor.getcolumnindex("phone"));
  amount = cursor.getint(cursor.getcolumnindex("amount"));
  person = new person(personid,name,phone,amount);
 }
 cursor.close();
 return person;
 }
 /**
 * 返回指定長(zhǎng)度記錄,limit 3,5,適用于分頁(yè)
 * @param offset 起始
 * @param maxresult 長(zhǎng)度
 * @return
 */
 public list<person> getscrolldata(int offset,int maxresult){
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 cursor cursor = sqldb.rawquery("select * from person order by personid asc limit ?,?", new string[]{string.valueof(offset),string.valueof(maxresult)});
 int personid;
 string name;
 string phone;
 int amount;
 person person = null;
 list<person> persons = new arraylist<person>();
 while(cursor.movetonext()){
  personid = cursor.getint(cursor.getcolumnindex("personid"));
  name = cursor.getstring(cursor.getcolumnindex("name"));
  phone = cursor.getstring(cursor.getcolumnindex("phone"));
  amount = cursor.getint(cursor.getcolumnindex("amount"));
  person = new person(personid,name,phone,amount);
  persons.add(person);
 }
 cursor.close();
 return persons;
 }
 /**
 * 返回cursor
 * @param offset 起始
 * @param maxresult 長(zhǎng)度
 * @return
 */
 public cursor getcursorscrolldata(int offset,int maxresult){
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 cursor cursor = sqldb.rawquery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?", new string[]{string.valueof(offset),string.valueof(maxresult)});
 return cursor;
 }
 /**
 * 返回總記錄數(shù)
 * @return
 */
 public long getcount(){
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 cursor cursor = sqldb.rawquery("select count(*) from person", null);
 //該查詢(xún)語(yǔ)句值返回一條語(yǔ)句
 cursor.movetofirst();
 long result = cursor.getlong(0);
 cursor.close();
 return result;
 }
 public void payment(){
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
 sqldb.begintransaction();//開(kāi)啟事務(wù)
 try{
  sqldb.execsql("update person set amount = amount -10 where personid=1");
  sqldb.execsql("update person set amount = amount + 10 where personid=2");
  sqldb.settransactionsuccessful();//設(shè)置事務(wù)標(biāo)志位true
 } finally {
  //結(jié)束事務(wù):有兩種情況:commit\rollback,事務(wù)提交或者回滾是由事務(wù)的標(biāo)識(shí)決定的
  //事務(wù)為ture則提交,事務(wù)為flase則回滾,默認(rèn)為false
  sqldb.endtransaction();
 }
 }
}

十一、otherpersonservice.java 業(yè)務(wù)類(lèi)源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.example.service;
import java.util.arraylist;
import java.util.list;
import com.example.domain.person;
import android.content.contentvalues;
import android.content.context;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqlitedatabase.cursorfactory;
import android.database.sqlite.sqliteopenhelper;
public class otherpersonservice {
 private dboperatehelper dboperatehelper;
 public otherpersonservice(context context) {
 this.dboperatehelper = new dboperatehelper(context);
 }
  /**
   * 保存記錄
   * @param person
   */
 public void save(person person){
 //得到數(shù)據(jù)庫(kù)實(shí)例,里面封裝了數(shù)據(jù)庫(kù)操作方法
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
   //sqldb.execsql("insert into person(name,phone) values('"+person.getname()+"','"+person.getphone()+"')");
 //利用占位符可以避免注入,但是注意數(shù)組參與與占位符對(duì)應(yīng)的字段要一一對(duì)應(yīng)
 //sqldb.execsql("insert into person(name,phone) values(?,?)",new object[]{person.getname(),person.getphone()});
 contentvalues values = new contentvalues();
 values.put("name", person.getname());
 values.put("phone", person.getphone());
 values.put("amount", person.getamount());
 //第一個(gè)參數(shù)是表名,第三個(gè)為字段值集合,第二個(gè)參數(shù)是空值字段,當(dāng)?shù)谌齻€(gè)字段值集合為空時(shí),系統(tǒng)會(huì)自動(dòng)插入一條第二個(gè)參數(shù)為空的sql語(yǔ)句
 //否則當(dāng)?shù)谌齻€(gè)參數(shù)為空時(shí),如果第二個(gè)參數(shù)也為空,那么插入表就會(huì)找不到插入的字段信息,會(huì)報(bào)錯(cuò)
 sqldb.insert("person", "name", values );
 //關(guān)閉數(shù)據(jù)庫(kù)
 sqldb.close();
 }
 /**
 * 刪除記錄
 * @param id
 */
 public void delete(integer id){
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
 //sqldb.execsql("delete from person where personid=?",new object[]{id});
 //第一個(gè)參數(shù)是表名,第二個(gè)是where后面的條件用占位符表示,第三個(gè)對(duì)應(yīng)占位符為參數(shù)值
 sqldb.delete("person", "personid=?", new string[]{integer.tostring(id)});
 sqldb.close();
 }
 /**
 * 更新記錄
 * @param person
 */
 public void update(person person){
 sqlitedatabase sqldb = dboperatehelper.getwritabledatabase();
 //sqldb.execsql("update person set name=?,phone=? where personid=?",new object[]{person.getname(),person.getphone(),person.getid()});
 //第一個(gè)參數(shù)為表名,第二個(gè)是一個(gè)更新值集合,采用鍵值對(duì)的形式,每個(gè)更新的字段對(duì)應(yīng)更新值
 //第三個(gè)參數(shù)是where后面條件字段用占位符標(biāo)識(shí),第四個(gè)參數(shù)是對(duì)應(yīng)where占位符的值
 contentvalues values = new contentvalues();
 values.put("name", person.getname());
 values.put("phone", person.getphone());
 values.put("amount", person.getamount());
 sqldb.update("person", values , "personid=?", new string[]{person.getid().tostring()});
 sqldb.close();
 }
 /**
 * 通過(guò)id查詢(xún)記錄
 * @param id
 * @return
 */
 public person find(integer id){
 /**
  * getwritabledatabase 與 getreadabledatabase 的區(qū)別:
  * getreadabledatabase會(huì)先返回getwritabledatabase(可寫(xiě)),如果調(diào)用getwritabledatabase失敗
  * 則才會(huì)調(diào)用getreadabledatabase后續(xù)方法,使數(shù)據(jù)庫(kù)只讀
  * 當(dāng)寫(xiě)入的數(shù)據(jù)超過(guò)數(shù)據(jù)庫(kù)大小則調(diào)用getwritabledatabase會(huì)失敗
  * 所以只讀時(shí)則可以使用此方法,其它情況(只要不是超過(guò)數(shù)據(jù)庫(kù)大小)也可以使用此方法
  */
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 //cursor cursor = sqldb.rawquery("select * from person where personid=?", new string[]{string.valueof(id)});
 //第一個(gè)參數(shù)是表名;第二個(gè)參數(shù)是查詢(xún)顯示的字段,null時(shí)默認(rèn)查詢(xún)顯示所有字段;
 //第三個(gè)參數(shù)是where查詢(xún)條件占位符;第四個(gè)是占位符對(duì)應(yīng)的值;
 //第五個(gè)參數(shù)是group by條件;第六個(gè)是having條件;第七個(gè)是order by條件
 cursor cursor = sqldb.query("person", null, "personid=?", new string[]{id.tostring()}, null, null, null);
 int personid;
 string name;
 string phone;
 int amount;
 person person = null;
 if(cursor.movetofirst()){
  personid = cursor.getint(cursor.getcolumnindex("personid"));
  name = cursor.getstring(cursor.getcolumnindex("name"));
  phone = cursor.getstring(cursor.getcolumnindex("phone"));
  amount = cursor.getint(cursor.getcolumnindex("amount"));
  person = new person(personid,name,phone,amount);
 }
 cursor.close();
 return person;
 }
 /**
 * 返回指定長(zhǎng)度記錄,limit 3,5,適用于分頁(yè)
 * @param offset 起始
 * @param maxresult 長(zhǎng)度
 * @return
 */
 public list<person> getscrolldata(int offset,int maxresult){
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 //cursor cursor = sqldb.rawquery("select * from person order by personid asc limit ?,?", new string[]{string.valueof(offset),string.valueof(maxresult)});
 //第一個(gè)參數(shù)是表名;第二個(gè)參數(shù)是查詢(xún)顯示的字段,null時(shí)默認(rèn)查詢(xún)顯示所有字段;
 //第三個(gè)參數(shù)是where查詢(xún)條件占位符;第四個(gè)是占位符對(duì)應(yīng)的值;
 //第五個(gè)參數(shù)是group by條件;第六個(gè)是having條件;第七個(gè)是order by條件
 //第八個(gè)參數(shù)是limit ?,? 條件
 cursor cursor = sqldb.query("person", null, null, null, null, null, "personid",offset+","+maxresult);
 int personid;
 string name;
 string phone;
 int amount;
 person person = null;
 list<person> persons = new arraylist<person>();
 while(cursor.movetonext()){
  personid = cursor.getint(cursor.getcolumnindex("personid"));
  name = cursor.getstring(cursor.getcolumnindex("name"));
  phone = cursor.getstring(cursor.getcolumnindex("phone"));
  amount = cursor.getint(cursor.getcolumnindex("amount"));
  person = new person(personid,name,phone,amount);
  persons.add(person);
 }
 cursor.close();
 return persons;
 }
 /**
 * 返回總記錄數(shù)
 * @return
 */
 public long getcount(){
 sqlitedatabase sqldb = dboperatehelper.getreadabledatabase();
 //cursor cursor = sqldb.rawquery("select count(*) from person", null);
 //第一個(gè)參數(shù)是表名;第二個(gè)參數(shù)是查詢(xún)顯示的字段,null時(shí)默認(rèn)查詢(xún)顯示所有字段;
 //第三個(gè)參數(shù)是where查詢(xún)條件占位符;第四個(gè)是占位符對(duì)應(yīng)的值;
 //第五個(gè)參數(shù)是group by條件;第六個(gè)是having條件;第七個(gè)是order by條件
 cursor cursor = sqldb.query("person", new string[]{"count(*)"}, null, null, null, null, null);
 //該查詢(xún)語(yǔ)句值返回一條語(yǔ)句
 cursor.movetofirst();
 long result = cursor.getlong(0);
 cursor.close();
 return result;
 }
}

十二、personservicetest.java 單元測(cè)試類(lèi)源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example.test;
import java.util.list;
import com.example.domain.person;
import com.example.service.dboperatehelper;
import com.example.service.personservice;import android.test.androidtestcase;
import android.util.log;
public class personservicetest extends androidtestcase {
 public void testcreatedb() throws exception{
 dboperatehelper dbhelper = new dboperatehelper(getcontext());
 dbhelper.getwritabledatabase();
 }
 public void testsave() throws exception{
 personservice ps = new personservice(getcontext());
 for(int i=1;i<=100;i++){
  person person = new person();
  person.setname("我是"+i);
  person.setphone(string.valueof(long.parselong("18888888800")+i));
  ps.save(person);
  log.i("personservice",person.tostring());
 }
 }
 public void testdelete() throws exception{
 personservice ps = new personservice(getcontext());
 ps.delete(10);
 }
 public void testupdate() throws exception{
 personservice ps = new personservice(getcontext());
 ps.update(new person(1,"xiaopang","18887654321",0));
 }
 public void testfind() throws exception{
 personservice ps = new personservice(getcontext());
 person person = ps.find(1);
 log.i("personservice", person.tostring());
 }
 public void testgetscrolldata() throws exception{
 personservice ps = new personservice(getcontext());
 list<person> persons = ps.getscrolldata(3, 5);
 for(person person:persons){
  log.i("personservice",person.tostring());
 }
 }
 public void testgetcount() throws exception{
 personservice ps = new personservice(getcontext());
 long count = ps.getcount();
 log.i("personservice",count.tostring());
 }
 public void testupdateamount() throws exception{
 personservice ps = new personservice(getcontext());
 person person1 = ps.find(1);
 person person2 = ps.find(2);
 person1.setamount(100);
 person2.setamount(100);
 ps.update(person1);
 ps.update(person2);
 }
 public void testpayment() throws exception{
 personservice ps = new personservice(getcontext());
 ps.payment();
 }
}

十三、otherpersonservicetest 單元測(cè)試類(lèi)源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.example.test;
import java.util.list;
import com.example.domain.person;
import com.example.service.dboperatehelper;
import com.example.service.otherpersonservice;
import android.test.androidtestcase;
import android.util.log;
public class otherpersonservicetest extends androidtestcase {
 public void testcreatedb() throws exception{
 dboperatehelper dbhelper = new dboperatehelper(getcontext());
 dbhelper.getwritabledatabase();
 }
 public void testsave() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 for(int i=1;i<=100;i++){
  person person = new person();
  person.setname("我是"+i);
  person.setphone(string.valueof(long.parselong("18888888800")+i));
  ps.save(person);
  log.i("personservice",person.tostring());
 }
 }
 public void testdelete() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 ps.delete(10);
 }
 public void testupdate() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 ps.update(new person(1,"xiaopang","18887654321",0));
 }
 public void testfind() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 person person = ps.find(1);
 log.i("personservice", person.tostring());
 }
 public void testgetscrolldata() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 list<person> persons = ps.getscrolldata(3, 5);
 for(person person:persons){
  log.i("personservice",person.tostring());
 }
 }
 public void testgetcount() throws exception{
 otherpersonservice ps = new otherpersonservice(getcontext());
 long count = ps.getcount();
 log.i("personservice",count.tostring());
 }
}

十四、注意事項(xiàng)以及相關(guān)知識(shí)點(diǎn):

1、掌握sqlite數(shù)據(jù)庫(kù)如何創(chuàng)建數(shù)據(jù)庫(kù)、建立表、維護(hù)字段等操作
繼承sqliteopenhelper類(lèi),構(gòu)造函數(shù)調(diào)用父類(lèi)構(gòu)造函數(shù)創(chuàng)建數(shù)據(jù)庫(kù),利用oncreate創(chuàng)建表,利用onupgrade更新表字段信息

2、掌握sqlite數(shù)據(jù)庫(kù)如何增、刪、改、查以及分頁(yè)
取得sqlitedatabase的實(shí)例,然后調(diào)用該實(shí)例的方法可以完成上述操作
sqlitedatabase提供兩種操作上述功能的方式:一是直接調(diào)用execsql書(shū)寫(xiě)sql語(yǔ)句,另一種是通過(guò)insert、update、delete、query等方法來(lái)傳值來(lái)拼接sql,前一種適合熟練掌握sql 語(yǔ)句的

3、對(duì)需要數(shù)據(jù)同步的處理請(qǐng)?zhí)砑邮聞?wù)處理,熟悉事務(wù)的處理方式

4、了解各個(gè)方法參數(shù)的意義以及傳值

5、掌握l(shuí)istview顯示后臺(tái)數(shù)據(jù)的使用方法
simpleadapter、simplecursoradapter以及自定義適配器的使用,以及onitemclicklistener取值時(shí)各個(gè)適配器返回值的區(qū)別以及取值方法

6、多學(xué)、多記、多練、多思,加油!

希望本文所述對(duì)大家android程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本aaa大片 | 日本不卡在线观看免费v | 网站色小妹| 强制高h | 冰山美人调教耻辱h | 91短视频版高清在线观看免费 | 欧美ggg666 | 欧洲破处 | 女性性色生活片免费观看 | 日本激情小说 | 国产小视频网站 | 女王厕便器vk | 深夜免费在线视频 | 美女脱一光二净的视频 | 三级理论在线观看 | 高清视频在线播放 | 羞羞私人影院可以直接免费观影吗 | 天堂成人在线观看 | 国产亚洲自愉自愉 | 国产女主播福利在线 | 果冻传媒第一二三专区 | 亚洲第一成年免费网站 | 欧美日韩国产一区二区三区伦 | 国产欧美又粗又猛又爽老 | 国产成人一区二区三区小说 | 国产精品四虎在线观看免费 | 无码射肉在线播放视频 | 男男playh片在线观看 | 暴露狂婷婷医院暴露tx | 动漫精品一区二区三区3d | www.色老板| 国产精品麻豆99久久 | 513热点| 成人亚洲精品一区 | free性丰满hd性欧美厨房 | 亚洲欧美日韩中文高清一 | 天天躁夜夜躁很很躁 | 久久AV国产麻豆HD真实乱 | 非洲一级毛片又粗又长aaaa | 精品视频手机在线观看免费 | 白丝憋尿 |