【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{
13 14     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");
29 30 //数据库第一次创建时onCreate方法会被调用,我们可以执行创建表的语句,当系统发现版本变化之后,会调用onUpgrade方法,我们可以执行修改表结构等语句
31 32 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 }
37 38 }

数据库业务操作 – DBManager.java

001 002 003 java.util.ArrayList;
004 java.util.List;
005 006 android.content.ContentValues;
007 008 android.database.Cursor;
009 android.database.sqlite.SQLiteDatabase;
010 011 012 013 *DBManager是建立在DBHelper之上,封装了常用的业务方法
014 * @author bixiaopeng 2013-2-16 下午3:06:26
015 016 DBManager {
017 018 private DBHelper       helper;
019 SQLiteDatabase db;
020 021 DBManager(Context context){
022 helper = new DBHelper(context);
023 db = helper.getWritableDatabase();
024 025 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 045 046 047 /**
048 * @param _id
049 * @param name
050 * @param age
051 * @param website
052 * @param weibo
053 054 add(_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 = ContentValues();
057 // cv.put("_id",_id);
058 cv.put("name"
059 "age"060 "website"
061 "weibo"062 db.insert(DBHelper.DB_TABLE_NAME,cv);
063 + age + + website + + weibo);
064
065 066 067 * 通过name来删除数据
068 069 070 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 077 078 079 080 * 清空数据
081 082 clearData() {
083 ExecSQL("DELETE FROM info"084 "clear data"085 086 087 088 * 通过名字查询信息,返回所有的数据
089 *
090 * @param name
091 092 ArrayList<MemberInfo> searchData(String name) {
093 String sql = "SELECT * FROM info WHERE name =" "'" + name + "'"094 return ExecSQLForMemberInfo(sql);
095 096 097 ArrayList<MemberInfo> searchAllData() {
098 "SELECT * FROM info"099 ExecSQLForMemberInfo(sql);
100 101 102 103 * 通过名字来修改值
104 105 * @param raw
106 * @param rawValue
107 * @param whereName
108 109 updateData(String raw,String rawValue,String whereName) {
110 "UPDATE info SET " + raw + " =" " " + rawValue + " WHERE name =" + whereName
111                      112 ExecSQL(sql);
113 114 115 116 117 * 执行SQL命令返回list
118 119 * @param sql
120 * @return
121 122 ArrayList<MemberInfo> ExecSQLForMemberInfo(String sql) {
123 ArrayList<MemberInfo> list = ArrayList<MemberInfo>();
124 Cursor c = ExecSQLForCursor(sql);
125 while (c.moveToNext()) {
126 MemberInfo info = 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);
133 134 c.close();
135 list;
136 137 138 139 * 执行一个SQL语句
140 141 142 143 ExecSQL(String sql) {
144 145 db.execSQL(sql);
146 "execSql: "
147 catch (Exception e) {
148 Log.e("ExecSQL Exception"
149 e.printStackTrace();
150 151 152 153 154 * 执行SQL,返回一个游标
155 156

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


SQLite架构简单,又有Json计算能力,有时会承担Json文件/RESTful的计算功能,但SQLite不能直接解析Json文件/RESTful,需要用Java代码硬写,或借助第三方类库,最后再拼成insert语句插入数据表,代码非常繁琐,这里就不展示了。参考前面的代码可知,入库的过程比较麻烦,不能只用SQL,还要借助Java或命令行。SPL是现代的数据计算语言,属于简化的面向对象的语言风格,有对象的概念,可以用点号访问属性并进行多步骤计算,但没有继承重载这些内容,不算彻底的面向对象语言。...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。本教程将告诉您如何使用 SQLite 编程,并让你迅速上手。.................................
安卓开发,利用SQLite实现登陆注册功能
相比大多数数据库而言,具有等优势,广泛应用于、等领域。
有时候,一个项目只有一个数据库,比如只有SQLite,或者MySQL数据库,那么我们只需要使用一个固定的数据库即可。但是一个项目如果写好了,有多个用户使用,但是多个用户使用不同的数据库,这个时候,我们就需要把软件设计成可以连接多个数据库的模式,用什么数据库,就配置什么数据库即可。4.Users实体类,这个实体类要和数据库一样的,形成一一对应的关系。11.Sqlite数据库,需要在代码里面创建数据库,建立表,再建立数据。8.我们开启MySQL数据库,然后进行调试,看程序的结果。2.安装SqlSugar。
基于Android的背单词软件,功能强大完整。
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统。说白了就是使用起来轻便简单,
Android的简单购物车案例
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库...
Qt设计较为美观好看的登录注册界面(包含SQLite数据库以及TCP通信的应用)
SQLite是用C语言开发的跨平台小型数据库,可嵌入其他开发语言,也可在单机执行。SPL是用Java开发的跨平台的数据计算语言,可嵌入Java,可在单机执行,可以数据计算服务的形式被远程调用。两者的代码都是解释执行的。...
新建库.openDATA_BASE;新建表createtableLIST_NAME(DATA);语法:NAME关键字...<用逗号分割>删除表droptableNAME;查看表.schema查看表信息新建数据insertintoLIST_NAMEvalues();语法:CLASS,PARAMETER...,CLASS是类别,PARAMETER是参数<用逗号分割新建的
importsqlite3classDemo01:def__init__(self):self.conn=sqlite3.connect("sql_demo_001.db")self.cursor1=self.conn.cursor()self.cursor1.execute("select*fromtable_001wherename=?andid=?",('ssss&#0
 在客户端配置文件<configuration>节点下,添加:<connectionStrings>      <add name="localdb" connectionString="Data Source=config/local.db;Version=3;UseUTF16Encoding=True;" providerName="System.Data.SQLite.SQLiteFactory"/&g
提到锁就不得不说到死锁的问题,而SQLite也可能出现死锁。下面举个例子:连接1:BEGIN(UNLOCKED)连接1:SELECT...(SHARED)连接1:INSERT...(RESERVED)连接2:BEGIN(UNLOCKED)连接2:SELECT...(SHARED)连接1:COMMIT(PENDING,尝试获取EXCLUSIVE锁,但还有SHARED锁未释放,返回SQLITE_BUSY)连接2:INSERT...
SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成。Python就内置了SQLite3,所以,在Python中使用SQLite,不需要安装任何东西,直接使用。在使用SQLite前,我们先要搞清楚几个概念:表
设计思想————首先要确定有几个页面、和每个页面的大致布局由于是入门,我也是学习了不是很长的时间,所以项目比较low。。。。第一个页面,也就是打开APP的首页面:今天这个博客,先实现添加功能!:首先对主界面进行布局:其中activity_main.xml的代码为<?xmlversion="1.0"encoding="