[原]使用SQL将数组转换为矩阵

一个矩阵怎么保存呢?

    | 1 2 3 (ROW)
 ---+-------
  1 | 1 2 3
  2 | 4 5 6
  3 | 7 8 9
(COL)

通常的做法是保存成一个矩阵数组如下:

         R          C          V
---------- ---------- ----------
         1          1          1
         1          2          2
         1          3          3
         2          1          4
         2          2          5
         2          3          6
         3          1          7
         3          2          8
         3          3          9

但是怎么转换回去呢?这个也很简单,估计大学C语言教材里就能找到例子,这里也不多说了,那么如何用SQL来解决这个问题呢?Thinking In Sets:

首先是Oracle的例子:

with array as (
	select 1 as r, 1 as c,1 as v from dual  union all
	select 1,2,2 from dual  union all
	select 1,3,3 from dual  union all
	select 2,1,4 from dual  union all
	select 2,2,5 from dual  union all
	select 2,3,6 from dual  union all
	select 3,1,7 from dual  union all
	select 3,2,8 from dual  union all
	select 3,3,9 from dual
)
select SYS_CONNECT_BY_PATH( v, ' ') matrix
from array
where level=(select count(distinct(c)) from array)
start with c=1
connect by prior r=r 
       and prior c=c-1
order by r ;
MATRIX
--------
 1 2 3
 4 5 6
 7 8 9

我们使用了Oracle的 connect by 进行递归层次查询获得结果:

再来看看SQL Server的例子(需要SQL Server 2005或以上版本):

with array as (
	select 1 as r, 1 as c,1 as v  union all
	select 1,2,2  union all
	select 1,3,3  union all
	select 2,1,4  union all
	select 2,2,5  union all
	select 2,3,6  union all
	select 3,1,7  union all
	select 3,2,8  union all
	select 3,3,9 
),
cte as ( 
  select 1 as lvl,r,c,cast(v as varchar(50)) as line 
    from array where c=1
  union all
  select lvl+1, a.r, a.c, cast(c.line+' '+cast(a.v as varchar(10)) as varchar(50)) 
    from cte c, array a
    where c.r=a.r and a.c=c.c+1
)
select line as matrix from cte
where lvl=(select COUNT(distinct(c)) from array)
order by r;
matrix
--------
1 2 3
4 5 6
7 8 9

SQL Server 借助CTE语法实现了递归层次查询。

 

算法很简单:

第一步,找出c=1的所有数据,也就是第一列上的数据

第二步,在当前数据的同一行上(Oracle:prior r=r / SQL Server:c.r=a.r)寻找下一个数据(Oracle:prior c=c-1 / SQL Server:a.c=c.c+1)

递归调用第二步,直到找不到下一个数据为止。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将二维码保存到数据库,你可以将其转换为图像格式,然后将图像保存到数据库中。以下是在Java中实现此操作的一种方法: 1. 首先,你需要使用第三方库来生成二维码图像。一个常用的库是ZXing(Zebra Crossing)。你可以在项目的Maven或Gradle配置文件中添加以下依赖项: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` 2. 接下来,你可以使用以下代码生成二维码图像并保存到数据库: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; public class QRCodeSaver { public static void main(String[] args) { String data = "Hello, World!"; // 二维码内容 try { // 生成二维码矩阵 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); BitMatrix matrix = new MultiFormatWriter().encode(data, BarcodeFormat.QR_CODE, 200, 200, hints); // 将矩阵转换为图像 BufferedImage image = new BufferedImage(matrix.getWidth(), matrix.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < matrix.getWidth(); x++) { for (int y = 0; y < matrix.getHeight(); y++) { int color = matrix.get(x, y) ? 0x000000 : 0xFFFFFF; image.setRGB(x, y, color); } } // 将图像保存到字节数组 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); byte[] imageBytes = baos.toByteArray(); // 将图像保存到数据库 saveToDatabase(imageBytes); } catch (Exception e) { e.printStackTrace(); } } private static void saveToDatabase(byte[] imageBytes) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); statement = connection.prepareStatement("INSERT INTO qrcode_table (image) VALUES (?)"); statement.setBytes(1, imageBytes); statement.executeUpdate(); } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } } ``` 请注意,上述代码中的数据库连接细节(数据库URL、用户名和密码)应根据你的实际情况进行修改。此外,你还需要创建一个名为`qrcode_table`的表,其中包含一个名为`image`的BLOB列,用于保存二维码图像。 该代码将生成一个200x200像素的二维码图像,并将其保存为PNG格式的字节数组。然后,它将使用JDBC将该字节数组保存到数据库中。 请确保在运行代码之前已经正确配置了数据库连接,并根据实际情况修改数据库连接细节和表结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值