【Android数据存储】SQLite使用实例(附源码)

前端之家收集整理的这篇文章主要介绍了【Android数据存储】SQLite使用实例(附源码)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实例: 会员信息管理

功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员


 

数据库基类 – DBHelper.java

01 package com.wirelessqa.sqlite;
02  
03 import android.content.Context;
04 android.database.sqlite.sqliteDatabase;
05 android.database.sqlite.sqliteOpenHelper;
06 android.util.Log;
07  
08 /**
09  * DBHelper继承了sqliteOpenHelper,作为维护和管理数据库的基类
10 * @author bixiaopeng 2013-2-16 下午3:05:52
11 */
12 public class DBHelper  extends sqliteOpenHelper{
@H_403_195@14
13     static final String DB_NAME = "wirelessqa.db";
15 String DB_TABLE_NAME = "info";
16 private final int DB_VERSION=117 public DBHelper(Context context) {
18         //Context context,String name,CursorFactory factory,int version
19 //factory输入null,使用默认值
20         super(context,DB_NAME, null,DB_VERSION);
21     }
22     //数据第一次创建的时候会调用onCreate
23     @Override
24 void onCreate(sqliteDatabase db) {      
25 //创建表
26           db.execsql("CREATE TABLE IF NOT EXISTS info" 
27                     "(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR,age INTEGER,website STRING,weibo STRING)");
28 Log.i(WirelessQA.TAG, "create table");
@H_403_195@30
29 //数据库第一次创建时onCreate方法会被调用,我们可以执行创建表的语句,当系统发现版本变化之后,会调用onUpgrade方法,我们可以执行修改表结构等语句
@H_403_195@32
31 onUpgrade(sqliteDatabase db,monospace!important; font-size:1em!important; border:0px!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; min-height:auto!important">oldVersion,monospace!important; font-size:1em!important; border:0px!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; min-height:auto!important">newVersion) {
33 //在表info中增加一列other
34 //db.execsql("ALTER TABLE info ADD COLUMN other STRING"); 
35         Log.i("WIRELESSQA""update sqlite "+oldVersion+"---->"+newVersion);
36 }
@H_403_195@38
37 }

数据库业务操作 – DBManager.java

001 @H_403_195@002 @H_403_195@003 java.util.ArrayList;
004 java.util.List;
@H_403_195@006
005 android.content.ContentValues;
@H_403_195@008
007 android.database.Cursor;
009 android.database.sqlite.sqliteDatabase;
010 @H_403_195@011 @H_403_195@012 @H_403_195@013 *DBManager是建立在DBHelper之上,封装了常用的业务方法
014 * @author bixiaopeng 2013-2-16 下午3:06:26
@H_403_195@016
015 DBManager {
@H_403_195@018
017 private DBHelper       helper;
019 sqliteDatabase db;
020 @H_403_195@021 DBManager(Context context){
022 helper = new DBHelper(context);
023 db = helper.getWritableDatabase();
024 @H_403_195@025 @H_403_195@026     027      * 向表info中增加一个成员信息
028 *
029 * @param memberInfo
030 */
031 add(List<MemberInfo> memberInfo) {
032 db.beginTransaction();// 开始事务
033 try {
034             for (MemberInfo info : memberInfo) {
035                 "------add memberInfo----------"036 "/" + info.age + + info.website + + info.weibo);
037                 // 向表info中插入数据
038 "INSERT INTO info VALUES(null,?,?)"Object[] { info.name,info.age,info.website,
039                         info.weibo });
040             041 db.setTransactionSuccessful();// 事务成功
042 } finally {
043 db.endTransaction();// 结束事务
044 @H_403_195@045 @H_403_195@046 @H_403_195@047 /**
048 * @param _id
049 * @param name
050 * @param age
051 * @param website
052 * @param weibo
@H_403_195@054
053 add(@H_502_1051@_id,monospace!important; font-size:1em!important; border:0px!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; min-height:auto!important">age,String website,String weibo) {
055 "------add data----------"056 ContentValues cv = @H_502_1051@ContentValues();
057 // cv.put("_id",_id);
058 cv.put("name" @H_403_195@065 @H_403_195@066 @H_403_195@067
059 "age"060 "website"
061 "weibo"062 db.insert(DBHelper.DB_TABLE_NAME,cv);
063 + age + + website + + weibo);
064
* 通过name来删除数据
068 @H_403_195@069 @H_403_195@070 @H_403_195@071 delData(String name) {
072 // Execsql("DELETE FROM info WHERE name ="+"'"+name+"'");
073 String[] args = { name };
074 db.delete(DBHelper.DB_TABLE_NAME,monospace!important; font-size:1em!important; color:blue!important; border:0px!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; min-height:auto!important">"name=?"
075 "delete data by " + name);
076 @H_403_195@077 @H_403_195@078 @H_403_195@079 @H_403_195@080 * 清空数据
@H_403_195@082
081 clearData() {
@H_403_195@086 @H_403_195@087 @H_403_195@088
083 Execsql("DELETE FROM info"084 "clear data"085 * 通过名字查询信息,返回所有的数据
089 *
090 * @param name
@H_403_195@092
091 ArrayList<MemberInfo> searchData(@H_502_1051@String name) {
093 String sql"SELECT * FROM info WHERE name =" "'" + name + "'"094 return ExecsqlForMemberInfo(sql);
@H_403_195@096 @H_403_195@097
095 ArrayList<MemberInfo> searchAllData() {
098 "SELECT * FROM info"099 ExecsqlForMemberInfo(sql);
100 @H_403_195@101 @H_403_195@102 @H_403_195@103 * 通过名字来修改
104 @H_403_195@105 * @param raw
106 * @param rawValue
107 * @param whereName
108 @H_403_195@109 updateData(String raw,String rawValue,String whereName) {
110 "UPDATE info SET " + raw + " =" " " + rawValue + " WHERE name =" + whereName
111                      112 Execsql(sql);
113 sql);
114 @H_403_195@115 @H_403_195@116 @H_403_195@117 * 执行sql命令返回list
118 @H_403_195@119 * @param sql
120 * @return
@H_403_195@122
121 ArrayList<MemberInfo> ExecsqlForMemberInfo(String sql) {
123 ArrayList<MemberInfo> list = @H_502_1051@ArrayList<MemberInfo>();
124 Cursor c = ExecsqlForCursor(sql);
125 while (c.moveToNext()) {
126 MemberInfo info = @H_502_1051@MemberInfo();
127 info._id = c.getInt(c.getColumnIndex("_id"));
128 info.name = c.getString(c.getColumnIndex());
129 info.age = c.getInt(c.getColumnIndex(130 info.website = c.getString(c.getColumnIndex(131 info.weibo = c.getString(c.getColumnIndex(132 list.add(info);
@H_403_195@134
133 c.close();
135 list;
136 @H_403_195@137 @H_403_195@138 @H_403_195@139 * 执行一个sql语句
140 @H_403_195@141 @H_403_195@142 @H_403_195@143 Execsql(String sql) {
144 @H_403_195@145 db.execsql(sql);
146 "execsql: "sql);
147 catch (Exception e) {
148 Log.e("Execsql Exception"
149 e.printStackTrace();
150 @H_403_195@151 @H_403_195@152 @H_403_195@153 @H_403_195@154 * 执行sql,返回一个游标
@H_403_195@156
155

猜你在找的Sqlite相关文章