当前位置: 首页 > 工具软件 > Table Helper > 使用案例 >

sqlitehelper android,SQLite Helper工具类

丰景同
2023-12-01

通过这个类,可以实现对数据库的创建,表的创建以及增删改查等操作.

package com.cjq.baidu.map;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.SQLException;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class LocationInfoUtil {

public SQLiteDatabase mSQLiteDatabase =

null;

private final static String DATABASE_NAME =

"mapforcjq.db";

private final static String TABLE_NAME =

"cjq";

private final static int DB_VERSION = 1;

private final static String KEY_ID =

"_id";

private final static String KEY_LATITUDE =

"latitude";

private final static String KEY_LONGITUDE =

"longitude";

private final static String KEY_TIME =

"dateandtime";

private final static String KEY_FIELD =

"field";

private final static String CREATE_TABLE =

"CREATE TABLE " + TABLE_NAME

+ " (" +

KEY_ID + " INTEGER PRIMARY KEY," + KEY_LATITUDE

+ " DOUBLE,"

+ KEY_LONGITUDE + " DOUBLE," + KEY_TIME

+ "

TIMESTAMP," + KEY_FIELD + " TEXT)";

不写类型

private final static String CREATE_TABLE = "CREATE TABLE " +

TABLE_NAME

+ " (" +

KEY_ID + " INTEGER PRIMARY KEY," + KEY_LATITUDE

+ "," +

KEY_LONGITUDE + "," + KEY_TIME

+ " ," +

KEY_FIELD + " TEXT)";

private DatabaseHelper mDatabaseHelper =

null;

private Context mContext = null;

private static class DatabaseHelper extends

SQLiteOpenHelper {

DatabaseHelper(Context context)

{

super(context,

DATABASE_NAME, null, DB_VERSION);

}

// 创建表

@Override

public void

onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_TABLE);

}

// 升级数据库

@Override

public void

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

{

db.execSQL("DROP

TABLE IF EXISTS notes");

onCreate(db);

}

}

public LocationInfoUtil(Context context)

{

mContext = context;

}

// 打开数据库,返回数据库对象

public void open() throws SQLException {

mDatabaseHelper = new

DatabaseHelper(mContext);

mSQLiteDatabase =

mDatabaseHelper.getWritableDatabase();

}

// 关闭数据库

public void close() {

mDatabaseHelper.close();

}

// 插入一条数据

public long insertData(long latitude, long

longitude, long time) {

ContentValues cv = new

ContentValues();

cv.put(KEY_LATITUDE,

latitude);

cv.put(KEY_LONGITUDE,

longitude);

cv.put(KEY_TIME, time);

return

mSQLiteDatabase.insert(TABLE_NAME, KEY_ID, cv);

}

// 删除一条数据

public boolean deleteData(long rowId) {

return

mSQLiteDatabase.delete(TABLE_NAME, KEY_ID + "=" + rowId, null)

> 0;

}

// 通过Cursor查询所有数据

public Cursor fetchAllData() {

return

mSQLiteDatabase.query(TABLE_NAME, new String[] { KEY_ID,

KEY_LATITUDE,

KEY_LONGITUDE, KEY_TIME }, null, null, null,

null,

null);

}

// 通过Cursor查询指定数据

public Cursor fetchData(long rowId) {

Cursor mCursor =

mSQLiteDatabase.query(TABLE_NAME, new String[] {

KEY_ID,

KEY_LATITUDE, KEY_LONGITUDE, KEY_TIME }, KEY_ID + "="

+

rowId, null, null, null, null, null);

if (mCursor != null) {

mCursor.moveToFirst();

}

return mCursor;

}

public boolean updateData(long latitude, long

longitude, long time) {

ContentValues cv = new

ContentValues();

cv.put(KEY_LATITUDE,

latitude);

cv.put(KEY_LONGITUDE,

longitude);

return

mSQLiteDatabase.update(TABLE_NAME, cv, KEY_TIME + "=" + time,

null)

> 0;

}

}

 类似资料: