12

How to create a new database in PHP with SQLite3?

$db = new SQLite3($dbname);

just opens the db, but I want to create if not exist.

1 Answer 1

18

To create you do use the new statement:

// create or open (if exists) the database
$database = new SQLite3('myDatabase.sqlite');

If you have named a database that doesn't exist it should get created.

Sign up to request clarification or add additional context in comments.

7 Comments

try{ $db = new SQLite3($dbname); } catch(Exception $exception){ echo $exception->getMessage(); } give me : Unable to open database: unable to open database file
Have you checked the permissions of the folder where you're trying to create the new database?
Thinked I already granted permissions, my bad :-( , thanks you for your help
And how to create table with fields in it?
@user4271704 $table_create_query = "create table books(id integer pirmary key, name text, jild int, comment text)"; $db->exec($table_create_query);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.