如果从 DBAL 级别不存在,则创建数据库
Create database if not exists from DBAL level
我正在尝试创建这样的数据库:
const QUERY_DB_CREATE = "
CREATE DATABASE :db_name
";
if (database is not exists) {
$stmt = $conn->prepare(self::QUERY_DB_CREATE);
$stmt->bindValue(':db_name', $db_name, 'string');
return $stmt->execute();
}
错误
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "'testtesttest2'"
我做错了什么?
您应该查看 DoctrineBundle command(用于 Symfony 框架)以创建数据库。在不了解您的实施细节的情况下,这些行对您的情况尤其有用:
$tmpConnection = DriverManager::getConnection($params);
...
$tmpConnection->getSchemaManager()->createDatabase($name);
我正在尝试创建这样的数据库:
const QUERY_DB_CREATE = "
CREATE DATABASE :db_name
";
if (database is not exists) {
$stmt = $conn->prepare(self::QUERY_DB_CREATE);
$stmt->bindValue(':db_name', $db_name, 'string');
return $stmt->execute();
}
错误
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "'testtesttest2'"
我做错了什么?
您应该查看 DoctrineBundle command(用于 Symfony 框架)以创建数据库。在不了解您的实施细节的情况下,这些行对您的情况尤其有用:
$tmpConnection = DriverManager::getConnection($params);
...
$tmpConnection->getSchemaManager()->createDatabase($name);