Answer the question
In order to leave comments, you need to log in
What is the best way to store code snippets in a database?
Good afternoon!
I am writing a small java + javafx + sqlite program that will store code snippets in a database.
I encountered the following gap - the written code, when trying to add it to the database, gives an error, although if you enter just text instead of the code, then everything works.
I suspect that the problem is rather with the sanitization of symbols ... Hence the question - how best to store snippets in the database? Should the snippet be base64 encoded and rendered decoded, or do characters need to be sanitized?
Error code:
[SQLITE_ERROR] SQL error or missing database (near "publish": syntax error)
@Override
public void ButtonAdd(ActionEvent actionEvent) {
snippet.setSnippetTitle(txtTitle.getText());
snippet.setSnippetCode(txtCode.getText());
snippet.setSnippetDescription(txtDescr.getText());
// Добавляем данные в БД
try {
SQLiteConfig config = new SQLiteConfig();
c = db.getInstance().getConnection();
PreparedStatement stat = c.prepareStatement("INSERT INTO snippets (snippetTitle, snippetCode, snippetDescr) VALUES ('" + snippet.getSnippetTitle() +
"','" + snippet.getSnippetCode() + "','" + snippet.getSnippetDescription() +
"')");
stat.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
ButtonCancel(actionEvent);
}
Answer the question
In order to leave comments, you need to log in
You don't know how to cook it right. We bet it will work.
// ....
PreparedStatement stat = c.prepareStatement("INSERT INTO snippets (snippetTitle, snippetCode, snippetDescr) VALUES (?,?,?)");
stat.setString(1,snippet.getSnippetTitle());
stat.setString(2,snippet.getSnippetCode());
stat.setString(3, snippet.getSnippetDescription());
stat.executeUpdate();
// ....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question