Android数据存储——SQLite数据库

本篇整合Android数据库的使用,以保存一个实体类为例子。

先来看下数据库语句:

ORM:关系对象映射

增加数据:

 ContentValues values = new ContentValues();
 values.put("name", "小丽");
 values.put("phone", "110");
 mDB.insert("student",//表名
             null, //规避插入语句的错误
             values );//插入的值 

删除数据:

mDB.delete("student", //表名
           "name =?",//条件语句
           new String[]{"小丽"});//条件语句的占位符

修改数据:

 ContentValues values = new ContentValues();
  values.put("phone", "10086");
  mDB.update("student",//表名
              values, //要修改的值
              "name =?",//条件语句
              new String[]{"小丽"});

查询数据:

 Cursor cursor = mDB.query("student",//表名
                null,//查询的字段
                null,//条件语句
                null,//条件语句的占位符
                null,//分组语句
                null,//分组条件
                null);//排序
  boolean toFirst = cursor.moveToFirst();
  while(toFirst){
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String phone = cursor.getString(cursor.getColumnIndex("phone"));
            MyData myData = new MyData(name, phone);
            dataList.add(myData);
            toFirst = cursor.moveToNext();
        }       

下面是数据库创建及使用步骤

1、方法调用

private DBMang_Order dbMang_Order; dbMang_Order=DBMang_Order.getInstance(getApplicationContext());

//查询
ArrayList<OrderUncheck> uncheckList = dbMang_Order.query();
//删除
dbMang_Order.delete(orderNo);
//增加
dbMang_Order.insert(new OrderUncheck(orderNo, account, action));
//修改
dbMang_Order.update(new OrderUncheck(orderNo, account, action));

2、首先创建要存储的实体类

public class OrderUncheck {

    String orderNo;
    double account;
    int action;
    public String getOrderNo() {
        return orderNo;
    }
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }
    public double getAccount() {
        return account;
    }
    public void setAccount(double account) {
        this.account = account;
    }
    public int getAction() {
        return action;
    }
    public void setAction(int action) {
        this.action = action;
    }
    public OrderUncheck(String orderNo, double account, int action) {
        super();
        this.orderNo = orderNo;
        this.account = account;
        this.action = action;
    }
}

3、创建数据库

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQL_Order extends SQLiteOpenHelper {

     private static final String name = "count"; //数据库名称    
     private static final int version = 1; //数据库版本  
     //第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类  

    public MySQL_Order(Context context) {
        super(context, name, null, version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="CREATE TABLE neworder (_id INTEGER PRIMARY KEY AUTOINCREMENT,orderNo VARCHAR(200),account VARCHAR(19100),action VARCHAR(200))";             
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

4、代码中被调用的方法在这个类:增删改查

public class DBMang_Order {
    private static DBMang_Order mDbManager=new DBMang_Order();
    private static SQLiteDatabase mDb;
    private static Context context;
    private  DBMang_Order() {

    }
    public static synchronized DBMang_Order getInstance(Context context){
        DBMang_Order.context = context;
        if(mDb==null){
            MySQL_Order mySQL = new MySQL_Order(context);
            mDb = mySQL.getWritableDatabase();
        }
        return mDbManager;
    }   
    interface NewTable{
        String TABLE_NAME="neworder";
        String TABLE_COLUMN_orderNo="orderNo";
        String TABLE_COLUMN_account="account";
        String TABLE_COLUMN_action="action";
    }
    //增
    public void insert(OrderUncheck info){      
        ContentValues values=new ContentValues();
        values.put(NewTable.TABLE_COLUMN_orderNo, info.getOrderNo());
        values.put(NewTable.TABLE_COLUMN_account, info.getAccount());
        values.put(NewTable.TABLE_COLUMN_action, info.getAction());
        Log.w("mDb.insert", ""+info);
        Log.w("mDb.insert",""+ values);
        mDb.insert(NewTable.TABLE_NAME, null, values);      
    }       
    //删
    public void delete(String orderNo) {
        Log.w("delete", orderNo);
        mDb.delete(NewTable.TABLE_NAME, NewTable.TABLE_COLUMN_orderNo+"=?", new String[]{orderNo});
    }
    //改
    public void update(OrderUncheck info){
        ContentValues values = new ContentValues();
        values.put(NewTable.TABLE_COLUMN_account, info.getAccount());
        mDb.update(NewTable.TABLE_NAME, values, NewTable.TABLE_COLUMN_orderNo+"=? and "+NewTable.TABLE_COLUMN_orderNo+"=?", new String[]{String.valueOf(info.getOrderNo()),""+info.getAccount()});
    }
    //查
    public ArrayList<OrderUncheck> query(){

        Cursor cursor = mDb.query(NewTable.TABLE_NAME, null, null, null, null, null, null);
        ArrayList<OrderUncheck> list=new ArrayList<OrderUncheck>();
        boolean toFirst = cursor.moveToFirst();         
        while (toFirst) {                       
            String orderNo = cursor.getString(cursor.getColumnIndex(NewTable.TABLE_COLUMN_orderNo));
            String account = cursor.getString(cursor.getColumnIndex(NewTable.TABLE_COLUMN_account));
            String action = cursor.getString(cursor.getColumnIndex(NewTable.TABLE_COLUMN_account));
            double accountd = Double.parseDouble(account);
            int actiond=Integer.parseInt(action);
            OrderUncheck order = new OrderUncheck(orderNo, accountd,actiond);
            list.add(order);
            toFirst=cursor.moveToNext();
        }
        cursor.close();
        return list;

    }       
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。