简体   繁体   中英

Create SQLite database in memory

Trying to learn a bit about PDO and is going through this tutorial . It has the following snippet of code:

<?php

  try
  {
    $db = new PDO('sqlite::memory');
    echo "SQLite created in memory.";
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }

When I run this I get the following exception message:

SQLSTATE[HY000] [14] unable to open database file

What does that mean? How can I get it to work? I am able to connect to a MySQL database and also a regular SQLite database file. So I know at least something is working...

I'm on Windows 7 64-bit with Apache 2.2.11 and PHP 5.3.0 (latest WampServer install). phpinfo() reports that I have pdo_sqlite with SQLite Library 3.6.15 enabled.

You have to write

$db = new PDO('sqlite::memory:');

the trailing : is missing.


The PDO_SQLITE Data Source Name (DSN) is composed of the following elements:

The DSN prefix is sqlite: .

  • To access a database on disk, append the absolute path to the DSN prefix.

  • To create a database in memory, append :memory: to the DSN prefix.

Documentation

不确定@win 权限,但在 *nix 上,SQLite 需要将临时文件的 dir 限制数据库文件写入权限。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM