Android之SQLite的使用

来源:互联网 发布:一目十行训练软件 编辑:程序博客网 时间:2024/05/05 12:02

最近自己在写一个小的app,用到了SQLite数据库,就简单探讨一下SQLite数据库在android 开发中的用法。数据库存储数据有很明显的优势,数据条理清晰,易于提取和存储,安全性也有一定的保证。



SQLite 的使用方法:

首先我们需要一个Helper,它继承于SQLiteOpenHelper

public class DBHelper extends SQLiteOpenHelper {    private static final String DATABASE_NAME = "inote.db";  //数据库的名字    private static final int DATABASE_VERSION = 2;           //数据库的版本号    private static final String CREATE_TABLE_SQL =           //创建的表                    "create table if not exists note"+                    "(nCTime varchar not null," +                    "nId integer," +                    "nSTime varchar not null," +                    "nTitle varchar not null," +                    "nContent varchar," +                    "nEngency varchar," +                    "nPointX integer not null," +                    "nPointY integer not null,"+                    "nAId Integer not null default 0, primary key(nCTime,nId))";    private static final String VERSION_TWO = "alter table note add nAId Integer default 0";    public DBHelper(Context context) {        super(context, DATABASE_NAME, null, DATABASE_VERSION);   //指明了第一次创建时的数据库名字,版本号    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL(CREATE_TABLE_SQL);                            //这里创建表    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  //当数据库版本号变化时执行        db.execSQL(VERSION_TWO);    }}

我们先来说一下这个数据库的版本号,开始之初我也没有理解这个是干嘛的,后来当我需要在原来的表中增加新的一列时才发想,我到底怎么增加一列呢?
原来我们想要保存原来数据库的数据又要增加一列,只需要修改下这里的数据库版本号比原来的大,并写好增加列的sql语句,打包好apk安装到原来已经安装的手机中,就可以了,系统会检测到这个版本号的变大,并执行onUpgrade函数,此时你的 数据库更新操作就增加了一列。

Helper创建好,我们就可以准备操作数据库了,这时我们在创建一个DBManager的工具类,用来增删更改数据。。
package csu.scrovor.cn.inote.Util.SQLite;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import java.util.Date;import java.util.ArrayList;import java.util.List;import csu.scrovor.cn.inote.Beans.Engency;import csu.scrovor.cn.inote.Beans.NoteInfo;import csu.scrovor.cn.inote.Util.Utils;/** * Created by lenovo on 2016/10/28. */public class DBManager {    private SQLiteDatabase db;    private DBHelper helper;    private String insertSql = "insert into note values(?,?,?,?,?,?,?,?,?)";    private String updateSql = "update note set nSTime=?,nTitle=?,nContent=?,nEngency=? where nCTime=? and nId=?";    private String deleteSql = "delete from note where nCTime=? and nId=?";    public DBManager(Context context){        helper = new DBHelper(context);        db = helper.getWritableDatabase(); //通过此方法来获取可写的数据库实例    }        public void insert(NoteInfo noteInfo){        db.beginTransaction();    //开始事务        try{            db.execSQL(insertSql,new Object[]{                    noteInfo.getCdate(),                    noteInfo.getnId(),                    noteInfo.getDate().toString(),                    noteInfo.getKeyWord(),                    noteInfo.getContent(),                    noteInfo.getEngency().toString(),                    noteInfo.getPointX(),                    noteInfo.getPointY(),                    noteInfo.getaId()            });            db.setTransactionSuccessful();        }catch(Exception e){            e.printStackTrace();        }finally {            db.endTransaction();   //结束事务        }    }    public void close(){        db.close();    }}
这里说明下,所有对数据库中的数据进行修改的都要包在beginTransaction()和endTransaction()之中,这是为了防止并发导致的数据异常。

0 0
原创粉丝点击