Android 使用数据库SQLite

package com.example.test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBase extends SQLiteOpenHelper {

	private static final String TABLE_NAME = "database";
	private static final String DATABASE_CREATE = "create table "
			+ TABLE_NAME
			+ " (_id integer primary key autoincrement, key text not null, secondkey text not null, data text not null);";
	private static final String DATABASE_NAME = "DB";
	private Cursor cursor;
	private SQLiteDatabase db;

	/**
	 * 创建数据库
	 * 
	 * @param paramContext
	 */
	public DataBaseUtils(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	/**
	 * 创建数据表
	 */
	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
		paramSQLiteDatabase.execSQL(DATABASE_CREATE);
	}

	/**
	 * 删除数据表
	 */
	public void onUpgrade(SQLiteDatabase db, int paramInt1, int paramInt2) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	/**
	 * 插入
	 * 
	 * @param key
	 * @param value
	 */
	public void insert(String key, String value) {
		this.insert(key, key, value);
	}

	public void insert(String key, String secondKey, String value) {
		this.db = getWritableDatabase();
		ContentValues contentValus = new ContentValues();
		contentValus.put("key", key);
		contentValus.put("secondkey", secondKey);
		contentValus.put("data", value);
		this.db.insert(TABLE_NAME, null, contentValus);
	}

	/**
	 * 更新
	 * 
	 * @param key
	 * @param value
	 */
	public void update(String key, String value) {
		this.update(key, key, value);
	}

	/**
	 * 更新
	 * 
	 * @param key
	 * @param value
	 */
	public void update(String key, String secondKey, String value) {
		this.db = getWritableDatabase();
		ContentValues contentValus = new ContentValues();
		contentValus.put("key", key);
		contentValus.put("secondkey", secondKey);
		contentValus.put("data", value);
		if (this.db.update(TABLE_NAME, contentValus, "key='" + key
				+ "' and secondkey='" + secondKey + "'", null) == 0) {
			this.db.insert(TABLE_NAME, null, contentValus);
		}
	}

	/**
	 * 删除
	 * 
	 * @param key
	 */
	public void delRccord(String key) {
		this.delRccord(key, key);
	}

	/**
	 * 删除
	 * 
	 * @param key
	 */
	public void delRccord(String key, String secondKey) {
		this.db = getWritableDatabase();
		ContentValues contentValus = new ContentValues();
		contentValus.put("key", key);
		contentValus.put("secondkey", secondKey);
		this.db.delete(TABLE_NAME, "key='" + key + "' and secondkey='"
				+ secondKey + "'", null);
	}

	/**
	 * 查询
	 * 
	 * @param paramString
	 * @return
	 */
	public String queryRocord(String key) {
		return this.queryRocord(key, key);
	}

	/**
	 * 查询
	 * 
	 * @param paramString
	 * @return
	 */
	public String queryRocord(String key, String secondKey) {
		String value = null;
		this.db = getReadableDatabase();
		this.cursor = this.db.query(TABLE_NAME, new String[] { "key",
				"secondkey", "data" }, "key='" + key + "' and secondkey='"
				+ secondKey + "'", null, null, null, null);
		if (this.cursor != null && this.cursor.getCount() > 0) {
			this.cursor.moveToFirst();
			value = this.cursor.getString(2);
		}
		cursor.close();
		return value;
	}

	/**
	 * 查询(多条记录取)
	 * 
	 * @param paramString
	 * @return
	 */
	public String[] queryRocords(String key) {
		String[] value = null;
		this.db = getReadableDatabase();
		this.cursor = this.db.query(TABLE_NAME, new String[] { "key", "data" },
				"key='" + key + "'", null, null, null, null);
		if (this.cursor != null && this.cursor.getCount() > 0) {
			int rows = this.cursor.getCount();
			value = new String[rows];
			this.cursor.moveToFirst();
			for (int i = 0; i < rows; i++) {
				this.cursor.moveToPosition(i);
				value[i] = this.cursor.getString(1);
			}
		}
		cursor.close();
		return value;
	}

	/**
	 * 关闭数据库
	 */
	public void close() {
		if (this.db != null)
			this.db.close();
		if (this.cursor != null) {
			this.cursor.close();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值