dbUtils

package com.example.muhanxi.dbutils;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;


import java.util.List;


public class MainActivity extends AppCompatActivity {


    private DbManager dbManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);






       IApplication application = (IApplication)   getApplication() ;


        //  获取 dbManager 对象 application.daoConfig 从application 中获取
        dbManager = x.getDb(application.daoConfig);




        findViewById(R.id.insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




                News news = new News();
                news.setId(2);
                news.setTitle("我是标题");
                news.setContent("我是内容");


                try {
                    dbManager.save(news);
                } catch (DbException e) {
                    e.printStackTrace();
                }


            }
        });




        findViewById(R.id.query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                try {
                    List<News> list =  dbManager.findAll(News.class);
                    News news =  list.get(0);
                    news.setTitle("------");
                    dbManager.update(news);


                    System.out.println("list = " + list.size());
                } catch (DbException e) {
                    e.printStackTrace();
                }


            }
        });
















    }

}



package com.example.muhanxi.dbutils;


import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;


/**
 * Created by muhanxi on 17/4/21.
 */






//创建一个表 指定表的名称
@Table(name = "news")
public class News {




    //  Column 表字段名 autoGen 表示id自动增长


    @Column(name =  "news_id" ,isId = true,autoGen = false)
    public  int id ;


    @Column(name = "news_title")
    public String title;
    @Column(name = "news_content")
    public String content;




    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public String getContent() {
        return content;
    }


    public void setContent(String content) {
        this.content = content;
    }
}


package com.example.muhanxi.dbutils;


import android.app.Application;


import org.xutils.DbManager;
import org.xutils.x;


/**
 * Created by muhanxi on 17/4/21.
 */


public class IApplication extends Application {
    DbManager.DaoConfig daoConfig ;


    @Override
    public void onCreate() {
        super.onCreate();




        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);


        initDB();




    }






    public void initDB(){
        daoConfig = new DbManager.DaoConfig()
                .setDbName("abc.db")    //设置数据库名称
                .setDbVersion(1)  //数据库版本
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
// TODO: ...
                        //  数据库版本号发生了变化  走这个回调


                    }
                });
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值