Android包含了常用于嵌入式系統(tǒng)的SQLite,免去了開發(fā)者自己移植安裝的功夫。SQLite 支持多數(shù) SQL92 標(biāo)準(zhǔn),很多常用的SQL命令都能在SQLite上面使用,除此之外Android還提供了一系列自定義的方法去簡(jiǎn)化對(duì)SQLite數(shù)據(jù)庫(kù)的操作。不過有跨平臺(tái)需求的程序就建議使用標(biāo)準(zhǔn)的SQL語(yǔ)句,畢竟這樣容易在多個(gè)平臺(tái)之間移植。
本文主要講解了SQLite的基本用法,如:創(chuàng)建數(shù)據(jù)庫(kù),使用SQL命令查詢數(shù)據(jù)表、插入數(shù)據(jù),關(guān)閉數(shù)據(jù)庫(kù),以及使用GridView實(shí)現(xiàn)了一個(gè)分頁(yè)欄(關(guān)于GridView的用法),用于把數(shù)據(jù)分頁(yè)顯示。
分頁(yè)欄的pagebuttons.xml的源碼如下:
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_height = "wrap_content" android:paddingBottom = "4dip" android:layout_width = "fill_parent" > < TextView android:layout_width = "wrap_content" android:layout_below = "@+id/ItemImage" android:layout_height = "wrap_content" android:text = "TextView01" android:layout_centerHorizontal = "true" android:id = "@+id/ItemText" > </ TextView > </ RelativeLayout > |
main.xml的源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < Button android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:id = "@+id/btnCreateDB" android:text = "創(chuàng)建數(shù)據(jù)庫(kù)" ></ Button > < Button android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:text = "插入一串實(shí)驗(yàn)數(shù)據(jù)" android:id = "@+id/btnInsertRec" ></ Button > < Button android:layout_height = "wrap_content" android:id = "@+id/btnClose" android:text = "關(guān)閉數(shù)據(jù)庫(kù)" android:layout_width = "fill_parent" ></ Button > < EditText android:text = "@+id/EditText01" android:id = "@+id/EditText01" android:layout_width = "fill_parent" android:layout_height = "256dip" ></ EditText > < GridView android:id = "@+id/gridview" android:layout_width = "fill_parent" android:layout_height = "32dip" android:numColumns = "auto_fit" android:columnWidth = "40dip" ></ GridView > </ LinearLayout > |
本文程序源碼如下:
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package com.testSQLite; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.SimpleAdapter; public class testSQLite extends Activity { /** Called when the activity is first created. */ Button btnCreateDB, btnInsert, btnClose; EditText edtSQL; //顯示分頁(yè)數(shù)據(jù) SQLiteDatabase db; int id; //添加記錄時(shí)的id累加標(biāo)記,必須全局 static final int PageSize= 10 ; //分頁(yè)時(shí),每頁(yè)的數(shù)據(jù)總數(shù) private static final String TABLE_NAME = "stu" ; private static final String ID = "id" ; private static final String NAME = "name" ; SimpleAdapter saPageID; // 分頁(yè)欄適配器 ArrayList<HashMap<String, String>> lstPageID; // 分頁(yè)欄的數(shù)據(jù)源,與PageSize和數(shù)據(jù)總數(shù)相關(guān) @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); btnCreateDB = (Button) this .findViewById(R.id.btnCreateDB); btnCreateDB.setOnClickListener( new ClickEvent()); btnInsert = (Button) this .findViewById(R.id.btnInsertRec); btnInsert.setOnClickListener( new ClickEvent()); btnClose = (Button) this .findViewById(R.id.btnClose); btnClose.setOnClickListener( new ClickEvent()); edtSQL=(EditText) this .findViewById(R.id.EditText01); GridView gridview = (GridView) findViewById(R.id.gridview); //分頁(yè)欄控件 // 生成動(dòng)態(tài)數(shù)組,并且轉(zhuǎn)入數(shù)據(jù) lstPageID = new ArrayList<HashMap<String, String>>(); // 生成適配器的ImageItem <====> 動(dòng)態(tài)數(shù)組的元素,兩者一一對(duì)應(yīng) saPageID = new SimpleAdapter(testSQLite. this , // 沒什么解釋 lstPageID, // 數(shù)據(jù)來源 R.layout.pagebuttons, //XML實(shí)現(xiàn) new String[] { "ItemText" }, new int [] { R.id.ItemText }); // 添加并且顯示 gridview.setAdapter(saPageID); // 添加消息處理 gridview.setOnItemClickListener( new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LoadPage(arg2); //根據(jù)所選分頁(yè)讀取對(duì)應(yīng)的數(shù)據(jù) } }); } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { if (v == btnCreateDB) { CreateDB(); } else if (v == btnInsert) { InsertRecord( 16 ); //插入16條記錄 RefreshPage(); } else if (v == btnClose) { db.close(); } } } /* * 讀取指定ID的分頁(yè)數(shù)據(jù) * SQL:Select * From TABLE_NAME Limit 9 Offset 10; * 表示從TABLE_NAME表獲取數(shù)據(jù),跳過10行,取9行 */ void LoadPage(int pageID) { String sql= "select * from " + TABLE_NAME + " Limit "+String.valueOf(PageSize)+ " Offset " +String.valueOf(pageID*PageSize); Cursor rec = db.rawQuery(sql, null); setTitle("當(dāng)前分頁(yè)的數(shù)據(jù)總數(shù):"+String.valueOf(rec.getCount())); // 取得字段名稱 String id="codetool">
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。 延伸 · 閱讀
精彩推薦
|