Javascript Sqlite3将输入字段值放入insert语句
我有一个包含4列的sqlite数据库Javascript Sqlite3将输入字段值放入insert语句,javascript,node.js,sqlite,electron,Javascript,Node.js,Sqlite,Electron,我有一个包含4列的sqlite数据库 Name Age bloodGroup lastdate 和4个输入字段和保存按钮,如下所示: <input type="text" name="" id="patientName"> <input type="number" name="" id="PatientAge"> <input type="text" name="" id="PatientBloodGroup"> <input type="date"
Name
Age
bloodGroup
lastdate
和4个输入字段和保存按钮,如下所示:
<input type="text" name="" id="patientName">
<input type="number" name="" id="PatientAge">
<input type="text" name="" id="PatientBloodGroup">
<input type="date" name="" id="PatientLastDate">
<button id="savebtn"> Save </button>
拯救
我使用以下javascript代码获取输入值并将其插入数据库的列中:
<script type="text/javascript">
document.getElementById('savebtn').addEventListener('click', saveFunction);
function saveFunction(){
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./database_name');
var patientName = document.getElementById('patientName').value;
var patientAge = document.getElementById('patientAge').value;
var patinetBloodGroup = document.getElementById('patientBloodGroup').value;
var PatientLastDate = document.getElementById('patientLastDate').value;
db.run(`INSERT INTO info(Name, Age, bloodGroup, lastdate) VALUES(patientName, patientAge, patientBloodGroup, PatientLastDate), function(err) {
if (err) {
return console.log(err.message);
}
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
db.close();
}
</script>
document.getElementById('savebtn')。addEventListener('click',saveFunction);
函数saveFunction(){
const sqlite3=require('sqlite3').verbose();
让db=newsqlite3.Database('./数据库名称');
var patientName=document.getElementById('patientName').value;
var patientAge=document.getElementById('patientAge').value;
var patinebloodgroup=document.getElementById('patientBloodGroup')。值;
var PatientLastDate=document.getElementById('PatientLastDate').value;
run(`INSERT-INTO-info(Name,Age,bloodGroup,lastdate)值(patientName,patientTage,patientBloodGroup,PatientLastDate),函数(err){
如果(错误){
返回console.log(错误消息);
}
log(`rowid${this.lastID}`插入了一行);
});
db.close();
}
程序运行时,会显示以下错误消息:
SQLITE_错误:没有这样的列:patientName
这看起来很愚蠢,但是你没有用引号括住这些值,也没有计算变量。
插入到
查询的形式为
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
因此,将db查询更改为:
db.run(`INSERT INTO info(Name, Age, bloodGroup, lastdate) VALUES('${patientName}', '${patientAge}', '${patientBloodGroup}', '${PatientLastDate}')`), function(err) {
if (err) {
return console.log(err.message);
}
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
现在,这段代码显然容易受到SQL注入的影响。你应该使用事先准备好的语句
db.run(`INSERT INTO info(Name, Age, bloodGroup, lastdate) VALUES(?, ?, ?, ?)`, patientName, patientAge, patientBloodGroup, PatientLastDate), function(err) {
if (err) {
return console.log(err.message);
}
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
你的数据库模式是什么?它说表
info
在itI中没有列Name
。很抱歉,我写错了错误消息。实际的错误消息如下:SQLITE_错误:没有这样的列:patientName@aritrachakraborty问题在于,您运行的是一个文本SQL字符串,而没有替换中的变量值。大多数数据库将此解释为从名为patientName
的字段中获取值的指令,这显然不是您想要的。尝试类似此页面上的“插入多条记录”部分:(显然,您只需要一条记录。当我在w3school中尝试此示例时,错误更改为:SQLITE_error:1 4列@GregHNZ的值