SQLite数据库用法

来源:互联网 发布:大数据的利弊 编辑:程序博客网 时间:2024/06/05 19:48

SQLite基类代码:

import net.intelink.express.db.DBOpenHelper;import android.content.Context;public class SQLiteDaoBase {protected DBOpenHelper dbHelper;public SQLiteDaoBase(Context context) {this.dbHelper = new DBOpenHelper(context);}}

数据库操作类BstatInfoDao:

import java.util.ArrayList;import java.util.List;import net.intelink.express.entity.BstatEntity;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class BstatInfoDao extends SQLiteDaoBase {private String TABLE = "TAB_BSTAT_INFO";public BstatInfoDao(Context context) {super(context);}public void add(String stano, String station, String parentStano, String isZFJ,String manager2, String managerTel2, String statAddress, String sendArea,String sendArea2) {SQLiteDatabase database = this.dbHelper.getWritableDatabase();Object[] args = new Object[] { stano, station, parentStano, isZFJ, manager2, managerTel2,statAddress, sendArea, sendArea2 };database.execSQL("insert into "+ TABLE+ " (stano,station,parentStano,isZFJ,manager2,managerTel2,statAddress,sendArea,sendArea2) values(?,?,?,?,?,?,?,?,?)",args);database.close();}public String get() {SQLiteDatabase database = this.dbHelper.getReadableDatabase();try {Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE + " Limit 1", null);if (cursor.getCount() > 0) {return "success";}} catch (Exception e) {throw new RuntimeException(e);} finally {database.close();}return "";}public List<BstatEntity> getBstatInfo() {List<BstatEntity> list = new ArrayList<BstatEntity>();BstatEntity entity = null;SQLiteDatabase database = this.dbHelper.getReadableDatabase();try {Cursor cursor = database.rawQuery("SELECT stano,station,parentStano,isZFJ,manager2,managerTel2,statAddress,sendArea,sendArea2 FROM "+ TABLE + " ", null);if (cursor.getCount() > 0) {while (cursor.moveToNext()) {entity = new BstatEntity();entity.setStano(cursor.getString(cursor.getColumnIndex("stano")));entity.setStation(cursor.getString(cursor.getColumnIndex("station")));entity.setParentStano(cursor.getString(cursor.getColumnIndex("parentStano")));entity.setIsZFJ(cursor.getString(cursor.getColumnIndex("isZFJ")));entity.setManager2(cursor.getString(cursor.getColumnIndex("manager2")));entity.setManagerTel2(cursor.getString(cursor.getColumnIndex("managerTel2")));entity.setIsZFJ(cursor.getString(cursor.getColumnIndex("statAddress")));entity.setSendArea(cursor.getString(cursor.getColumnIndex("sendArea")));entity.setSendArea2(cursor.getString(cursor.getColumnIndex("sendArea2")));list.add(entity);}}return list;} catch (Exception e) {throw new RuntimeException(e);} finally {database.close();}}public List<BstatEntity> getBstatInfo(String stanoOrStation) {List<BstatEntity> list = new ArrayList<BstatEntity>();BstatEntity entity = null;SQLiteDatabase database = this.dbHelper.getReadableDatabase();try {Cursor cursor = database.rawQuery("SELECT stano,station,parentStano,isZFJ FROM "+ TABLE + " where stano like ? or station like ?", new String[] {"%" + stanoOrStation + "%", "%" + stanoOrStation + "%" });if (cursor.getCount() > 0) {while (cursor.moveToNext()) {entity = new BstatEntity();entity.setStano(cursor.getString(cursor.getColumnIndex("stano")));entity.setStation(cursor.getString(cursor.getColumnIndex("station")));entity.setParentStano(cursor.getString(cursor.getColumnIndex("parentStano")));entity.setIsZFJ(cursor.getString(cursor.getColumnIndex("isZFJ")));list.add(entity);}}return list;} catch (Exception e) {throw new RuntimeException(e);} finally {database.close();}}}






0 0
原创粉丝点击