简体   繁体   中英

Are single quotations necessary in SQL queries? Specifically, Java JDBC queries

I am using MySQL Workbench as a GUI for my servers.

Looking at some of the queries that Workbench does, it seems to single quotation my database name and the column names, ie

"INSERT INTO `mydb`.`weather_data`(`dateTime`,`hourlyTemp`,`dewPoint`,`windSpeed`,`relHum`) VALUES(?,?,?,?,?)";

Now, I am using JDBC (Java) to do an automatic query. The string I use is:

String insertTableSQL = "INSERT INTO `mydb`.`weather_data`" 
                + "(`dateTime`,`hourlyTemp`,`dewPoint`,`windSpeed`,`relHum`) VALUES" 
                + "(?,?,?,?,?)";

My question is, should I use the single quotations? Should I remove them? Is it necessary?

EDIT second question:

This is my full preparedStatement();

private static void batchInsertRecordsIntoTable() throws SQLException{
        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;

        String insertTableSQL = "INSERT INTO `mydb`.`weather_data`" 
                + "(`dateTime`,`hourlyTemp`,`dewPoint`,`windSpeed`,`relHum`) VALUES" 
                + "(?,?,?,?,?)";
        try{
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);

            dbConnection.setAutoCommit(false);

            preparedStatement.setString(1, "1111111111"); // dateTime
            preparedStatement.setInt(2, 12); // hourlyTemp
            preparedStatement.setInt(3, 12); // dewPoint
            preparedStatement.setInt(4, 12); //windSpped
            preparedStatement.setInt(5, 12); //relHum
            preparedStatement.addBatch();

            preparedStatement.executeBatch();

            dbConnection.commit();

            System.out.println("Record was inserted into mydb weather_data");

        }catch(SQLException e){
            System.out.println(e.getMessage());
            dbConnection.rollback();
        }finally{
            if(preparedStatement!=null){
                preparedStatement.close();
            }
            if(dbConnection!=null){
                dbConnection.close();
            }
        }
    }

For some reason, the data is not being appended/added/inserted into my DB despite having no connection errors.

Not required, but very nice to have. You're letting the parser and any human reading it, what is a table, column, string. You should continue using them. Not using them can only hurt you, using them just adds extra bytes to your file.

You can put the table and field names in backticks, but you don't have to. The advantage of using backticks is that you may use some special characters in your field names (eg a comma). (But should anyone really want to do this?) The disadvantage is that backticks are not part of the ANSI SQL standard. So using backticks could possibly break your code if you migrate to an other SQL implementation as MSSQL.

tl;dr: Some consider it a good style to use backticks, but you are not required to do this if you do not want to use special characters in your field names.

Edited: as @a_horse_with_no_name said in the comments, MySQL is the only major system that supports backticks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM