سؤال

I've just started learning about SQL, and am trying to make a table and fill it with some data I have on an CSV-file.

The following is the application trying to do this:

public sqlThingy() throws Exception {

    File spritesheetsCreate = new File("sql/spritesheetsCreate.sql");
    File spritesheetsData = new File("sql/spritesheetsData.sql");

    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

    Connection conn = DriverManager
            .getConnection("jdbc:derby:TESTNAME;create=true");

    //CREATE TABLE
    try (PreparedStatement statement = conn
            .prepareStatement(readFile(spritesheetsCreate))) {
        statement.execute();
    }


    //FILL TABLE WITH DATA
    try (PreparedStatement ps = conn.prepareStatement(readFile(spritesheetsData))) {
        try (CSVReader rdr = new CSVReader(new FileReader(new File(
                "res/spritesheets.csv")), ',', '"', 1)) {
            for (String[] row : rdr.readAll()) {
                ps.setString(1, row[0]);
                ps.setString(2, row[1]);
                ps.setInt(3, Integer.valueOf(row[2]));
                ps.execute();
            }
        }
    }
}


private static String readFile(File f) throws Exception {
    String contents = "";
    try (Scanner sc = new Scanner(f)) {
        contents = sc.useDelimiter("\\Z").next();
    }
    return contents;
}

public static void main(String[] args) throws Exception {

    ResourceLoaderSQL test = new ResourceLoaderSQL();

}

The two SQL-files used by my app, look like this:

spritesheetsCreate.sql:

CREATE TABLE spritesheetstable( name VARCHAR(7) NOT NULL PRIMARY KEY , spritesheetlocation VARCHAR(15) , tilesize INTEGER )

spritesheetsData.sql:

INSERT INTO spritesheetstable("name", "spritesheetlocation", "tilesize") VALUES (?,?,?)

The CSV-file looks like this:

spritesheets.csv:

name,spritesheet-location,tilesize
spot1,location/loc1,10
spot2,location/loc2,20
spot3,location/loc3,30
spot4,location/loc4,40

The error I get when this beauty-to-be runs, is:

Exception in thread "main" java.sql.SQLSyntaxErrorException: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at game.io.ResourceLoaderSQL.<init>(ResourceLoaderSQL.java:36)
at game.io.ResourceLoaderSQL.main(ResourceLoaderSQL.java:83)
Caused by: java.sql.SQLException: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 16 more
Caused by: ERROR 42X14: 'name' is not a column in table or VTI 'APP.SPRITESHEETSTABLE'.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ResultColumn.bindResultColumnByName(Unknown Source)
at org.apache.derby.impl.sql.compile.ResultColumnList.bindResultColumnsByName(Unknown Source)
at org.apache.derby.impl.sql.compile.InsertNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 10 more

So can anyone spot what's going on here? I can't see the solution.

هل كانت مفيدة؟

المحلول

Quoting table names make them case sensitive.

You're creating the table with unquoted field names, which makes the column names case insensitive (and uppercase), but you're quoting them in the query with lowercase so the query can't find them.

Either remove the quoting in both places, or consistently quote the names in both create table and queries.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top