当前位置: 首页 > 工具软件 > ActiveAndroid > 使用案例 >

ActiveAndroid 数据库

阎承
2023-12-01

https://github.com/pardom/ActiveAndroid
5.0以上在清单文件加这句话

Application

package qianfeng.activeandroid;

import android.app.Application;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Configuration;

/**
 * Created by keven on 16/7/6.
 */
public class AppContent extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        //初始化
//        ActiveAndroid.initialize(this);
        Configuration config = new Configuration.Builder(this)
                .setDatabaseName("CD1603")
                .setDatabaseVersion(2)
                .create();
        ActiveAndroid.initialize(config);
    }
}

MainActivity

package qianfeng.activeandroid;

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

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;

import java.util.List;

import qianfeng.activeandroid.entity.Book;
import qianfeng.activeandroid.entity.User;

public class MainActivity extends AppCompatActivity {

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

    public void _query(View view) {
        Select select = new Select();
        //查询一条
        User user_1 = select.from(User.class).executeSingle();
        Log.e("user_1",user_1.toString());

        //查询所有
      /*  List<User> users = select.from(User.class).execute();
        for (User user:users){
            Log.e("users",user.toString());
        }*/

        //查询所有 倒序
     /*   List<User> users = select.from(User.class).orderBy("id DESC").execute();
        for (User user : users){
            Log.e("users",user.toString());
        }
*/

        //分页查询
       /* List<User> users = select.from(User.class).offset(10).limit(10).execute();
        for (User user : users){
            Log.e("users",user.toString());
        }*/

        //条件查询
//        List<User> users = select.from(User.class).where("id like ?", "%5%").execute();
//        List<User> users = select.from(User.class).where("id > ?", "5").execute();
//        List<User> users = select.from(User.class).where("id < ?", "5").execute();
//        List<User> users = select.from(User.class).where("id = ?", "5").execute();

        //多条件查询
//        List<User> users = select.from(User.class).where("id > ?", 5).and("id < ?", 20).and("age like ?", "2%").execute();
//        for (User user : users){
//            Log.e("users",user.toString());
//        }

    }

    public void _update(View view) {
        Select select = new Select();
        //查询一条
        User user1 = select.from(User.class).executeSingle();
        user1.setName("姑娘");
        //数据存在更新,不存在新增
        Long id = user1.save();
    }

    public void _insert(View view) {
        User user = new User();
        user.setName("随便");
        user.setAge("23");
        user.setSex("女");
        //保存到数据库
        user.save();

        //事物概念
        ActiveAndroid.beginTransaction();
        try{
            for (int i=0;i<20;i++){
                User u = new User();
                u.setName("随便:"+i);
                u.setAge(String.valueOf(23+i));
                u.setSex("女");
                //保存到数据库
                u.save();
            }
            ActiveAndroid.setTransactionSuccessful();
        }finally {
            ActiveAndroid.endTransaction();
        }

    }

    public void _delete(View view) {
        Delete delete = new Delete();
        //删除一个
        delete.from(User.class).where("id = ?",5).execute();
        //删除一票
        delete.from(User.class).where("id like ?","%3%").execute();
    }

    public void _addBook(View view) {
        Select select = new Select();
        User user = select.from(User.class).executeSingle();

        Book book = new Book();
        book.bookName="斗罗大陆";
        book.user = user;
        book.save();
    }

    public void _queryUserBook(View view) {
        Select select = new Select();
        Book book = select.from(Book.class).executeSingle();
        Log.e("book",book.toString());

    }
}

表名User

package qianfeng.activeandroid.entity;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

import java.util.List;

/**
 * Created by keven on 16/7/6.
 */
@Table(name = "user")
public class User extends Model{

    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private String age;
    @Column(name = "sex")
    private String sex;

    public List<Book> books(){
        return getMany(Book.class,"user");
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {


        return "User{" +
                "id='" + getId() + '\'' +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", books='" + books().size() + '\'' +
                '}';
    }
}

表名Book

package qianfeng.activeandroid.entity;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

/**
 * Created by keven on 16/7/6.
 */
@Table(name = "book")
public class Book extends Model{

    @Column(name = "bookName")
    public String bookName;
    @Column(name = "user")
    public User user;

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", user=" + user +
                '}';
    }
}

*重要清单配置文件加上这一句*

  <meta-data android:name="AA_MODELS" android:value="com.qf.activedatabase.Student"/>
 类似资料: