本文实例讲述了android中数据库常见操作。分享给大家供大家参考,具体如下:
android中数据库操作是非常常见了,我们会经常用到,操作的方法也有很多种形式,这里我就把最常见的两种形式记录下来了,以备以后用到方便查看。我就不写注释和解释了,因为android数据库的操作和其它数据库操作本质上都是一样的,大同小异。需要的一些基本解释都在代码中,直接上代码了。
简单的代码文件目录:
首先这个类是数据库帮助类,dbhelper.java,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package net.loonggg.db; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; /** * 数据库帮助类,继承android自带的sqliteopenhelper 主要用于数据库的创建与更新 * * @author loonggg * */ public class dbhelper extends sqliteopenhelper { public dbhelper(context context) { super (context, dbinfo.db.db_name, null , dbinfo.db.db_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(dbinfo.table.user_info_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql(dbinfo.table.user_info_drop); oncreate(db); } } |
其次是数据库信息类,dbinfo.java,代码如下:
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
|
package net.loonggg.db; /** * 数据库信息类,主要是保存一些数据库的版本,名字,及数据库表的创建语句和表的信息等,通过这个类记录,方便操作 * * @author loonggg * */ public class dbinfo { /** * 数据库信息 * * @author loonggg * */ public static class db { // 数据库名称 public static final string db_name = "test.db" ; // 数据库的版本号 public static final int db_version = 1 ; } /** * 数据库表的信息 * * @author loonggg * */ public static class table { public static final string user_info_tb_name = "user_table" ; public static final string user_info_create = "create table if not exists " + user_info_tb_name + " ( _id integer primary key,userid text,username text)" ; public static final string user_info_drop = "drop table" + user_info_tb_name; } } |
再次是数据库操作类,dbservice.java,代码如下:
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
|
package net.loonggg.service; import java.util.arraylist; import java.util.hashmap; import java.util.list; import net.loonggg.db.dbhelper; import net.loonggg.db.dbinfo.table; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; /** * 数据库操作类,这个类主要的功能是:存放数据库操作的一些方法 这里有一些例子:包含数据库的增删改查,分别有两种方法的操作,各有优缺点,都在解释中 * * @author loonggg * */ public class dbservice { private dbhelper dbhelper = null ; public dbservice(context context) { dbhelper = new dbhelper(context); } /** * 添加一条记录到数据库 * * @param id * @param name */ public void add(string id, string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); // 不好之处:无返回值,无法判断是否插入成功 db.execsql( "insert into user_table (userid,username) values (?,?)" , new object[] { id, name }); db.close(); } public long addandroid(string id, string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); contentvalues values = new contentvalues(); values.put( "userid" , id); values.put( "username" , name); // 好处:有返回值 long result = db.insert(table.user_info_tb_name, null , values); // 返回值是插入的是第几行,大于0代表添加成功 db.close(); return result; } /** * 查询某条记录是否存在 * * @param name * @return */ public boolean find(string name) { sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = db.rawquery( "select * from user_table where username = ?" , new string[] { name }); boolean result = cursor.movetonext(); db.close(); return result; } public boolean findandroid(string name) { sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = db.query(table.user_info_tb_name, null , "username = ?" , new string[] { name }, null , null , null ); boolean result = cursor.movetonext(); // true代表查找到了 db.close(); return result; } /** * 修改一条记录 * * @param id * @param name */ public void update(string id, string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); // 缺点无返回值 db.execsql( "update user_table set username = ? where userid = ?" , new object[] { name, id }); db.close(); } public int updateandroid(string id, string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); contentvalues values = new contentvalues(); values.put( "username" , name); // 返回值大于0代表修改更新成功 int result = db.update(table.user_info_tb_name, values, "userid = ?" , new string[] { id }); db.close(); return result; } /** * 删除一条记录 * * @param name */ public void delete(string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); db.execsql( "delete from user_table where username = ?" , new string[] { name }); db.close(); } public int deleteandroid(string name) { sqlitedatabase db = dbhelper.getwritabledatabase(); int result = db.delete(table.user_info_tb_name, "username = ?" , new string[] { name }); // 返回值为受影响的行数,大于0代表成功 db.close(); return result; } /** * 返回所有的数据库信息 * * @return */ public list<hashmap<string, string>> findall() { list<hashmap<string, string>> list = null ; sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = db.rawquery( "select * from user_table" , null ); if (cursor.getcount() > 0 ) { list = new arraylist<hashmap<string, string>>(); while (cursor.movetonext()) { string id = cursor.getstring(cursor.getcolumnindex( "userid" )); string name = cursor.getstring(cursor .getcolumnindex( "username" )); hashmap<string, string> map = new hashmap<string, string>(); map.put( "id" , id); map.put( "name" , name); list.add(map); } } cursor.close(); db.close(); return list; } public list<hashmap<string, string>> findallandroid() { list<hashmap<string, string>> list = null ; sqlitedatabase db = dbhelper.getreadabledatabase(); cursor cursor = db.query(table.user_info_tb_name, new string[] { "userid" , "username" }, null , null , null , null , null ); if (cursor.getcount() > 0 ) { list = new arraylist<hashmap<string, string>>(); while (cursor.movetonext()) { string id = cursor.getstring(cursor.getcolumnindex( "userid" )); string name = cursor.getstring(cursor .getcolumnindex( "username" )); hashmap<string, string> map = new hashmap<string, string>(); map.put( "id" , id); map.put( "name" , name); list.add(map); } } cursor.close(); db.close(); return list; } } |
最后是mainactivity,简单的调用了一下,这些操作,代码如下:
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
|
package net.loonggg.test; import net.loonggg.service.dbservice; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends activity { private button queryone; private button insert; private button update; private button delete; private button findall; private dbservice service; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); queryone = (button) findviewbyid(r.id.queryone); insert = (button) findviewbyid(r.id.insert); update = (button) findviewbyid(r.id.update); delete = (button) findviewbyid(r.id.delete); findall = (button) findviewbyid(r.id.findall); queryone.setonclicklistener( new buttonlistener()); insert.setonclicklistener( new buttonlistener()); update.setonclicklistener( new buttonlistener()); delete.setonclicklistener( new buttonlistener()); findall.setonclicklistener( new buttonlistener()); service = new dbservice( this ); } class buttonlistener implements view.onclicklistener { @override public void onclick(view v) { switch (v.getid()) { case r.id.queryone: // service.find("loonggg"); service.findandroid( "loonggg" ); break ; case r.id.insert: // service.add("1", "loonggg"); service.addandroid( "2" , "heihei" ); break ; case r.id.update: // service.update("1", "timmy"); service.updateandroid( "1" , "haha" ); break ; case r.id.delete: // service.delete("timmy"); service.deleteandroid( "heihei" ); break ; case r.id.findall: // service.findall(); service.findallandroid(); break ; default : break ; } } } } |
还有mainactivity对应的布局文件,activity_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
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <button android:id= "@+id/queryone" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "查询一条记录" /> <button android:id= "@+id/insert" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "添加" /> <button android:id= "@+id/update" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "修改" /> <button android:id= "@+id/delete" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "删除" /> <button android:id= "@+id/findall" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "查询全部" /> </linearlayout> |
到这里就介绍完了,这些代码并不高深,之所以记录下来,是留着以后用到的时候方便查看,当然这个代码对于初学者,还是非常有帮助的。
希望本文所述对大家android程序设计有所帮助。