純粹記錄一下最基礎寫法,幾乎沒有寫什麼邏輯,寫法也並不是很完善(因為我自己也剛剛摸索出來這麼寫可以...= =!) 望高手指教 也希望能夠幫到比我還新的新手.... //1.insert操作 let insertSql = 'insert into User(username,password,na ...
純粹記錄一下最基礎寫法,幾乎沒有寫什麼邏輯,寫法也並不是很完善(因為我自己也剛剛摸索出來這麼寫可以...= =!) 望高手指教 也希望能夠幫到比我還新的新手....
//1.insert操作
let insertSql = 'insert into User(username,password,name,gender,age) values (?,?,?,?,?)'; let insertParams = [username, password, name, gender, age]; mysqlConnection.query(insertSql, insertParams, function (error, results, fields) { if (error) throw error; return results; }); //2.select操作(我自己測試的時候發現,字元串類型的列,要給參數額外增加引號,否則會被當做數字類型或列名來進行處理) let selectSql = 'select id from User where username = \''.concat(username, '\' and password = \'', password, '\''); mysqlConnection.query(selectSql, function (error, results, fields) { if(error) throw error; return results; }); //3.delete操作 let deleteSql = 'delete from user where id = \''.concat(id, '\''); mysqlConnection.query(deleteSql, function (error, results, fields) { if (error) throw error; return results; }); //4.update操作 let updateSql = 'update User set age = 30 where id = \''.concat(id, '\''); mysqlConnection.query(updateSql, function (error, results, fields) { if (error) throw error; return results; });