
#insert#Insert

```
$data['kid']=basKeyid::kidTemp();
$data['kno']='1';
$data['atime']=time(); 
$data['content']='123abc@q.com'; 
$db->table('xtest_keyid')->data($data)->insert(); 
```


#update#Update

```
$data['kno']='2';
unset($data['kid']);
$db->table('xtest_keyid')->data($data)->where("kid='2015-cr-f9pt2r7'")->update(); 
```


#find#Select

1. Query a row:

```
$info=$db->table('Tab-Name')->where($condition)->find(); 
Returns an array, or returns false

Fields 
$info=$db->table('Tab-Name')->field('id,title')->where($condition)->find(); 

Order 
$info=$db->table('Tab-Name')->field('id,title')->where($condition)->order('id desc')->find(); 
Notice: order('id DESC'), order('id ASC') 

Cache 
$info=$db->table('Tab-Name')->cache(10)->where($condition)->find(); 
cache($time) $time>0: Cache time; $time=0: NO-Cache; $time=-1: Permanent cache 
```

2. Query rows

```
$list=$db->table('Tab-Name')->where($condition)->select(); 
Returns an array, or returns false

Fields 
$list=$db->table('Tab-Name')->field('id,title')->where($condition)->select(); 

Order 
$list=$db->table('Tab-Name')->field('id,title')->where($condition)->order('id desc')->select(); 

Limit
$list=$db->table('Tab-Name')->field('id,title')->where($condition)->order('id desc')->limit(10)->select(); 

Cache  
$list=$db->table('Tab-Name')->cache($time)->field('id,title')->where($condition)->order('id desc')->limit(10)->select(); 
cache($time) $time>0: Cache time; $time=0: NO-Cache; $time=-1: Permanent cache 
```

3. Count

```
$count=$db->table('Tab-Name')->where($condition)->count(); 
Returns a int number, or returns false
$count=$db->table('Tab-Name')->cache($time)->where($condition)->count(); 

```

* Notice: 
 - field(),limit(),order(),cache(),table(),where(); There is no sequence of them 
 - $condition: Condition can be a string or an array, if it is an array, the array subscript is the field name, use `and` connec more then one conditions  

* Complex SQL, can be written as a native SQL query conditions


#delete#Delete

```
$condition: Condition can be a string or an array, if it is an array, eg: $condition['id']=1; 
Returns deleted rows, or returns false 
Notice: To prevent accidentally deleted all data from the table, it will not delete anything while $condition is null.
$db->table('xtest_keyid')->where("kid='2015-cr-f9pt2r7'")->delete(); 
```


#query#Common-Code

```
If $sql is the query condition, return array; and the cache can be set up: $db->cache(expire)->query($sql); 
If $sql is NOT the query condition, cache settings is NOT invalid. 
$db->query($sql); 
    --- Common code --- 
    $db->table($tabid)->data(basReq::in($fm))->insert();
    $db->table($tabid)->where("kid='$id'")->delete();
    $db->table($tabid)->data(array('enable'=>'1'))->where("kid='$id'")->update();  
    $list = $db->table('base_model')->field('kid,title')->limit(3)->select(); if($list)foreach($list as $r){}}
    $fm = $db->table($tabid)->where("model='$mod' AND kid='$kid'")->find();
    $count = $db->table('base_model')->where("pid='groups'")->count();  
    $sql = "SELECT kid,title FROM base_model_ys WHERE pid='groups' LIMIT 3";
    $list = $db->arr($sql);
    print_r($db->fields('xtest_keyid_ys'));
    print_r($db->tables());
```


#dbelse#Else

* View sql: 
 - code: echo $db->sql; or: echo $db->getSql();
 - When there is a problem with the database operation, used to view the generated SQL statement is correct

* Use the DB prefix/suffix from the DB-configs; $db->pre/$db->ext;
 - Mainly used to write the original SQL statement.

* Special data table prefix
 - $db->table('Tab-Name',$nofix=false)->
 - $nofix===2，=> return string : full-table-name 
 - $nofix===1，=> return object : $db, use org table-name
 - $nofix===0，=> return object : $db, auto added prefix/suffix

* More details code see class-file: /imcat/core/glib/glbDBObj.php


#dbtips#Notice

* Do NOT use `$db->` code IN `$db->` operation, (Sometimes, it will go wrong.)

```
eg.
$db->table('users_uacc')->data($acc+basSql::logData())->insert(); //
-=> Edit by: 
$dataex = basSql::logData(); 
$db->table('users_uacc')->data($acc+$dataex)->insert(); 
```

* System design `defect`
 - In principle, avoid using the MySQL keyword as a data table field; In this system, we used [show/key/char] words for the fields name of the db-table:
 - [show] for control the info show or hide, [key(in wex_menu/base_paras)], [char(in init_types/types_*)]; 
 - This are some small `defects` for the system, please according to the following address:
 - $list = $db->table('users_inmem')->field('uid,grade,show')->where($usql)->select(); 
 - You must Edit it as : ->field('uid,grade,\`show\`'); <!-- the char \ in the source, it is the escape symbol for makedown -->


* General sql

```
  UPDATE users_adminer_ys set uid=concat('2010-4q-',uname)
  UPDATE `users_person_ys` b INNER JOIN users_uacc_ys a ON a.uname=b.uname SET b.uid=a.uid;
  UPDATE `dede_addonarticle` SET body=REPLACE (body,'</td>',");

```