namespace app\controller;
use app\BaseController;
use app\model\Admin;
use Exception;
use think\facade\Db;
class Datatest extends BaseController
{
public function index()
{
//查询表
$admin = Db::table("guestbooke")->select();
return json($admin);
}
public function demo()
{
//find只会找到1条
//select 会找到所有数据
//条件查询
//$demo = Db::table("admin")->where("id", 1)->find();
//获取最近查询的sql语句
// $gl= Db::getLastSql();
//如果没有找到数据,则返回数据异常
// $demo=Db::table("admin")->where("id",1)->findorFail();
//如果没有找到数据,则返回NUll
// $demo=Db::table("admin")->where("id",1)->findorEmpty();
// $demo = Db::table("admin")->select()->toArray();
//将返回的值返回成数组
//$rs=dump($demo);
//查询单独列是值
//$demo=Db::table("news")->column("newtitle","news_id");
// echo $rs=dump($demo);
//分批查询
/* $demo = Db::table("admin")->chunk(1,function($username){
foreach($username as $newt){
dump($newt);
}
echo "a";
}); */
//每次只读取一行,使内存大大减少消耗
/* $curcor=Db::table("news")->cursor();
foreach($curcor as $cr){
dump($cr);
} */
//return json($demo);
//避免资源浪费,可以使用链表方式,以保存请求,多次调用
/*
$rs=Db::table("admin");//Query
$rt=$rs->select();
$rp=$rs->where("id",1)->find();
$rp2=$rs->select();
return Db::getLastSql();
//为了清除上传留下来的sql语句 可以使用 removeOption方法
//$rp2=$rs->removeOption("where")->select();
return Db::getLastSql(); */
}
public function getUser()
{
//模型初探
$rs = Admin::select();
return $rs;
}
public function insert()
{
//插入数据 insert 方法;
/* $data=[
"username" => "wck",
"password" => "wck"
];
$rs=Db::table("admin");
$insert=$rs->insert($data);
*/
//强行新增数据 strict(false) 他会强行增加已有的字段
/* $data = [
"username" => "ck",
"password" => "ck",
"cc"=>"aaa"
];
$rs = Db::table("admin");
return $insert = $rs->strict(false)->insert($data); */
//replace插入会检查主键与传入的值是否相同,如果相同(即重复),则会报错
// $insert = $rs->replaace()->insert($data);
//insertGetId()方法会返回插入数据的ID值
//$insert = $rs->insertGetId($data);
}
//批量增加数据
public function insertList()
{
/* $dataALL = [
[
"username" => "ac",
"password" => "ccck"
],
[
"username" => "cack",
"password" => "ack"
]
];
$rs=Db::table("admin");
$insert=$rs->insertALL($dataALL); */
}
//修改数据
public function update()
{
/* $data=[
"username" => "acbc"
];
$rs=Db::table("admin");
return $update=$rs->where("id",9)->update($data); */
//exp(需要修改的字段,使用方法)
/*
return $tr=$rs->where("id","11")
->exp("username",'UPPER("ccad")')
->update(); */
//批量操作
/* $rs=Db::table("admin");
return $rs->where("id","10")->update([
"username" =>Db::raw("UPPER(username)")
]); */
}
public function delete()
{
//极简删除
//$rs=Db::table("admin");
/* return $rs->delete(9); */
//范围删除
/* return $rs->delete([9-18]); */
//根据id删除
/* $rs=Db::table("admin");
return $rs->where("id","9")->delete(); */
//全部删除
/* return $rs->delete(true); */
}
//聚合 原子
public function poly()
{
//返回数据库行数
//$rs=Db::table("admin");
//return $rf=$rs->count();
//返回数据库中最大值 ,结果非数组将使用强行转换,true false
//return $rf=$rs->max("字段",false); min是最小值 avg 平均 sum 综合
//返回sql语句 或者是结果 true 是返回sql false执行sql语句
//return $rf=$rs->fetchSql(true)->select();
//子操作查询buildSql生成mysql代码
/* $rs=Db::table("category")->field("module")->where("module",0)->buildSql(true);
$rf=Db::table("news")->where("categ_id","exp","in".$rs)->select();
return json($rf); */
//原生sql
/* $rs=Db::query("select * from admin");
dump($rs); */
//whereRaw=>字符串查询
/* $rs=Db::table("admin")->whereRaw('username="root" and password="root"')->select();
return $rs; */
//字段查询 field()
/* $rs=Db::table("admin")->field("id,username,password")->select();
return $rs; */
//字段可使用函数 fieldRaw("字段1,方法(字段2)");
//eg:$rs=Db::table("admin")->fieldRaw("id,UPPER(username)")->select()
//可以排除不想要的字段 WhistoutField("字段");
//eg:$rs=Db::table("admin")->withoutfield("username")->select()
//验证数据的合法性
//eg:$rs=Db::table("admin")->field("id","username","password")->insert($date)
}
//给数据库起别名
public function alits()
{
/* return $rs=Db::table("admin")->alias("aaa")->select(); */
}
//数据库limit order
public function limit()
{
//limit
// $rs=Db::table("admin");
/* $ra=$rs->limit(1,3)->select();
dump($ra); */
//order
/* $rp=$rs->order("id","desc")->select();
return $rp; */
//$rp=$rs->order(["id"=>"desc","username"=>"asc"])->select();
//同样支持函数 orderRaws
//return $rp;
}
public function much()
{
//where 支持连续使用
$rs = Db::table("admin");
//$rp=$rs->where("id",1)->where("password","admin")->select();
/* $rp=$rs->where([
["id",">","0"],
["password","<>","admin"]
])->select();
//return $rp;
return Db::getLastSql(); */
}
//事务处理
public function new()
{
//自动模式
//当数据不存在,发生错误时,不会执行function里面的代码
/* Db::Transaction(function(){
$rs=Db::table("admin")->where("id",10)->save(["id"=>Db::raw("id+1")]);
$rp=Db::table("admin1")->where("id",11)->save(["id"=>Db::raw("id-1")]);
}); */
//手动模式
/* DB::startTrans();
try{
$rs=Db::table("admin")->where("id",10)->save(["id"=>Db::raw("id+1")]);
$rp=Db::table("admin1")->where("id",11)->save(["id"=>Db::raw("id-1")]);
Db::commit();
}
catch(Exception $e){
echo "调用失败,数据调用失败";
Db::rollback();
} */
}
//数据集返回
public function get()
{
//$rs=Db::table("admin")->select();
//返回成数组
//dump($rs->toarray());
//随机
// dump($rs->shuffle());
//删除最后一个并显示出来
// $rs->pop();
//显示删除的那个
//dump($rs->pop());
}
}