0

Do I need to create a new statement each time I execute a query?

This line doesn't work (I have already established connection with the database )

Statement.execute("INSERT INTO PS2 (uuid , team, kills , deaths , rank_lvl, rank_name, pts) "
                    + "VALUES"
                    + " (`" + name.toString() + "` , `none` , 0 , 0 , 1 , `Starter` , 0)");

I create the table with this code :

Statement.execute("CREATE TABLE IF NOT EXISTS PS2"
                + "(uuid VARCHAR(45),"
                + "team VARCHAR(20),"
                + "kills INTEGER,"
                + "deaths INTEGER,"
                + "rank_lvl INTEGER,"
                + "rank_name VARCHAR(25),"
                + "pts INTEGER)");

After executing the code , the database has been created . Afterwards after trying to insert row, the row is not created!

10
  • Are ` backticks correct as string quotes? Try ' Commented Jul 2, 2014 at 9:47
  • There is no error , there is just no rows in the database , @AlexK. i am testing Commented Jul 2, 2014 at 9:49
  • 1
    What RDBMS do you use? Some of them are caching data and while you does'nt commit or flush there is no data written to disk. Commented Jul 2, 2014 at 9:49
  • What about the first question? Commented Jul 2, 2014 at 9:52
  • 3
    And never ever use a concat string for SQL. Use a reusable PreparedStatement. Commented Jul 2, 2014 at 9:52

2 Answers 2

2

You're using backticks ` instead of quotes ' to wrap your values. Backticks are only used to wrap column names.

Statement.execute("INSERT INTO PS2 (`uuid` , team, kills , deaths , rank_lvl, rank_name, pts) "
    + "VALUES"
    + " ('" + name.toString() + "' , 'none' , 0 , 0 , 1 , 'Starter' , 0)");

As mention by Caweren, you should be careful of SQL reserved keywords. UUID could be one of them (with an Oracle V9i database, for example), so you should wrap it with backticks.

Sign up to request clarification or add additional context in comments.

8 Comments

I would manually try this request, to know it the problem come from the request, or from the code.
It worked manually :S , using this piece of code : INSERT INTO PS2(uuid , team , kills , deaths , rank_lvl , rank_name , pts) VALUES('460b9dd9-af9c-4942-a49d-28d365658e7a' , 'none' , 0 ,0 , 1 , 'Starter', 0)
There are no errors when executing the INSERT statement BUT the row is not created!
Then it come from somewhere else. What kind of database are you using ?
Everything is correct. Does any INSERT work via PHPMyAdmin (even on other tables) ?
|
0

uuid is a reserved keyword for SQL, you should wrap it in quotes: `uuid`

2 Comments

The database got created without it being wrapped by quotes
Tables can be created using reserved words, but to use them in statements or clauses you have wrap them in quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.