react-native

时间:2019-12-22 22:53:42

标签: javascript mysql reactjs react-native express

我正在尝试通过react-native使用MySQL将数据插入数据库。但是,此错误总是弹出: ER_PARSE_ERROR:您的SQL语法有错误。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的''附近使用

我尝试了其他语法,例如INSERT INTO userTest (id,name,email,phone_number) VALUES ?,但仍然存在错误

代码段:

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
// Starting our app.
const app = express();

app.use(bodyParser.urlencoded({extended:true}));

const con = mysql.createPool({
  host     : '',//endpoint
  user     : 'administrator',
  password : '',
  database : 'database_3'//db name
});

var server = app.listen(9090, function(){
    var host = server.address().address
    var port = server.address().port
    console.log("Server start");
    console.log('Go check out http://localhost:9090/users');
  });

app.get('/users', function (req, res) {
    // Connecting to the database.
    con.getConnection(function (err, connection) {

    // Executing the MySQL query (select all data from the 'userTest' table).
    con.query('SELECT * FROM userTest', function (error, results, fields) {
      // If some error occurs, we throw an error.
      if (error) {
        console.log("Error in selecting value");
        throw error;
    } 
      else {
        console.log("Success");
        console.log(results);};
      res.send(results)
    });
  });
});

app.post('/users', function(req,res){
  con.query('INSERT INTO userTest SET ?', req.body, function(error,rows,fields){
    if (error) console.log('error here: ' + error );
    else{
      console.log(rows);
      res.send(JSON.stringify(rows));
    }
  })

})

App.js代码段:

export default class App extends Component {
  constructor(props){
    super(props);
    this.state={
      apiData:[],
      naData:[]
    }
    this.dataId=null;
    this.name=null;
    this.email=null;
    this.phone_number=null;
  }
  getButton=() => {
    fetch('http://172.20.10.3:9090/users',{
      method:'GET'
    }).then((responseData) => {
      return responseData.json();
    }).then((jsonData) => {
      console.log(jsonData);
      this.setState({apiData:jsonData})
      console.log(this.state.apiData)
    }).done();
    this.dataId=null;
  }

  saveButton = () => {
    fetch('http://172.20.10.3:9090/users',{
      method:'POST',
      headers:{
        'Accept':'application/json',
        'Content-Type':'application/json'
      },
      body: JSON.stringify({name:this.name, email:this.email, phone_number:this.phone_number})
    }).then((responseData) => {
      return responseData.json();
    }).then((jsonData) => {
      this.setState({naData:jsonData})
      console.log(this.state.naData)
    }).done();
    this.dataId=null;
    this.name=null;
    this.email=null;
    this.phone_number=null;

  }

  render(){...}

    
}

MySQL userTest表的详细信息。 userTest table details

0 个答案:

没有答案