|
|
| 回复:PostgreSQL 中存大对象有哪些优缺点? 六, 25 十二月 2004 23:16
sum_z 引用 2004-12-25 14:23
laser 引用 2004-12-21 10:36
我一般不建议用lo_*接口,建议用bytea类型存储二进制流,
用text/varchae()类型存储文本流。这些字段是变长字段,不应该有你
说的问题。
如果用bytea的话,insert/update的时候,岂不是要把整个二进制流都写到SQL语句中?每个字节都转义一下?!还是有别的什么办法?
不同的语言有自己的方法,给个java例子
String url = "jdbc:postgresql://localhost/xxx";
String user = "xx";
String password = "xxx";
String sql = "INSERT INTO images (name, content) VALUES (?, ?)";
Connection db = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
try
{
File f = new File("d:/work/aaa.jpg");
is = new BufferedInputStream(new FileInputStream(f));
db = DriverManager.getConnection(url, user, password);
ps = db.prepareStatement(sql);
int i = 1;
ps.setString(i++, "aaa.jpg");
ps.setBinaryStream(i++, is, (int) f.length());
ps.executeUpdate();
}
finally
{
if (is!=null) try catch (Exception e) {}
if (rs!=null) try catch (Exception e) {}
if (ps!=null) try catch (Exception e) {}
if (db!=null) try catch (Exception e) {}
} |
|