Android存储之SQLite数据库数据库
创建数据库
package ……;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by kongqw on 2015/11/22.
*/
public class SearchRecordOpenHelper extends SQLiteOpenHelper {
private static final String CREATE_SEARCH_RECORD_TABLE = "CREATE TABLE SEARCH_RECORD_TABLE(ID INTEGER PRIMARY KEY AUTOINCREMENT,TEXT VARCHAR(50) NOT NULL)";
public SearchRecordOpenHelper(Context context) {
super(context, "search_record.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_SEARCH_RECORD_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
操作数据库
package ……;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
/**
* Created by kongqw on 2015/11/22.
* 查询记录的增删
*/
public class SearchRecordDao {
private static final String TAG = "SearchRecordDao";
private SearchRecordOpenHelper mSearchRecordOpenHelper;
private SQLiteDatabase mSQLiteDatabase;
public SearchRecordDao(Context context) {
mSearchRecordOpenHelper = new SearchRecordOpenHelper(context);
}
/**
* 添加一条查询记录
*
* @param text 插入的数据
*/
public boolean addSearchRecord(String text) {
try {
mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("TEXT", text);
long r = mSQLiteDatabase.insert("SEARCH_RECORD_TABLE", null, values);
if (-1 == r) {
Log.e(TAG, "查询数据插入失败");
return false;
} else {
Log.e(TAG, "查询数据插入成功");
return true;
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "查询插入数据出现异常");
return false;
} finally {
mSQLiteDatabase.close();
}
}
/**
* 删除一条记录
*
* @param text 要删除的数据
*/
public boolean deleteSearchRecord(String text) {
try {
mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
mSQLiteDatabase.delete("SEARCH_RECORD_TABLE", "TEXT = ?", new String[]{text});
Log.e(TAG, "查询数据删除成功");
return true;
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "查询删除数据出现异常");
return false;
} finally {
mSQLiteDatabase.close();
}
}
/**
* 查询查询记录
*
* @return
*/
public ArrayList<String> selectSearchRecord() {
Cursor cursor = null;
ArrayList<String> searchRecord = new ArrayList<String>();
try {
mSQLiteDatabase = mSearchRecordOpenHelper.getReadableDatabase();
cursor = mSQLiteDatabase.query("SEARCH_RECORD_TABLE", new String[]{"text"}, null, null, null, null, "id desc");
while (cursor.moveToNext()) {
String text = cursor.getString(0);
searchRecord.add(text);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
mSQLiteDatabase.close();
return searchRecord;
}
}
/**
* 查询指定行数的数据
*
* @return
*/
public ArrayList<String> querySearchRecord() {
return innerSelectSearchRecordWithLimit(20 + "");
}
/**
* 查询指定条数的查询记录
*
* @return
*/
private ArrayList<String> innerSelectSearchRecordWithLimit(String limit) {
Cursor cursor = null;
ArrayList<String> searchRecord = new ArrayList<String>();
try {
mSQLiteDatabase = mSearchRecordOpenHelper.getReadableDatabase();
cursor = mSQLiteDatabase.query("SEARCH_RECORD_TABLE", new String[]{"text"}, null, null, null, null, "id desc", limit);
while (cursor.moveToNext()) {
String text = cursor.getString(0);
searchRecord.add(text);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
mSQLiteDatabase.close();
return searchRecord;
}
}
/**
* 删除所有历史记录
*
* @return 删除是否成功
*/
public boolean deleteAllSearchRecord() {
try {
mSQLiteDatabase = mSearchRecordOpenHelper.getWritableDatabase();
mSQLiteDatabase.delete("SEARCH_RECORD_TABLE", null, null);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
mSQLiteDatabase.close();
}
}
}
查看数据库