好库网首页 | 我的好库
好库网 好库网社区
IT社区 » WEB开发与应用 » PHP » PDO如何访问SQLITE
回复 发帖

查看:1489    回复:1 PDO如何访问SQLITE
访问demo10的空间
发表于 2011/2/24 14:11:12
1楼
如题!谢谢!
访问好饱的空间
发表于 2011/2/24 14:15:07
2楼
给个例子吧
<?
$dsn = 'sqlite:minicms.db';
$conn = new PDO($dsn,'','');
$cmd = $conn->prepare('SELECT * FROM doccat');
$cmd->execute();
$rs = $cmd->fetchAll();
//echo $rs[0][1];
foreach($rs as $row)
{
  echo $row["catname"].','.$row["cname"];
  //foreach ($row as $col)
  //{
  //   echo $col.',';
  //}

}
?>

插入数据例子1:
$cmd = $conn->prepare('INSERT INTO doccat(catname, cname, sortnum) VALUES(?, ?, ?)');
$cmd->bindParam(1, $catname);
$cmd->bindParam(2, $cname);
$cmd->bindParam(3, $sortnum);
$catname = 'meirong';
$cname = '美容';
$sortnum = 20;
$cmd->execute();

例子2:
$cmd = $conn->prepare('INSERT INTO doccat(catname, cname, sortnum) VALUES(?, ?, ?)');
$cmd->execute(array('meirong','美容',20));


绑定参数的例子:

<?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());  //显示查询结果
?>
您需要登录后才可以回帖 登录 | 注册
回复 发帖