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();
}
}
}
Android 使用数据库SQLite
最新推荐文章于 2022-12-14 19:01:06 发布