Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 542
Svish Avatar asked Mar 11 '10 14:03

Svish


People also ask

How do I create a SQLite database in memory?

The most common way to force an SQLite database to exist purely in memory is to open the database using the special filename ":memory:". In other words, instead of passing the name of a real disk file into one of the sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2() functions, pass in the string ":memory:".

Is SQLite in memory db?

SQLite in-memory databases are databases stored entirely in memory, not on disk. Use the special data source filename :memory: to create an in-memory database. When the connection is closed, the database is deleted.

How do you create a database in memory?

Use create inmemory database to create an in-memory database, using model or another user database as its template. You can also create temporary databases as in-memory databases that reside entirely in in-memory storage. However, you cannot specify a template database for an in-memory temporary database.

Does SQLite load entire database into memory?

No, Sqlite will read data for hard disk by paging. The Sqlite document said: "In order to return data from the database to the user, for example as the results of a SELECT query, SQLite must at some point read data from the database file.

How do I create an in-memory SQLite database?

An in-memory SQLite database can be created and attached to your database connection using the ATTACH DATABASE command. An in-memory database will be saved in memory instead of being file-based. The syntax to attach an in-memory database in SQLite is:

How to create a database in SQLite?

This file will be used as database by SQLite engine. If you have noticed while creating database, sqlite3 command will provide a sqlite> prompt after creating a database file successfully. Once a database is created, you can verify it in the list of databases using the following SQLite .databases command.

What is SQLite3 command in SQLite?

In SQLite, sqlite3 command is used to create a new SQLite database. You do not need to have any special privilege to create a database. Following is the basic syntax of sqlite3 command to create a database: −

How do I export a SQLite database to a text file?

You can use .dump dot command to export complete database in a text file using the following SQLite command at the command prompt. The above command will convert the entire contents of testDB.db database into SQLite statements and dump it into ASCII text file testDB.sql.


1 Answers

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

like image 55
whlk Avatar answered Oct 07 '22 01:10

whlk