在MySQL数据库中存储0和1的系列

时间:2013-06-19 18:12:02

标签: mysql string node.js store bit

我正在使用MySql和Nodejs。我需要存储一个只有0和1的巨大字符串。 (这是一个地图网格.0 =可以移动,1 =无法移动。)

例如:00000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000赔 ...

我应该接受该字符串,将其编码为base64字符串并以此方式存储吗?然后,当我需要它时,我解码它?如果是这样,我将如何使用Nodejs / Javascript?

由于

2 个答案:

答案 0 :(得分:1)

您存储的数据似乎很容易压缩,为什么不使用内置的MySql COMPRESS函数

原材料:

mysql> select length('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000');                                                                                                                                                                                             
332

使用COMPRESS

mysql> select length(compress('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000'));                                                                                                                                                                                                                                                      
23

答案 1 :(得分:0)

不存储字符串,存储二进制文件。

以下是将数据转换为二进制的方法示例。希望您可以重新设计事物,这样您的地图数据就不会首先表示为字符串。

        static void Main(string[] args)
    {
        const string data =
            "00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000";

        var arrayLength = data.Length/8;
        if (data.Length%8 != 0)
            arrayLength++;
        var binaryData = new byte[arrayLength];

        byte nextByte=0;
        var k = -1;
        for (var i = 0; i < data.Length; i++)
        {
            var j = i%8;
            if (j == 0)
            {
                binaryData[++k] = nextByte;
                nextByte = 0;
            }

            var bit = int.Parse(data[i].ToString());
            if(bit==1)
                nextByte |= (byte)(bit << j);
        }
        binaryData[k] = nextByte;
    }