免费论坛网站建设,多少钱可以报警立案,天津东丽做网站,网站源码下载有什么用LitePal在查询API方面做了非常多的优化#xff0c;基本上可以满足绝大多数场景的查询需求#xff0c;并且代码也十分整洁。
例如我们需要查询表中的所有数据#xff1a;
Listbooks DataSupport.findAll(Book.class);
没有冗长的参数列表#xff0c;只需要调用一下…LitePal在查询API方面做了非常多的优化基本上可以满足绝大多数场景的查询需求并且代码也十分整洁。
例如我们需要查询表中的所有数据
Listbooks DataSupport.findAll(Book.class);
没有冗长的参数列表只需要调用一下findAll()方法然后通过Book.class参数指定查询Book就可以了。另外findAll()方法的返回值是一个Book类型的List集合。例如在一个登录界面有两个Edittext和一个Button用来输入用户名和密码然后点击按钮进行登录我们只是把这两个字段建成一张表用来存储用户的登录信息。代码如下
package com.example.testappb;import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;import org.litepal.LitePal;
import org.litepal.crud.DataSupport;import java.sql.BatchUpdateException;
import java.util.List;public class MainActivity extends AppCompatActivity {private Button user;private Button driver;private EditText accountEdit;private EditText passwordEdit;private Button login;private Button Register;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);.. ....//获取输入框内对象实例 user (Button)findViewById(R.id.button_user) ;accountEdit (EditText)findViewById(R.id.edittext_account);passwordEdit (EditText)findViewById(R.id.edittext_password);//设置登录按钮透明度login (Button)findViewById(R.id.login);login.getBackground().setAlpha(25);login.setOnClickListener(new View.OnClickListener() {Override//public void onClick(View view) {String account accountEdit.getText().toString();String password passwordEdit.getText().toString();// Driveruser driveruser new Driveruser();//首先创造了一个Driveruser的实例。// driveruser.getDtel();//使用set方法对数据进行设置// driveruser.getDpassword();// driveruser.save();//使用save()保存。save方法来源于DataSupport中继承而来的//登录匹配数据库ListDriveruser driveruers DataSupport.findAll(Driveruser.class);for(Driveruser driveruser:driveruers){//遍历List集合中的Driveruser对象然后下面依次比较如果输入的用户名和密码与数据库中的存储的用户名和密码相似则允许登录并跳转到下一个活动if((driveruser.getDtel().toString()).equals(account)(driveruser.getDpassword().toString()).equals(password)){Toast.makeText(MainActivity.this,登录成功,Toast.LENGTH_SHORT).show();Intent intent new Intent(MainActivity.this,FirstActivity.class);//登录成功跳转到FirstActivity活动startActivity(intent);//启动活动finish();}else//异常判断{Toast.makeText(MainActivity.this,用户名或者密码无效请重新输入,Toast.LENGTH_SHORT).show();break;}}});..............}
}
除了findAll()方法LitePal还提供了很多其他非常有用的查询API。比如我们想要
查询Book表中第一条数据可以这样写
Book firstBook DataSupport.findFirst(Book.class);
查询Book表中的最后一条数据可以这样写
Book lastBook DataSupport.findLast(Book.class);
还可以通过连缀查询来定制更多的查询功能。与SQL对应select()方法用于指定查询哪几列的数据查询name和author这两列的数据可以这样写
ListBook books DataSupport.select(name,author).find(Book.class);where()方法用于指定查询的约束条件比如只查询页数大于400的数据可以这样写
ListBook books DataSupport.where(pages ?,400).find(Book.class);order()方法用于指定结果的排序方式对应SQL中的order by 关键字。比如将查询结果按照书价从高到低排序这样写
ListBook books DataSupport.order(price desc).find(Book.class);其中desc表示降序排列asc或者不写表示升序排列。list()方法用于指定查询结果的数量比如只查询表中的前3条数据可以这样写
ListBook books DataSupport.limit(3).find(Book.class);
offset()方法用于指定查询结果的偏移量比如查询表中的第2条第3条第4条数据就可以这样写
ListBook books DataSupport.limit(3).offset(1).find(Book.calss).
limit()和offset()方法共同对应了SQL当中的limit关键字。
当然我们还可以对这5个方法进行任意的连缀组合来完成一个比较复杂的查询操作
ListBook books
DataSupport.select(name,author,pages).where(pages?,400).order(pages).limit(10).offset(10).find(Book.class)。
这段代码表示查询Book表中第11——20条满足页数大于400这个条件的name,author和pages这三列数据并将查询结果按照页数升序排列。但是当有血特殊需求上述的API都满足不了的时候LitePal仍然支持使用原生的SQL来进行查询
Cursor c DataSupport.findBySQL(select * from Book where pages ? and price ?,400,20);
调用DataSupport.findBySQL()方法进行原生查询其中第一个参数用于指定SQL语句后面的参数用于指定占位符的值。注意findBySQL()方法返回的是一个Cursor对象接下来还需要通过之前的老方式将数据一一取出。所谓的老方式详见6.4.6节郭霖《第一行代码》第二版。P225