java 糖ORM,SQLite

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 糖ORM,SQLite相关的知识,希望对你有一定的参考价值。

public class DB_Table extends SugarRecord<DB_Table> {

    String col1, col2;

    public DB_Table(){
    }

    public DB_Table(String col1, String col2, String col3){
        this.col1 = col1;
        this.col2 = col2;
    }

    public String getCol1(){return col1;}
    public String getCol2(){return col2;}

    public void setCol1(String col1){this.col1 = col1;}
    public void setCol2(String col2){this.col2 = col2;}

}
//OBTENER TODOS LOS DATOS
List<DB_Table> filas = DB_Table.listAll(DB_Table.class);
for (int i = 0; i < filas.size(); i++){
    String col1 = filas.get(i).getCol1();
    String col2 = filas.get(i).getCol2();
}

List<DB_Table> items = DB_Table.findWithQuery(DB_Table.class, "SELECT * FROM DBITEMS WHERE STATE = ? ORDER BY LA ASC", "1");
//? es la variable que es reepmplazada en el segundo parametro

//INSERTAR DATOS
for (int i = 0; i < 5; i++){
    String col1 = "data col1 " + i;
    String col2 = "data col2 " + i;
    DB_Table insert = new DB_Table(col1, col2);
    insert.save();
}

//BORRAR FILA
DB_Table.deleteAll(DB_Table.class, "col1 = ?", "data col1 3");
//
Author author = Author.findById(Author.class, 23L);
author.delete();

//BORRA TODA LA TABLA
DB_Table.deleteAll(DB_Table.class); 

//ACTUALIZAR EN BASE AL ID
DB_Table itemUpdate = DB_Table.findById(DB_Table.class, 1L); //Num Long
itemUpdate.setCol1("col1 item updated");
itemUpdate.setCol2("col2 item updated");
itemUpdate.save();

//ACTUALIZAR EN BASE A OTRO CAMPO QUE NO SEA EL ID
//List<DB_Table> item = DB_Table.find(DB_Table.class, "col2 = ?", "data col2 5");
//Long id = item.get(0).getId();
//DB_Table itemUpdate = DB_Table.findById(DB_Table.class, id);
//itemUpdate.setCol1("col1 item updated");
//itemUpdate.setCol2("col2 item updated");
//itemUpdate.save();
Select query = Select.from(db_items.class).where(Condition.prop("col2").eq("data col2 5"));
db_items itemUpdate = (db_items)query.first();
itemUpdate.setProp_send_id(prop_send_id);
itemUpdate.setCol1("col1 item updated");
itemUpdate.setCol2("col2 item updated");
itemUpdate.save();

//WHERE OR AND...
Select query = Select.from(DB_Table.class)
                .where(Condition.prop("col1").eq(3))
                .or(Condition.prop("col1").eq(2));
                //.and()

List<DB_Table> items = query.list();


/////////////////////

//EDADES MENORES A 20
Select youngAuthorQuery = Select.from(Author.class)
                                .where(Condition.prop("age").lt(20));
//EDADES ENTRE 20 Y 50 Y LIMITADO A 5 FILAS
Select specificAuthorQuery = Select.from(Author.class)
                                   .where(Condition.prop("age").gt(20),
                                          Condition.prop("age").lt(50))
                                   .limit(5);
// Get number of items that would be returned by a query with .count()
long numberYoungAuthors = youngAuthorQuery.count();

// Get all of the items corresponding to the query with .list()
List<Author> specificAuthors = specificAuthorQuery.list()

// Get just the first item that corresponds to the query with .first()
Author firstAuthor = specificAuthorQuery.first();

以上是关于java 糖ORM,SQLite的主要内容,如果未能解决你的问题,请参考以下文章

orm4sqlite

支持 UPSERT 的 Android SQLite ORM? [关闭]

SQLite 的轻量级 ORM [关闭]

数据库与ORM

在 SQlite 上与 Objective-C 一起使用的最佳 ORM 是啥? [关闭]

C#使用sqlite-net搭建简易的ORM