SQLite

sqlite的增删查改CRUD

CRUD是指在做计算处理时的增加(Create)、读取(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。

1、获取Android定义的几个常用目录:
Context.getCacheDir()方法
用于获取/data/data//cache目录

Context.getFilesDir()方法
用于获取/data/data//files目录

Context.getExternalFilesDir()方法
可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据

Context.getExternalCacheDir()方法
可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据

2、执行SQL语句的步骤
A、获得SQLiteDatabase数据库对象
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(“path”, null);

B、执行Sql语句
db.execSQL(“delete from t”);//不需要返回结果

Cursor c = db.rawQuery(“select * from t where _id=? and name=?”,
new String[]{“1”, “lzh”});//获取查询结果
while(c.moveToNext()){
int id = c.getInt(0);//获取游标所在行的第0个字段的值
String name = c.getString(1); //获取游标所在行的第1上字段的值
}

C、关闭数据库
db.close();

3、两个工具软件
RootExplore:文件查看器
SQLite Editor:SQLite编辑器

实现:

/**
 * 数据库访问对象,以mydiary数据库diary表为例
 * @author trkj
 *
 */
public class DiaryDAO extends SQLiteOpenHelper {

    /**
     * 创建数据库
     * @param context
     * @param name
     * @param factory
     * @param version
     */
    public DiaryDAO(Context context) {
        super(context, "mydiary.db", null, 1);
    }

    /**
     * 创建表
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String schema = "create table diary("
                + "_id integer primary key autoincrement, "
                + "title, weather, dt, content)";
        // 执行SQL语句
        db.execSQL(schema);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //数据库升级时回调
    }

    /**
     * 插入日记
     * @param title
     * @param weather
     * @param content
     */
    public void insert(String title, String weather, 
            String content){
        String dt = new Date().toLocaleString();

        String sql = "insert into diary(title, weather, dt, content)" +
                " values(?,?,?,?)";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(sql, new String[]{title, weather, dt, content});
        db.close();
    }

    /**
     * 删除日记
     * @param id
     */
    public void delete(long id){
        String sql = "delete from diary where _id=?";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(sql, new String[]{id + ""});
        db.close();
    }

    /**
     * 修改日志
     * @param title
     * @param weather
     * @param content
     */
    public void update(String title, String weather, 
            String content, long id){
        String sql = "update diary set title=?," +
                " weather=?, content=? where _id=?";

        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(sql, new String[]{title, weather, content, id + ""});
        db.close();
    }


    /**
     * 查询日记
     * @return
     */
    public Cursor select(){
        String sql = "select * from diary order by _id desc";//降序排序
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(sql, null);
        return c;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值