gpt4 book ai didi

sql - 如何在postgresql中插入图片?

转载 作者:行者123 更新时间:2023-11-29 11:40:52 25 4
gpt4 key购买 nike

我想知道如何在我的 postgresql 表中的一个字段中插入图像。我找不到合适的教程来解决这个问题。该字段的数据类型是oid。有人试过这个吗?谢谢!

最佳答案

// All LargeObject API calls must be within a transaction
conn.setAutoCommit(false);

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();

//create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);

//open the large object for write
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);

// copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0)
{
obj.write(buf, 0, s);
tl += s;
}

// Close the large object
obj.close();

//Now insert the row into imagesLO
PreparedStatement ps = conn.prepareStatement("INSERT INTO imagesLO VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();

here 中找到示例代码.非常好的一堆sql操作。

关于sql - 如何在postgresql中插入图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com