企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 创建数据表和操作数据库 # 原生sql语句: ``` <pre class="calibre13">``` <span class="token5">CREATE</span> <span class="token5">TABLE</span> <span class="token5">IF</span> <span class="token5">NOT</span> <span class="token5">EXISTS</span> <span class="token3">student</span><span class="token1">(</span> <span><span class="token4">`id`</span></span> <span class="token5">INT</span> <span class="token5">AUTO_INCREMENT</span> <span class="token5">PRIMARY</span> <span class="token5">KEY</span><span class="token1">,</span> <span><span class="token4">`name`</span></span> <span class="token5">VARCHAR</span><span class="token1">(</span><span class="token5">255</span><span class="token1">)</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">DEFAULT</span> <span class="token4">''</span> <span class="token5">COMMENT</span> <span class="token4">'姓名'</span><span class="token1">,</span> <span><span class="token4">`age`</span></span> <span class="token5">TINYINT</span> <span class="token5">UNSIGNED</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">DEFAULT</span> <span class="token5">0</span> <span class="token5">COMMENT</span> <span class="token4">'年龄'</span><span class="token1">,</span> <span><span class="token4">`sex`</span></span> <span class="token5">TINYINT</span> <span class="token5">UNSIGNED</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">DEFAULT</span> <span class="token5">10</span> <span class="token5">COMMENT</span> <span class="token4">'性别'</span><span class="token1">,</span> <span><span class="token4">`create_at`</span></span> <span class="token5">INT</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">DEFAULT</span> <span class="token5">0</span> <span class="token5">COMMENT</span> <span class="token4">'新增时间'</span><span class="token1">,</span> <span><span class="token4">`update_at`</span></span> <span class="token5">INT</span> <span class="token5">NOT</span> <span class="token5">NULL</span> <span class="token5">DEFAULT</span> <span class="token5">0</span> <span class="token5">COMMENT</span> <span class="token4">'修改时间'</span> <span class="token1">)</span><span class="token5">ENGINE</span><span class="token">=</span><span class="token5">INNODB</span> <span class="token5">DEFAULT</span> <span class="token5">CHARSET</span><span class="token">=</span><span class="token5">UTF8</span> <span class="token5">AUTO_INCREMENT</span><span class="token">=</span><span class="token5">1001</span> <span class="token5">COMMENT</span><span class="token">=</span><span class="token4">'学生表'</span><span class="token1">;</span> ``` ``` # 增删改查 ``` <pre class="calibre13">``` <span class="token2">public</span> <span class="token2">function</span> <span class="token3">sqltest1</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">//查询</span> <span class="token6">// $sqldata = DB::select('select * from student');</span> <span class="token6">//新增</span> <span class="token6">//$sqldata = DB::insert('insert into student(name,age) values (?,?)',['张三1',28]); //返回true</span> <span class="token6">//dump($sqldata);</span> <span class="token6">//更新</span> <span class="token6">// $num = DB::update('update student set age =? where name = ?',[20,'张三1']);</span> <span class="token6">// dump($num); //返回1</span> <span class="token6">//查询</span> <span class="token6">// $sqldata = DB::select('select * from student where id > ?',[1002]);</span> <span class="token6">// dd ($sqldata);</span> <span class="token6">//删除</span> $num <span class="token">=</span> <span class="token5">DB</span><span class="token1">:</span><span class="token1">:</span><span class="token2">delete</span><span class="token1">(</span><span class="token4">'delete from student where id > ?'</span><span class="token1">,</span><span class="token1">[</span><span class="token5">1002</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">dd</span><span class="token1">(</span>$num<span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回1</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` <span class="token2">public</span> <span class="token2">function</span> <span class="token3">query1</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">// 查询构造器</span> <span class="token6">//$num = DB::table('student')->insert(['name'=>'abc','age'=>35]);</span> <span class="token6">//$num = DB::table('student')->insertGetId(['name'=>'we','age'=>12]);</span> $num <span class="token">=</span> <span class="token5">DB</span><span class="token1">:</span><span class="token1">:</span><span class="token3">table</span><span class="token1">(</span><span class="token4">'student'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">insert</span><span class="token1">(</span><span class="token1">[</span> <span class="token1">[</span><span class="token4">'name'</span><span class="token">=></span><span class="token4">'abc'</span><span class="token1">,</span><span class="token4">'age'</span><span class="token">=></span><span class="token5">35</span><span class="token1">]</span><span class="token1">,</span> <span class="token1">[</span><span class="token4">'name'</span><span class="token">=></span><span class="token4">'test'</span><span class="token1">,</span><span class="token4">'age'</span><span class="token">=></span><span class="token5">23</span><span class="token1">]</span> <span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $num<span class="token1">;</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` <span class="token6">// 更新数据</span> <span class="token6">// $num = DB::table('student')</span> <span class="token6">// ->where('id',1003)</span> <span class="token6">// ->update(['name'=>'ceshi1003']); 返回1</span> <span class="token6">// 自增</span> <span class="token6">//$num = DB::table('student')->where('id',1004)->increment('age',3);</span> <span class="token6">// 自减</span> <span class="token6">// $num = DB::table('student')->where('id',1004)->decrement('age',5);</span> <span class="token6">// 不带条件自减</span> <span class="token6">// $num = DB::table('student')->decrement('age',5); //返回影响的所有行数数量</span> <span class="token6">//自增的同时更新其他字段</span> <span class="token6">// $num = DB::table('student')->where('id',1004)->increment('age',5,['name'=>'gengxin']);</span> ``` ``` ``` <pre class="calibre14">``` <span class="token6">// 删除数据</span> <span class="token6">//DB::table('student')->delete(); //删除整个表</span> <span class="token6">//$num = DB::table('student')->where('id',1004)->delete(); //返回1</span> <span class="token6">// $num = DB::table('student')->where('id','>=',1004)->delete();//返回影响的行数</span> <span class="token6">//$num = DB::table('student')->truncate(); //不会任何东西 删除整个表</span> <span class="token2">return</span> $num<span class="token1">;</span> ``` ```