简体   繁体   中英

PHP PDO connecting to SQLITE db

So this morning I try to connect to SQLite database using PDO and also create a table. Firstly, I create a file called db.sqlite, and then create my connection and execute a create table query, but the execute pdo function always returns false .

$pdo = new PDO("sqlite:db.sqlite");
$STH = $pdo->prepare(
'CREATE TABLE "users" (
     "id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , 
     "full_name" VARCHAR, 
     "description" TEXT, 
     "token" INTEGER);
    ');
 $STH->execute();

So what I did next is to remove the db.sqlite from the connection and replace with :memory: to create a db in memory, which works perfectly.

$pdo = new PDO("sqlite::memory:");

So I am confused, why can I use :memory: and not the file, and how do I fix it?

Below is the error I get when I enable exception: 在此处输入图片说明

The reason why it will not work is because the file database.sqlite was within the /var/www/ directory, this gives it an automatic RW-RW-R-- permission, meaning that you will not be able to change the structure of the file, I did two things and it worked, firstly I changed the owner of the www directory.

chown -R <your username>:<your username> www/*

Then changed the permission of the project directory

chmod -R 777 <project>/*

and it worked for me

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