- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个 Java 工具,将 StarDict 数据库转换为 SQLite 数据库,以便与 Android 词典应用程序一起使用。但是我收到以下错误:
java.sql.SQLException: near "-": syntax error at
org.sqlite.DB.throwex(DB.java:288) at
org.sqlite.NativeDB.prepare(Native Method) at
org.sqlite.DB.prepare(DB.java:114) at
org.sqlite.Stmt.executeUpdate(Stmt.java:102) at
com.trivisionsc.ConvertData.createData(ConvertData.java:353) at
com.trivisionsc.ConvertData.excuteConvert(ConvertData.java:314) at
com.trivisionsc.ConvertData$1.actionPerformed(ConvertData.java:116) at
javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.setPressed(Unknown Source) at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at
java.awt.Component.processMouseEvent(Unknown Source) at
javax.swing.JComponent.processMouseEvent(Unknown Source) at
java.awt.Component.processEvent(Unknown Source) at
java.awt.Container.processEvent(Unknown Source) at
java.awt.Component.dispatchEventImpl(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Window.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.EventQueue.dispatchEvent(Unknown Source) at
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at
java.awt.EventDispatchThread.pumpEvents(Unknown Source) at
java.awt.EventDispatchThread.pumpEvents(Unknown Source) at
java.awt.EventDispatchThread.run(Unknown Source)
这是来源:
package com.trivisionsc;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class ConvertData extends JFrame
{
// Variables declaration
int count=0;
private static final long serialVersionUID = 1L;
private JLabel lblIndexFilePath;
private JLabel lblDictFilePath;
private JLabel lblDisplayProcess;
private JTextField txtSourceFilePath;
private JTextField txtDbFilePath;
private JButton btnPerform;
private JButton btnStop;
private JButton btnIndexFileSelect;
private JButton btnDictFileSelect;
private JPanel contentPane;
private JFileChooser fc;
private String messageResult;
private int start;
private int countWord;
private int numberWord;
int result;
boolean stop;
// End of variables declaration
public ConvertData()
{
super();
createLayout();
this.setVisible(true);
}
private void createLayout()
{
// Initialize
lblIndexFilePath = new JLabel();
lblDictFilePath = new JLabel();
lblDisplayProcess= new JLabel();
txtSourceFilePath = new JTextField();
txtDbFilePath = new JTextField();
btnPerform = new JButton();
btnStop=new JButton();
btnIndexFileSelect=new JButton();
btnDictFileSelect=new JButton();
contentPane = (JPanel)this.getContentPane();
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//lblDisplayProcess
lblDisplayProcess.setForeground(new Color(255, 0, 0));
lblDisplayProcess.setHorizontalAlignment(SwingConstants.LEFT);
//lblIndexFilePath
lblDictFilePath.setHorizontalAlignment(SwingConstants.LEFT);
lblIndexFilePath.setText(" Path store source dict");
//lblDictFilePath
lblDictFilePath.setHorizontalAlignment(SwingConstants.LEFT);
lblDictFilePath.setText(" Path store database");
// txtSourceFilePath
txtSourceFilePath.setForeground(new Color(0, 0, 255));
txtSourceFilePath.setToolTipText("Enter path store source dict");
// txtDbFilePath
txtDbFilePath.setForeground(new Color(0, 0, 255));
txtDbFilePath.setToolTipText("Enter path store database");
// btnPerform
btnPerform.setText("Convert");
btnPerform.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//insert database
excuteConvert();
}
});
// btnStop
btnStop.setText("Stop");
btnStop.setEnabled(false);
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
stop=true;
}
});
// btnIndexFileSelect
btnIndexFileSelect.setText("Browser...");
btnIndexFileSelect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int returnVal = fc.showOpenDialog(ConvertData.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
txtSourceFilePath.setText(file.getAbsolutePath());
}
}
});
// btnDictFileSelect
btnDictFileSelect.setText("Browser...");
btnDictFileSelect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int returnVal = fc.showOpenDialog(ConvertData.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
txtDbFilePath.setText(file.getAbsolutePath());
}
}
});
// contentPane
contentPane.setLayout(null);
contentPane.setBorder(BorderFactory.createEtchedBorder());
// add component for Frame
addComponent(contentPane, lblIndexFilePath, 35,10,126,18);
addComponent(contentPane, lblDictFilePath, 35,47,126,18);
addComponent(contentPane, txtSourceFilePath, 160,10,203,22);
addComponent(contentPane, btnIndexFileSelect, 365,9,80,25);
addComponent(contentPane, txtDbFilePath, 160,45,203,22);
addComponent(contentPane, btnDictFileSelect, 365,44,80,25);
addComponent(contentPane, btnPerform, 160,75,90,30);
addComponent(contentPane, btnStop, 250,75,90,30);
addComponent(contentPane, lblDisplayProcess, 35,110,250,18);
//set title for program
this.setTitle("Convert data for TDict");
// set icon for program
ImageIcon receivedIcon = new ImageIcon("resource\\images\\tri.png");
Image logoImg=receivedIcon.getImage();
this.setIconImage(logoImg);
//set position display
this.setLocation(new Point(400, 300));
//set size display
this.setSize(new Dimension(500, 200));
//set event close window
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//set disable resize
this.setResizable(false);
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
public void excuteConvert()
{
//set empty for display result run
lblDisplayProcess.setText("");
//name dictionary
String dictName=null;
// path to file
String sourceStorePath=txtSourceFilePath.getText();
String dbStorePath=txtDbFilePath.getText();
// path file source dictionary don't exits
File directorySource = new File(sourceStorePath);
// path file source dictionary don't exits
if (sourceStorePath==null || sourceStorePath.equals("")||!directorySource.exists())
{
JOptionPane.showMessageDialog(ConvertData.this,"Path is invalid!","Error",JOptionPane.ERROR_MESSAGE,null);
txtSourceFilePath.requestFocus();
return;
}
// path file database don't exits
File directoryDb = new File(dbStorePath);
if (dbStorePath==null || dbStorePath.equals("")||!directoryDb.exists())
{
JOptionPane.showMessageDialog(ConvertData.this,"Path is invalid!","Error",JOptionPane.ERROR_MESSAGE,null);
txtDbFilePath.requestFocus();
return;
}
// check in source directory include: file index,dictionary and information?
File[] files = directorySource.listFiles();
int check=0;
if (files.length>0)
{
for (int i = 0; i < files.length; i++)
{
if(files[i].getName().endsWith(".idx"))
{
check++;
dictName=files[i].getName().replaceAll(".idx", "");
}
if(files[i].getName().endsWith(".dict"))
check++;
if(files[i].getName().endsWith(".ifo"))
check++;
}
}
else
{
// Folder don't include any file
JOptionPane.showMessageDialog(ConvertData.this,"Source directory have to include files: .idx,.dict and .ifo","Error",JOptionPane.ERROR_MESSAGE,null);
txtSourceFilePath.requestFocus();
return;
}
if(check<3)
{
// Folder don't include full file
JOptionPane.showMessageDialog(ConvertData.this,"Source directory have to include files: .idx,.dict and .ifo","Error",JOptionPane.ERROR_MESSAGE,null);
txtSourceFilePath.requestFocus();
return;
}
// path file information
String infoFilePath=sourceStorePath+"\\"+dictName+".ifo";
// path file index
String indexFilePath=sourceStorePath+"\\"+dictName+".idx";
//path file content
String dataFilePath=sourceStorePath+"\\"+dictName+".dict";
// read file information to get max size file index
int idxFileSize=0;
try
{
// open stream read file
FileInputStream fstream=new FileInputStream(infoFilePath);
DataInputStream in = new DataInputStream(fstream);
BufferedReader input = new BufferedReader (new InputStreamReader(in));
String strLine;
int k=0;
//Read File Line By Line
while ((strLine = input.readLine()) != null) {
if (strLine.contains("idxfilesize="))
{
String valueStr=strLine.replaceAll("idxfilesize=", "");
idxFileSize = Integer.parseInt(valueStr.trim());
k++;
}
if (strLine.contains("wordcount="))
{
String valueStr=strLine.replaceAll("wordcount=", "");
numberWord = Integer.parseInt(valueStr.trim());
k++;
}
if(k>1) break;
}
//Close the input stream
input.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(ConvertData.this,"System error!Please try again","Error",JOptionPane.ERROR_MESSAGE,null);
setActive();
return;
}
// perform convert
this.createData(dbStorePath,indexFilePath,dataFilePath,idxFileSize,dictName);
}
@SuppressWarnings("deprecation")
public void createData(String dbStorePath,String indexFilePath,String dataFilePath,int idxFileSize,String dictName){
// Open connect database SQLite
try
{
File checkFile = new File(dbStorePath+"\\"+dictName+".db");
if (checkFile.exists())
{
result =JOptionPane.showConfirmDialog((ConvertData.this), "Are you sure to overwrite?", "File existed!", JOptionPane.YES_NO_OPTION);
if (result==1)
{
setActive();
return;
}
else
{
boolean isDeleteSuccess=checkFile.delete();
if (!isDeleteSuccess)
{
JOptionPane.showMessageDialog(ConvertData.this,"Delete file unsuccessfully. Please restart program. ","Error",JOptionPane.ERROR_MESSAGE,null);
setActive();
return;
}
}
}
//Disable controls before insert database
setUnActive();
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+dbStorePath+"\\"+dictName+".db");
final Statement st = conn.createStatement();
// Create table and index
st.executeUpdate("CREATE TABLE IF NOT EXISTS "+dictName+"(Word TEXT NOT NULL PRIMARY KEY, Content TEXT,Id INTEGER NOT NULL);");
st.executeUpdate("CREATE INDEX wrod_idx ON "+dictName+"(Id);");
// Read file
FileInputStream fileIndex=new FileInputStream(indexFilePath);
final BufferedInputStream input = new BufferedInputStream(fileIndex);
// File content
FileInputStream fileDict=new FileInputStream(dataFilePath);
final BufferedInputStream dictInput = new BufferedInputStream(fileDict);
// Array store data read form File index
final byte[] data = new byte[idxFileSize];
input.read(data);
final String dictionName=dictName;
countWord=0;
// Read data
new Thread(new Runnable() {
public void run() {
for (int i=0; i < data.length; i++)
{
if (data[i] == '\0')
{
try
{
//Read data form index
int lengthOfData = i - start;
byte[] tmp = new byte[lengthOfData];
System.arraycopy(data, start, tmp, 0, lengthOfData);
int length = byteArrayToInt(data, i+5);
//Word
String word = new String(tmp, "UTF-8");
//Read content from file data
byte[] value = new byte[length];
dictInput.read(value);
//Content
String content = new String(value,"UTF-8");
i += 9;
start = i;
// insert into database
st.executeUpdate("INSERT INTO "+dictionName+"(Word,Content,Id) VALUES ('"+Utility.encodeContent(word)+"','"+Utility.encodeContent(content)+"',"+countWord+")");
messageResult="Executing .... Insert element "+countWord+"/"+numberWord;
countWord++;
if (stop)
{
setActive();
messageResult="Cancel";
i=data.length;
st.close();
}
}
catch(Exception e)
{
;
}
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
lblDisplayProcess.setText(messageResult);
}
}
);
try
{
Thread.sleep(1);
} catch(Exception e)
{
JOptionPane.showMessageDialog(ConvertData.this,"System error!Please try again","Error",JOptionPane.ERROR_MESSAGE,null);
setActive();
return;
}
}
}
try
{
if(!stop&&countWord==numberWord)
{
setActive();
lblDisplayProcess.setText("Completed! ");
st.close();
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(ConvertData.this,"System error!Please try again","Error",JOptionPane.ERROR_MESSAGE,null);
setActive();
return;
}
///
}
}).start();
}catch(Exception e)
{
JOptionPane.showMessageDialog(ConvertData.this,"System error!Please try again","Error",JOptionPane.ERROR_MESSAGE,null);
setActive();
return;
}
}
//method to calculate value of byteArray
public static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
public void updateControl() throws Throwable
{
for (int i=0; i<100; i++) {
System.out.println("thread "
+Thread.currentThread().getName()+" step "+i);
Thread.sleep(500);
}
}
public final void setActive()
{
//set for control enable
txtDbFilePath.setEnabled(true);
txtSourceFilePath.setEnabled(true);
btnIndexFileSelect.setEnabled(true);
btnDictFileSelect.setEnabled(true);
btnPerform.setEnabled(true);
btnStop.setEnabled(false);
}
public final void setUnActive()
{
//set for control disable
txtDbFilePath.setEnabled(false);
txtSourceFilePath.setEnabled(false);
btnIndexFileSelect.setEnabled(false);
btnDictFileSelect.setEnabled(false);
btnPerform.setEnabled(false);
btnStop.setEnabled(true);
}
public static void main(String[] args)
{
//set Look and Feel
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
//perform run program convert data for TDict
new ConvertData();
};
}
有什么想法吗?非常感谢。
最佳答案
问题在于:
st.executeUpdate("CREATE TABLE IF NOT EXISTS "+dictName+"(Word TEXT NOT NULL PRIMARY KEY, Content TEXT,Id INTEGER NOT NULL);");
据推测,dictName
包含一个-
字符,它不是SQL 中纯文字的合法部分。清理或在其周围放置 "
双引号"
字符。他们需要反斜杠引号作为 Java 字符串的一部分。但是清理(例如,通过去除所有非字母字符)要好得多。
关于java - 错误 java.sql.SQLException : near "-": syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7786037/
reqwest v0.9 将 serde v1.0 作为依赖项,因此实现 converting serde_json errors into reqwest error . 在我的代码中,我使用 se
我有这个代码: let file = FileStorage { // ... }; file.write("Test", bytes.as_ref()) .map_err(|e| Mu
我只是尝试用angular-cli创建一个新项目,然后运行服务器,但是它停止并显示一条有趣的消息:Error: No errors。 我以这种方式更新了(希望有帮助):npm uninstall -g
我从我的 javascript 发送交易 Metamask 打开传输对话框 我确定 i get an error message in metamask (inpage.js:1 MetaMask -
这个问题在这里已经有了答案: How do you define custom `Error` types in Rust? (3 个答案) How to get a reference to a
我想知道两者之间有什么大的区别 if let error = error{} vs if error != nil?或者只是人们的不同之处,比如他们如何用代码表达自己? 例如,如果我使用这段代码: u
当我尝试发送超过 50KB 的图像时,我在 Blazor 服务器应用程序上收到以下错误消息 Error: Connection disconnected with error 'Error: Serv
我有一个error-page指令,它将所有异常重定向到错误显示页面 我的web.xml: [...] java.lang.Exception /vi
我有这样的对象: address: { "phone" : 888, "value" : 12 } 在 WHERE 中我需要通过 address.value 查找对象,但是在 SQL 中有函数
每次我尝试编译我的代码时,我都会遇到大量错误。这不是我的代码的问题,因为它在另一台计算机上工作得很好。我尝试重新安装和修复,但这没有帮助。这是整个错误消息: 1>------ Build starte
在我的代码的类部分,如果我写一个错误,则在不应该的情况下,将有几行报告为错误。我将'| error'放在可以从错误中恢复的良好/安全位置,但是我认为它没有使用它。也许它试图在某个地方恢复中间表情? 有
我遇到了 csv 输入文件整体读取故障的问题,我可以通过在 read_csv 函数中添加 "error_bad_lines=False" 来删除这些问题来解决这个问题。 但是我需要报告这些造成问题的文
在 Spring 中,验证后我们在 controller 中得到一个 BindingResult 对象。 很简单,如果我收到验证错误,我想重新显示我的表单,并在每个受影响的字段上方显示错误消息。 因此
我不知道出了什么问题,因为我用 Java 编程了大约一年,从来没有遇到过这个错误。在一分钟前在 Eclipse 中编译和运行工作,现在我得到这个错误: #A fatal error has been
SELECT to_char(messages. TIME, 'YYYY/MM/DD') AS FullDate, to_char(messages. TIME, 'MM/DD
我收到这些错误: AnonymousPath\Anonymized.vb : error BC30037: Character is not valid. AnonymousPath\Anonymiz
我刚刚安装了 gridengine 并在执行 qstat 时出现错误: error: commlib error: got select error (Connection refused) erro
嗨,我正在学习 PHP,我从 CRUD 系统开始,我在 Windows 上安装了 WAMP 服务器,当我运行它时,我收到以下错误消息。 SCREAM: Error suppression ignore
我刚刚开始一个新项目,我正在学习核心数据教程,可以找到:https://www.youtube.com/watch?v=zZJpsszfTHM 我似乎无法弄清楚为什么会抛出此错误。我有一个名为“Exp
当我使用 Jenkins 运行新构建时,出现以下错误: "FilePathY\XXX.cpp : fatal error C1853: 'FilePathZ\XXX.pch' precompiled
我是一名优秀的程序员,十分优秀!