Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBaljot Malhi
3,953 PointsInsert exactly one row into languages table in SQL connecting to a database
I do not know what is wrong with the code below can anyone help me out with this error it show when I run it.
Bummer! It looks like you don't have an executeUpdate
call that contains the proper INSERT statement. Check your SQL syntax, as well as column names and values.
// Load class "org.sqlite.JDBC"
Class.forName("org.sqlite.JDBC");
// Establish connection to database named "treehouse.db"
try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db") ){
Statement statement = c.createStatement();
statement.executeUpdate( "DROP TABLE IF EXISTS languages");
statement.executeUpdate("CREATE TABLE languages (id INTEGER PRIMARY KEY, name STRING" );
statement.executeUpdate(" INSERT INTO languages (id, name) "+
" VALUES( 2, 'Java')");
} catch(SQLException ex) {
System.err.printf("There was a database error: %s%n",ex.getMessage());
}
Iris Guo
4,523 PointsI see. We should delete the space before the INSERT keyword to pass the online judge.
Roger Rainer S. Braun
23,616 PointsI think so too. Try deleting the space before "INSERT INTO languages". If that doesn't work try to remove the concatenation operator and write it as one query.
Benjamin Smith
18,184 PointsClass.forName("org.sqlite.JDBC");
try(Connection c = DriverManager.getConnection("jdbc:sqlite:treehouse.db") ){ Statement statement = c.createStatement(); statement.executeUpdate( "DROP TABLE IF EXISTS languages");
// You were missing ")" after "STRING"
statement.executeUpdate("CREATE TABLE languages (id INTEGER PRIMARY KEY, name STRING)" );
// You do not need to concatenate your SQL call.
statement.executeUpdate("INSERT INTO languages (id, name) VALUES( 2, 'Java')");
} catch(SQLException ex) {
}
Iris Guo
4,523 PointsIris Guo
4,523 PointsI have the same question! Have you found the answer?