3

是否可以使用 dsql() 的任何方法进行查询?

就像是:

$dq=$this->api->db->dsql()->execute('insert into table xx (field1,field2) values ('a','b')');

谢谢

4

2 回答 2

3

DSQL现在被重构为一个单独的库:https ://git.io/dsql

$this->api->db->dsql()->table('table')->set('field1','a')->set('field2','b')->do_insert();

自然也可以使用

$this->api->db->dsql()->table('table')->set($associative_array)->do_insert();
于 2011-09-21T10:15:11.380 回答
1

罗马书已经展示了一个插入的例子。您可以像这样选择一个数据数组(注意 debug() 是可选的,并将在页面顶部输出生成的 sql 语句)

   $db=$this->api->db->dsql()->table('table1 x')
             ->field('x.col1')
         ->field('sum(x.col2) col2') 
  //         ->debug()
         ->join('table2 y','x.id=y.some_id')
         ->where('x.col3','Y')
         ->where('x.col4',$this->auth->api->get('id'))
         ->group('x.col2')
                     ->order('x.col1')
         ->do_getAll();

然后循环遍历结果集做某事

   foreach ($db as $row) {
      if (is_array($row)) {
         //do something here with row['col1'], row['col2']            
      }
    }

只需选择一个值

   $db=$this->api->db->dsql()->table('table1 x')
             ->field('x.col1')
         ->where('x.col2,'constant')
         ->do_getOne();

您可以更新记录

           $this->api->db->dsql()->table('table1 x')
             ->where('id',$somevariable)
             ->set('col1',$somevalue)
             ->debug()
             ->do_update();

并删除记录

            $this->api->db->dsql()->table('table1 x')
             ->where('id',$somevariable)
             ->do_delete();

在可能的情况下,最好使用如下模型来持久化您的数据,但 ATK4 的好处是它不会妨碍您,因此如果您需要,您可以在页面中的任何位置执行 SQL 以获得简单的结果。

       $s=$p->add('Model_Story')->loadData($story);
       $s->set('points', $value);
       $s->update();
于 2011-10-14T05:23:43.433 回答