SQLite 中更新语句的问题 [英] problems with update statement in SQLite

查看:25
本文介绍了SQLite 中更新语句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SQLite 创建了一个数据库.我想更新功能"列的值(Blob 类型)...但我不知道如何编写更新"语句.这是我试过的:

I have created a database using SQLite. I want to update the value of a "features" column( type Blob)...but i do not know how to write the "update" statement . This is what i tried:

    try {
        stat = conn.createStatement();
    } catch (SQLException e) {
    }
    try {
        byte[] b = getFunction();
        stat.executeUpdate("update table set features="+b);
    } catch (SQLException e) {
    }

我收到以下错误:

java.sql.SQLException: unrecognized token: "[B@13a317a"

所以我猜b"是问题所在?

so i guess that "b" is the problem ?

推荐答案

用 PreparedStatement 试试这个:

Try this one with PreparedStatement:

Connection con = null;
PreparedStatement stmt = null;

try {
    byte[] b = getFunction();
    con = ...;
    stmt = con.prepareStatement("update table set features=?");
    stmt.setBytes(1, b);
    stmt.executeUpdate();

    con.commit();
}
catch (SQLException e) {
    //handle exception (consider con.rollback()) and con maybe null here)
}
finally {
    //close stmt and at least con here (all maybe null here)
}

我个人总是使用 PreparedStatements.当您必须编写大量此类代码时,请考虑编写一些实用程序类以减少样板代码.

Personally I am always using PreparedStatements. When you have to write a lot of this code then consider writing some Utility-Classes to reduce Boilerplate-Code.

特别是,当您处理普通 JDBC 时,您应该考虑为 Connection、Statement 和 ResultSet 方法上的空安全调用方法编写实用程序类.

In particular you should consider writing Utilty-Classes for null-safe calling methods on Connection, Statement and ResultSet methods when you are dealing with plain JDBC.

编辑Thomas Jung 写的关于防止 SQL 注入的内容是始终使用 PreparedStatements 的另一大优点.+1 给他 :-)

EDIT What Thomas Jung wrote about preventing SQL Injections is another big pro for always using PreparedStatements. +1 for him :-)

这篇关于SQLite 中更新语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