SQLite是Android使用的轻量级的数据库,开发Android应用是对数据库的操作自然是必不可少。

Android提供了一个SQLiteOpenHelper类来可以很方便的操作数据库,

继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个方法。 
       onCreate: 当数据库被首次创建时执行该方法,一般将创建表等初始化操作在该方法中执行。 
       onUpgrade:当打开数据库时传入的版本号与当前的版本号不同时会调用该方法。

下面是我写的一个SQLite基本操作的demo。

主要包含两个java类——

DBUtil类,继承自SQLiteOpenHelper,用以实现各种操作功能:

 package barry.android.db;

 import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; public class DBUtil extends SQLiteOpenHelper { private final static String DATABASE_NAME = "db2004";
private final static int DATABASE_VERSION = 1;
private static final String TABLE_NAME ="students";
private static final String FILED_1 = "name";
private static final String FILED_2 = "password"; public DBUtil(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
System.out.println("new DBUtil");
} @Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+TABLE_NAME+" ( "+FILED_1 +" TEXT, "+ FILED_2 +" TEXT );";
db.execSQL(sql);
System.out.println("oncreate创建表");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("onUpgrade删除表");
this.onCreate(db);
} /**
* 查询表中所有的数据
* @return
*/
public Cursor select(){
return this.getReadableDatabase()
.query(TABLE_NAME, null, null, null, null, null, null);
} /**
* 插入一条数据到表中
* @param name 字段一的值
* @param password 字段二的值
*/
public void insert(String name ,String password){
ContentValues cv = new ContentValues();
cv.put(FILED_1, name);
cv.put(FILED_2, password);
this.getWritableDatabase().insert(TABLE_NAME, null, cv);
this.getWritableDatabase().close();//关闭数据库对象
} /**
* 删除表中的若干条数据
* @param name 一个包含所有要删除数据的"name"字段的数组
*/
public void delete(String[] name){
String where = FILED_1+" = ?";
String[] whereValues = name;
this.getWritableDatabase().delete(TABLE_NAME, where, whereValues);
this.getWritableDatabase().close();
} /**
* 更新表中的数据(修改字段二"password")
* @param name 要更新的数据"name"字段值
* @param newPassword 新的"password"字段
*/
public void update(String name,String newPassword){
ContentValues cv = new ContentValues();
cv.put(FILED_2, newPassword);
String where =FILED_1+" = ?";
String[] whereValues= {name};
this.getWritableDatabase().update(TABLE_NAME, cv, where, whereValues);
this.getWritableDatabase().close();
} /**
* 清空表中的数据
*/
public void clean (){
this.getWritableDatabase().execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("clean删除表");
this.onCreate(this.getWritableDatabase());
this.getWritableDatabase().close();
}
}

以及调用DBUtil的Activity:

 package barry.android.db;

 import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle; public class Demo04_helperActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); DBUtil dbUtil = new DBUtil(this);
dbUtil.insert("周杰伦", "jaychou");
dbUtil.insert("韩寒", "twocolds");
dbUtil.insert("郭德纲", "yunhejiuxiao"); System.out.println("***********************************全部数据息");
printData(dbUtil); dbUtil.delete(new String[]{"周杰伦"}); System.out.println("***********************************删除'周杰伦'之后数据");
printData(dbUtil); dbUtil.update("郭德纲", "longtengsihai");;
System.out.println("***********************************修改‘郭德纲’的密码为'longtengsihai'");
printData(dbUtil); dbUtil.clean(); } private void printData(DBUtil dbUtil) {
Cursor cursor = dbUtil.select();
if(cursor.moveToFirst()){
System.out.println("当前表中的数据条数:"+cursor.getCount());
do{
System.out.println(cursor.getString(0)+cursor.getString(1));
}while(cursor.moveToNext());
}
cursor.close();
}
}

该程序所执行的操作为:

1.在创建一个名为"db2004"的数据库,(即DBUtil的“DATABASE_NAME”字段)。

2.当数据库被首次创建时执行DBUtil的onCreate方法,创建一张名为students的表,包含两个字段(name,password)。(即DBUtil的”TABLE_NAME、FILED_1、FILED_2”字段)。

