Sqlite简单尝试

前端之家收集整理的这篇文章主要介绍了Sqlite简单尝试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.ine.db.impl;

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;

public class DBHelper extends sqliteOpenHelper {
    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context)
    {
        // CursorFactory设置为null,使用默认值
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        // context.deleteDatabase(DATABASE_NAME);
    }

    // 数据库第一次被创建时onCreate会被调用
    @Override
    public void onCreate(sqliteDatabase db)
    {
        db.execsql("CREATE TABLE IF NOT EXISTS count"
                + "(data_id INTEGER PRIMARY KEY AUTOINCREMENT,date TEXT,type TEXT)");
    }

    // 如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade
    @Override
    public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion)
    {
        db.execsql("ALTER TABLE count ADD COLUMN other STRING");
    }
}@H_301_81@ 
package com.ine.db.impl;

import java.util.ArrayList;
import java.util.List;

import com.ine.entity.CrtData;
import com.ine.tool.CrtTool;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;

public class DBManager {
    private DBHelper dbh;
    private sqliteDatabase db;

    public DBManager(Context context)
    {
        dbh = new DBHelper(context);
        // 因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName,
        // mFactory);
        // 所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = dbh.getWritableDatabase();
    }

    public boolean save(long date,String type)
    {
        long resoult = -1;
        db.beginTransaction(); // 开始事务
        try
        {
            // ContentValues以键值对的形式存放数据
            ContentValues cv = new ContentValues();
            cv.put("date",date);
            cv.put("type",type);
            // 插入ContentValues中的数据
            // 第二个参数表示如果插入的数据每一列都为空的话,需要指定此行中某一列的名称,系统将此列设置为NULL,不至于出现错误
            resoult = db.insert("count",cv);

            db.setTransactionSuccessful(); // 设置事务成功完成

        } finally
        {
            db.endTransaction(); // 结束事务
        }
        return (-1 < resoult) ? true : false;
    }

    public List<CrtData> getAll()
    {
        Cursor c = db.rawQuery("SELECT * FROM count",null);
        return getByCursor(c);
    }

    public List<CrtData> getByType(String type)
    {
        Cursor c =
                db.rawQuery("SELECT * FROM count WHERE type = ?",new String[]
                { type });
        return getByCursor(c);
    }

    public void delAll()
    {
        db.execsql("DELETE FROM count;");
    }

    private List<CrtData> getByCursor(Cursor c)
    {
        ArrayList<CrtData> datas = new ArrayList<CrtData>();
        while (c.moveToNext())
        {
            CrtData data = new CrtData();
            data.setData_id(c.getInt(c.getColumnIndex("data_id")));
            data.setDate(c.getString(c.getColumnIndex("date")));
            data.setType(c.getString(c.getColumnIndex("type")));
            datas.add(CrtTool.initCrtData(data));
        }
        c.close();
        return datas;
    }

    /** * close database */
    public void closeDB()
    {
        db.close();
    }
}@H_301_81@

猜你在找的Sqlite相关文章