FinalDb操作sqlite的数据库(增删改查)

前端之家收集整理的这篇文章主要介绍了FinalDb操作sqlite的数据库(增删改查)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用Stetho工具查看sqlite数据库,使用方法见:http://blog.csdn.net/u010002184/article/details/52563247

如下实例:

package com.example.liuyazhou.myfifthapplication;

/**
 * Created by liuyazhou on 2016/9/17.
 */

import java.util.Date;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.annotation.view.ViewInject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AfinalOrmDemoActivity extends FinalActivity {

    String TAG = AfinalOrmDemoActivity.class.getName();

    @ViewInject(id = R.id.textView)
    TextView textView; //这里使用了afinal的ioc功能
    @ViewInject(id = R.id.btn_add,click = "btnAddClick")
    Button btnAdd; //对象名没有用到,用到了click方法名
    @ViewInject(id = R.id.btn_delete,click = "btnDeleteClick")
    Button btnDelete;
    @ViewInject(id = R.id.btn_update,click = "btnUpdateClick")
    Button btnUpdate;
    @ViewInject(id = R.id.btn_select,click = "btnSelectClick")
    Button btnSelect;


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

    }//onCreate()

    public void btnAddClick(View v) { ////这里参数 View v 是必须要有的,不然点击按钮没有反应
        //   FinalDb db = FinalDb.create(this); //这样默认数据库名称是 afinal.db
        FinalDb db = FinalDb.create(this,"test.db",true);
        User user = new User();
        user.setEmail("123@mail.com");
        user.setName("Tom2");
        user.setRegisterDate(new Date());
        user.setMoney(123.0);
        db.save(user);
        Toast.makeText(AfinalOrmDemoActivity.this,"添加数据",Toast.LENGTH_LONG).show();
    }

    public void btnDeleteClick(View v) {
        FinalDb db = FinalDb.create(this,true);  ////拿到引用
        // db.delete(user); //根据对象主键进行删除
        // db.deleteById(user,1); //根据主键删除数据
      //  db.deleteByWhere(User.class,"money=2.0"); //自定义where条件删除
        db.deleteById(User.class,3);
        //db.deleteAll(User.class); //删除Bean对应的数据表中的所有数据
        Toast.makeText(AfinalOrmDemoActivity.this,"删除数据",Toast.LENGTH_LONG).show();
    }

    public void btnUpdateClick(View v) {
        FinalDb db = FinalDb.create(this,true);

//        User user = new User();
//        user.setMoney(111.0);
//        db.update(user,"id = 1");//根据where条件更新

        User user = new User();
        user.setId(3); /////这个属性必须要有,表示 update user_test2 set ....where id =3;
        user.setMoney(10.0);
        user.setEmail("abc@mail.com");
        user.setName("Bruce");
        db.update(user);

        Toast.makeText(AfinalOrmDemoActivity.this,"修改数据",Toast.LENGTH_LONG).show();
    }

    public void btnSelectClick(View v) {
        FinalDb db = FinalDb.create(this,true);
//        List<User> userList = db.findAll(User.class);  ///查询所有
//        Log.e("AfinalOrmDemoActivity","用户数量:" + (userList != null ? userList.size() : 0));

//        List<User> resultList = db.findAllByWhere(User.class,"name='Tom'");
        List<User> resultList = db.findAllByWhere(User.class,"name like '%m%'");
        for (int i = 0; i < resultList.size(); i++) {
            Log.i(TAG,"btnSelectClick: " + resultList.get(i).getId() + "," + resultList.get(i).getMoney() + "," + resultList.get(i).getName() + "," + resultList
                    .get(i).getRegisterDate() + "\n");
            Toast.makeText(AfinalOrmDemoActivity.this,"查询数据",Toast.LENGTH_LONG).show();
        }

    }//btnSelectClick()


}
package com.example.liuyazhou.myfifthapplication;

import android.app.Application;

import com.facebook.stetho.Stetho;

/**
 * Created by liuyazhou on 2016/9/17.
 */
public class CustomApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Stetho.initializeWithDefaults(this);
    }
}
package com.example.liuyazhou.myfifthapplication;

/**
 * Created by liuyazhou on 2016/9/17.
 */
import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Table;

import java.util.Date;

@Table(name="user_test2")  ////这样使新建的表名是 user_test2
public class User {
    @Id
    private int id;
    private String name;
    private String email;
    private Date registerDate;
    private Double money;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="增" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="删" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="改" />

    <Button
        android:id="@+id/btn_select"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="hello"
        android:textSize="20sp" />

</LinearLayout>
.jar 和 AndroidManifest.xml 和 build.gradle 的配置 详见http://blog.csdn.net/u010002184/article/details/52563247

原文链接:https://www.f2er.com/sqlite/198730.html

猜你在找的Sqlite相关文章