-
Android SQLiteDatabase的使用
- package com.shawn.test;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- public class DatabaseAdapter{
-
- private static final String DB_NAME = "Test.db";
- private static final int DB_VERSION = 1;
-
- private static final String DB_TABLE = "my_order";
- private static final String KEY_ID = "_id";
- private static final String KEY_ORDER_ID = "order_id";
- private static final String KEY_TYPE = "_type";
- private static final String KEY_STATE = "_state";
-
- private Context context;
- private DatabaseHelper mDatabaseHelper;
- private SQLiteDatabase mSQLiteDatabase;
-
- private static class DatabaseHelper extends SQLiteOpenHelper{
-
- private static final String DB_CREAT = "CREATE TABLE "
- + DB_TABLE
- + " (" + KEY_ID + " INTEGER PRIMARY KEY,"
- + KEY_ORDER_ID + " TEXT,"
- + KEY_TYPE + " INTEGER,"
- + KEY_STATE + " INTEGER)";
-
- public DatabaseHelper(Context context) {
- super(context, DB_NAME, null , DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
-
- db.execSQL(DB_CREAT);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
- db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
- onCreate(db);
- }
- }
-
- public DatabaseAdapter(Context context) {
- this.context = context;
- }
-
- public void open() {
- mDatabaseHelper = new DatabaseHelper(context);
- mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
- }
-
- public void close() {
- mSQLiteDatabase.close();
- mDatabaseHelper.close();
- }
-
-
- public long insertData(String orderId, int type) {
- ContentValues values = new ContentValues();
- values.put(KEY_ORDER_ID, orderId);
- values.put(KEY_TYPE, type);
- values.put(KEY_STATE, Config.STATE_APPLY);
- long id = mSQLiteDatabase.insert(DB_TABLE, KEY_ID, values);
- return id;
- }
-
- public boolean deleteData(Context context, long id) {
- boolean delete = mSQLiteDatabase.delete(DB_TABLE, KEY_ID + "=" +id, null)>0;
- return delete;
- }
-
- public boolean updateData(long id, int state) {
- ContentValues values = new ContentValues();
- values.put(KEY_STATE, ""+state);
- boolean update = mSQLiteDatabase.update(DB_TABLE, values, KEY_ID + "=" +id, null)>0;
- return update;
- }
-
- public Cursor fetchData(String selection) {
- Cursor mCursor = mSQLiteDatabase.query(DB_TABLE, new String[]{KEY_ID, KEY_ORDER_ID, KEY_TYPE, KEY_STATE}, selection, null, null, null, null);
- if(mCursor != null)
- mCursor.moveToFirst();
- return mCursor;
- }
- }
-
相关阅读:
buildroot编译文件系统,出现mount: you must be root. can't open /dev/console: Permission denied这种错误。
Ubuntu 18.04实现实时显示网速
git 第一次push 出现fatal: 无法读取远程仓库。
Sql2012 AlwaysOn
SCVMM 安装
POJ 3537 Crosses and Crosses
HDOJ 1524 A Chess Game
HDOJ 1907 John
HDOJ 1848 Fibonacci again and again
HDOJ 1536 S-Nim
-
原文地址:https://www.cnblogs.com/Free-Thinker/p/3467401.html
Copyright © 2020-2023
润新知