第二阶段十天冲刺第五天

今天继续数据库的连接,途中碰上不少问题,因为网络上许多教程已过期,所以不得不重新找

DAO层

package com.example.runapp.Dao;

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


import com.example.runapp.DataBase.DatabaseHelper;
import com.example.runapp.DataBase.OpenHelper;
import com.example.runapp.DataBase.SQLiteDB;
import com.example.runapp.entity.Order;

public class OrderDao {

    private DatabaseHelper helper;
    private SQLiteDatabase db;
    public OrderDao(Context context) {
        helper = new DatabaseHelper(context);
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }
    public void addOrder(Order item) {
        db.beginTransaction();

        try {
           db.execSQL("INSERT INTO order_list(id,kind,describe,money,remark,date) VALUES(null,?,?,?,?,?)",
                    new Object[]{item.getKind(), item.getDescribe(),item.getMoney(),item.getRemark(),item.getDate()});

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    public void deleteOrder(int id) {
        String sql = "delete from AccountIncome where id="+id;

        db.beginTransaction();
        try {
            db.execSQL(sql);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

    }
/*
    public boolean delete (int id)
    {
        boolean f=false;
        String sql="delete from order_list where id='"+id+"'";
        Connection conn=Helper.getConn();
        Statement state=null;
        int a=0;
        try {
            state=conn.createStatement();
            a=state.executeUpdate(sql);
        }catch(SQLException e) {
            e.printStackTrace();}
        finally {
            Helper.close(state, conn);
        }
        if(a>0)
        {
            f=true;
        }
        return f;
    }*/
/*
    //订单在线修改
    public boolean update(Order Car) {
        String sql = "update order_list set kind='" + Car.getKind() + "', describe='" + Car.getDescribe() + "', money='" + Car.getMoney()+"',remark='"+Car.getRemark()+
                "' where id='" + Car.getId() + "'";
        Connection conn = Helper.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Helper.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
*/

}
package com.example.runapp.Dao;

public class UserDao {

}

 

posted @ 2020-05-27 21:19  铁大跑腿  阅读(108)  评论(0)    收藏  举报