当前位置: 首页 > 工具软件 > txtSQL > 使用案例 >

txtSQL简单例子

梁兴修
2023-12-01

这几天为了文库的事情在学txtSQL,这款开源的文本数据库确实功能很强大。虽然还欠缺一些功能,不过也已经足够用了。一边看帮助手册一边写了一些简单的例子,等什么时候有空翻译一下他的帮助手册好了(里面还有一些错误需要修订)


<?php
   include('./txtSQL.class.php');
   $sql = new txtSQL('./data');
   $sql->connect('root', 'bar'); 
   
   /*创建数据库
   $sql->createdb(array('db' => 'demo')) or die('Error creating txtSQL DB, txtSQL said: '.$sql->get_last_error());
   */
  
  /*选择数据库*/
   $sql->selectdb ( 'demo' );
  
   /*创建表
   $columns = array('id'    => array('type' => 'int', 'auto_increment' => 1, 'permanent' => 1 ),
                 'name'  => array('type' => 'string', 'max' => 50), 
                 'email' => array('type' => 'string', 'max' => 50) 
                 );
   foreach($columns as $k => $v){
     echo "\$colums[$k]=";
     foreach($v as $k1 => $v1){
      echo "$k1:$v1 ";
     }
     echo "<br>";
  }
  
  $sql->execute('create table',array(
         'table' => 'table1',
         'columns' => $columns)) or die('建表发生错误:'.$sql->get_last_error());
  if($sql->table_exists('table1','demo')){
   echo "建表成功";
  }else{
   echo "建表失败";
  }
  */
  
  /*插入纪录
  $value = array(0 => array('name' => 'abc','email' => 'abc@123.com'),
         1 => array('name' => '123','email' => '123@bac.com'),
         2 => array('name' => 'df','email' => 'reg@bac.com'),
         3 => array('name' => 'trt','email' => 'hgh@bac.com'),
         4 => array('name' => 'tyty','email' => 'ytyt@bac.com')
  );
  for($i=0;$i<5;$i++){
   echo "\$value[$i]:";
   foreach($value[$i] as $k => $v){
     echo "[$k]=$v ";
   }
   echo "<br>";
  }
  for($i=0;$i<5;$i++){
   if(!$sql->execute('insert',array('table' => 'table1','values' => $value[$i] ))){
    die('An error occurred, txtSQL said: '.$sql->get_last_error()); 
   }else{
    echo "插入成功<br>";
   }
  } 
  */
  
  /*修改表数据
  $value = array('name' => 'abc','email' => 'aaa@123.com');
  if(!$sql->execute('update',array('table' => 'table1','where' => array('id = 1'),'values' => $value))){
   die('An error occurred, txtSQL said: '.$sql->get_last_error()); 
  }else{
   echo "修改成功";
  }
  */
  
  /*删除表内容
  $delete = array('table' => 'table1','where' => array('id > 1'));
  $delcount = $sql->execute('delete',$delete);
  echo $delcount;
  */
  
  /*显示表内容*/
  $select = array('table' => 'table1','orderby' => array('id','asc')/*,'where' => array('email =~ %bac%'),'limit' => array(0,9)*/);
  $data = $sql->execute('select',$select);
   echo "<table border=1><tr><th>id</th><th>name</th><th>email</th></tr>";
  foreach ( $data as $key => $row ) 
{ 
   echo "<tr>";
    foreach($row as $k => $v){
     echo "<td>$v</td>";
    }
   echo "</tr>";
}
 echo "</table>";
 
 echo "表内共有 ".$sql->table_count('table1')." 行<BR>";
 
 /*加入主键
 $sql->execute('alter table',array('table' => 'table1','action' => 'addkey','name' => 'id','values' => array('name' => 'id')));
 */
 
 echo "最后插入ID号:".$sql->last_insert_id('table1') ;
 
 $sql->disconnect();
?>
<title>Demo</title>


 类似资料: