将图像文件名插入到MySQL数据库中,可以通过以下步骤完成:
CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY,
filename VARCHAR(255)
);
mysql
模块。const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
const filename = 'image.jpg';
connection.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL database');
const sql = 'INSERT INTO images (filename) VALUES (?)';
connection.query(sql, [filename], (err, result) => {
if (err) throw err;
console.log('Image filename inserted');
connection.end();
});
});
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL database');
const sql = 'SELECT filename FROM images';
connection.query(sql, (err, result) => {
if (err) throw err;
console.log('Image filenames:');
result.forEach((row) => {
console.log(row.filename);
});
connection.end();
});
});
这样,你就可以将图像文件名存储在MySQL数据库中,并从数据库中检索它们。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云