线程“main”java.sql中的异常。SQLException: near“s”:语法错误。

时间:2021-12-09 01:28:16

I wrote a program that populates a database with a list of words. Problem is, it throws up the "Exception in thread "main" java.sql.SQLException: near "s": syntax error" every time I try to run the code. I realize similar questions have been asked on this forum, but in the context of MY code I am unable to rectify the error.

我编写了一个程序,它用一个单词列表填充数据库。问题是,它在“线程”main“java.sql”中抛出“异常”。SQLException:在每次尝试运行代码时,都会出现语法错误。我意识到在这个论坛上也有类似的问题,但是在我的代码中,我无法纠正错误。

Thus I turn to you for help.

因此,我向你求助。

This is the code

这是代码

import java.io.*;
import java.sql.*;
import java.util.Scanner;

public class db_populate {

    public static void main(String[] args) throws SQLException, IOException,
            ClassNotFoundException {
        Connection c = null;
        Statement stmt = null;
        Scanner in = null;
        String word;
        String wordcat;
        String j, sql;
        int i = 0;

        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:dictionary.db");
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");

        stmt = c.createStatement();

        in = new Scanner(new File("wordsEn.txt"));

        while (in.hasNext()) {
            word = in.nextLine();
            i++;
            j = Integer.toString(i);
            wordcat = word.substring(0, 2);
            sql = "INSERT INTO WORDS (ID,CAT,WORD) " + "VALUES(" + j + ",'"
                    + wordcat + "','" + word + "');";
            stmt.executeUpdate(sql);
        }

        stmt.close();
        c.commit();
        c.close();
        in.close();
        System.out.println("Records created successfully");
    }

}

and these are the errors I get when I run.

这些是我运行时得到的错误。

Opened database successfully
Exception in thread "main" java.sql.SQLException: near "s": syntax error
    at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
    at org.sqlite.core.NativeDB._exec(Native Method)
    at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
    at db_populate.main(db_populate.java:34)

2 个解决方案

#1


6  

Use PreparedStatement to avoid problems with erroneous input:

使用PreparedStatement来避免错误输入的问题:

PreparedStatement p = c.prepare("INSERT INTO WORDS (ID,CAT,WORD) VALUES(?, ?, ?)");
p.setInt(1, i);
p.setString(2, wordcat);
p.setString(3, word);
p.execute();
//commit results if using InnoDB, etc

#2


0  

Thanks Rogue for a good code. I had an error in SQLite during INSERT operation, SQL string contained "'" inside a text. So, an escaping in this way helped to me. I've changed a code a bit.

谢谢流氓的好代码。在INSERT操作期间,我在SQLite中有一个错误,SQL字符串包含在文本中的“'”。所以,这样的逃跑对我有帮助。我已经修改了一些代码。

PreparedStatement prst = conn.prepareStatement(
    "INSERT INTO test(id, name, type) "
    + "VALUES(?, ?, ?)");
prst.setLong(1, id);
prst.setString(2, title);
prst.setObject(3, type);
prst.execute();

So when I tried to insert a null value in INTEGER column, I used a method setObject().

因此,当我试图在INTEGER列中插入null值时,我使用了一个方法setObject()。

#1


6  

Use PreparedStatement to avoid problems with erroneous input:

使用PreparedStatement来避免错误输入的问题:

PreparedStatement p = c.prepare("INSERT INTO WORDS (ID,CAT,WORD) VALUES(?, ?, ?)");
p.setInt(1, i);
p.setString(2, wordcat);
p.setString(3, word);
p.execute();
//commit results if using InnoDB, etc

#2


0  

Thanks Rogue for a good code. I had an error in SQLite during INSERT operation, SQL string contained "'" inside a text. So, an escaping in this way helped to me. I've changed a code a bit.

谢谢流氓的好代码。在INSERT操作期间,我在SQLite中有一个错误,SQL字符串包含在文本中的“'”。所以,这样的逃跑对我有帮助。我已经修改了一些代码。

PreparedStatement prst = conn.prepareStatement(
    "INSERT INTO test(id, name, type) "
    + "VALUES(?, ?, ?)");
prst.setLong(1, id);
prst.setString(2, title);
prst.setObject(3, type);
prst.execute();

So when I tried to insert a null value in INTEGER column, I used a method setObject().

因此,当我试图在INTEGER列中插入null值时,我使用了一个方法setObject()。