DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > SQLite > 驱动及连接

在PHP5中使用PDO访问SQLite3

代码 1:

  <html>
  <?php
  $dsn = 'sqlite:sql.db';
  try
  {
  $dbh = new PDO($dsn, $user, $password);    //建立连接
  echo 'PDO Connection Ok<BR>';
   //建表
  $dbh->exec("CREATE TABLE PKU(id integer,name varchar(255))");
  //echo 'Create Table ok<BR>
';
   print("Create Table ok<BR>
");
  $dbh->exec("INSERT INTO PKU values(1,'jarjin')");
  echo 'Insert Data ok<BR>';
  $dbh->beginTransaction();
  $sth = $dbh->prepare('SELECT * FROM PKU');
  $sth->execute();
   //获取结果
  $result = $sth->fetchAll();
  print_r($result);
  $dsn=null;
  }
  catch (PDOException $e)
  {
   echo 'Connection failed: ' . $e->getMessage();
   $dsn = null;
  }
  ?>
  </html>


  代码 2:

  <html>
  <?php
   //$pdo = new PDO('sqlite::memory:');  //内存中的数据库
   $pdo = new PDO('sqlite:mydb.db');
   $pdo->exec('CREATE TABLE test(ID INT NOT NULL PRIMARY KEY, Field VARCHAR(12) NULL);');
   $stmt = $pdo->prepare('INSERT INTO test(ID, Field) VALUES(?, ?)');
   $one = 1;
   $two = 2;
   $null = NULL;
   // Try with PDO_PARAM_NULL
   $stmt->bindParam(1, $one, PDO::PARAM_INT);    //绑定参数
   $stmt->bindParam(2, $null, PDO::PARAM_STR);
   assert($stmt->execute());
   // Try with PDO_PARAM_STR
   $stmt->bindParam(1, $two, PDO::PARAM_INT);
   $stmt->bindParam(2, $null, PDO::PARAM_STR);
   assert($stmt->execute());
   // Check we have rows..
   $stmt = $pdo->prepare('SELECT * FROM test');
   assert($stmt->execute());
   var_dump($stmt->fetchAll());

   // Check we have rows with field is null

echo '<hr />';
   $stmt = $pdo->prepare('SELECT * FROM test WHERE Field IS NULL');
   assert($stmt->execute());
   var_dump($stmt->fetchAll());    //显示查询结果
  ?>
  </html>


  下载 php_pdo.dll 和 php_pdo_sqlite.dll

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号