MySQL NODEJS: A Complete Guide to Creating Databases and Related Tables
P粉682987577
2023-08-26 14:44:38
<p>I'm setting up a mysql/nodejs application. </p>
<p>I want to drop the database, recreate the database, and create the tables every time the server restarts. </p>
<p>If I specify a connection to the database: </p>
<pre class="brush:js;toolbar:false;">let con = mysql.createConnection({
host: "localhost",
user: "root",
password: "pass",
database: "my_db",
});
</pre>
<p>I can create tables, indexes, and insert values, but only the first time. Every time after that it tells me that everything has been created. </p>
<p>On the other hand, if I don't specify the database when creating the connection, I can drop the database, create a new one, but when I try to create the table, I get an error saying I don't have a database associated with the table . </p>
<p>Is there any way to solve this problem? </p>
You don’t need to use a database in the connection
After that you can create the database
Then use
Connect to the newly created database
This is just to show the idea and split it into separate functions.