3.往数据库中插入三条数据“周杰伦、韩寒、郭德纲”。然后.查询出表中所有数据并打印。

4.删除数据“周杰伦”。然后.查询出表中所有数据并打印。

5.将数据“郭德纲”的password修改为"longtengsihai"。然后.查询出表中所有数据并打印。

6.清除表中的所有数据,程序结束。

执行的结果为:

02-07 11:22:47.361: I/System.out(962): new DBUtil
02-07 11:22:47.490: I/System.out(962): ***********************************全部数据息
02-07 11:22:47.490: I/System.out(962): 当前表中的数据条数:3
02-07 11:22:47.500: I/System.out(962): 周杰伦jaychou
02-07 11:22:47.500: I/System.out(962): 韩寒twocolds
02-07 11:22:47.500: I/System.out(962): 郭德纲yunhejiuxiao
02-07 11:22:47.511: I/System.out(962): ***********************************删除'周杰伦'之后数据
02-07 11:22:47.540: I/System.out(962): 当前表中的数据条数:2
02-07 11:22:47.540: I/System.out(962): 韩寒twocolds
02-07 11:22:47.550: I/System.out(962): 郭德纲yunhejiuxiao
02-07 11:22:47.560: I/System.out(962): ***********************************修改‘郭德纲’的密码为'longtengsihai'
02-07 11:22:47.590: I/System.out(962): 当前表中的数据条数:2
02-07 11:22:47.590: I/System.out(962): 韩寒twocolds
02-07 11:22:47.590: I/System.out(962): 郭德纲longtengsihai
02-07 11:22:47.601: I/System.out(962): clean删除表
02-07 11:22:47.610: I/System.out(962): oncreate创建表

结果正确。

android SQLite数据库的基本操作的更多相关文章

  1. Android——SQLite/数据库 相关知识总结贴

    android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...

  2. Android Sqlite 数据库版本更新

      Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...

  3. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...

  4. Android sqlite数据库存取图片信息

    Android sqlite数据库存取图片信息 存储图片:bitmap private byte[] getIconData(Bitmap bitmap){ int size = bitmap.get ...

  5. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

  6. 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持

    IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...

  7. [Android] SQLite数据库之增删改查基础操作

        在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Un ...

  8. Android Sqlite数据库加密

    Android使用的是开源的SQLite数据库,数据库本身没有加密,加密思路通常有两个: 1. 对几个关键的字段使用加密算法,再存入数据库 2. 对整个数据库进行加密 SQLite数据库加密工具: 收 ...

  9. Android SQLite数据库使用

    在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...

随机推荐

  1. boost.xml_parser中文字符问题 (转)

    当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...

  2. SVN命令行更新代码

    命令列表 svn help查看帮助信息 Available subcommands: add auth blame (praise, annotate, ann) cat changelist (cl ...

  3. 基于Python的datetime模块和time模块源码阅读分析

    目录 1 前言  2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...

  4. org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.*.Paper.totalTime

    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:109) at o ...

  5. c++11 简明学习

    https://coolshell.cn/articles/5265.html http://www.cnblogs.com/me115/p/4800777.html#h29 https://chan ...

  6. CucumberPeople 1.3.2 发布

    CucumberPeople 网站: http://alterhu.github.io/CucumberPeople/ This eclipse plugin based on RubyMine ,a ...

  7. 修改QGIS来支持DPI为96的WMTS/WMS服务

    缘由 因为各种各种wmts地图客户端产品的标准的支持不一定是一致的,就像ArcGIS不同版本加载WMTS图层的时候计算的规则就有差别(米和经纬度之间转换系数的区别),导致会出现适应各个客户端而出的WM ...

  8. no accounts with itunes connect access

    有时候打包上传的时候 会遇见  no accounts with itunes connect access 的报错 原因主要如下: 1. 你没有被开发者管理员加入 itunes connect 权限 ...

  9. JS 全屏代码

    // 推断各种浏览器,找到正确的方法 function launchFullscreen(element) { if(element.requestFullscreen) { element.requ ...

  10. MySQL设置全局sql日志

     分别执行开启日志以及日志路径和日志文件名 SET GLOBAL general_log_file = '/var/lib/mysql/localhost.log';SET GLOBAL genera ...