创建时将默认表内容插入SQLite数据库

时间:2018-07-19 01:06:37

标签: php sqlite

经过数小时的尝试,在这里@Stack寻求清晰的解决方案后,放弃了并希望获得一些投入。我承认,不精通SQLite,所以请原谅我的愚蠢...

使用一个简单的SQLite教程@ https://www.sourcecodester.com/tutorials/php/12212/sqlite-3-database-encryption.html

请注意,该教程使用OPENSSL进行加密

创建数据库+表非常简单直接。

问题是我们要插入默认表内容如果表为空 但不要覆盖现有数据

换句话说,当dbconfig.php被调用时:

<?php
//Create a new SQLite3 Database
$db = new SQLite3('members.db');

//Create a new table to our database 
$query = "CREATE TABLE IF NOT EXISTS members (firstname STRING, lastname STRING, address STRING, key STRING)";
$db->exec($query);

?>

需要插入默认的表格内容,例如:

$query = "INSERT INTO members (firstname,lastname,address,key) VALUES ('Jane','Doe','New York','key')";

//Create a new SQLite3 Database
$db = new SQLite3('members.db');

//Create a new table to our database 
$query = "CREATE TABLE IF NOT EXISTS members (firstname STRING, lastname STRING, address STRING, key STRING)";

$query = "INSERT INTO members (firstname,lastname,address,key) VALUES ('Jane','Doe','New York','key')";

$db->exec($query);

以上内容在创建表时不会插入。即使这样做,似乎也会覆盖现有数据。

也尝试过:

$query = "INSERT INTO members (firstname,lastname,address,key) VALUES ('Jane','Doe','New York','key')";

//Create a new SQLite3 Database
$db = new SQLite3('members.db');

//Create a new table to our database 
$query = "CREATE TABLE IF NOT EXISTS members (firstname STRING, lastname STRING, address STRING, key STRING)";

$query = "INSERT OR IGNORE INTO members (firstname,lastname,address,key) VALUES ('Jane','Doe','New York','key')";

$db->exec($query);

一位老师在教程站点上询问如何执行此操作,但没有答案。真正试图在学校重新开始之前完成一个项目。需要帮助,请:)

0 个答案:

没有答案