2013-06-19 47 views
1

我正在使用MySql和Nodejs。我需要存儲一個由0和1組成的大字符串。 (這是一個地圖上的座標0 =可以移動,1 =無法移動。)在MySQL數據庫中存儲0和1的系列

例:00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000 ...

我應該採取的字符串,其編碼爲Base64字符串,並將其存儲那樣?那麼當我需要它時,我解碼它?如果是這樣,我將如何使用Nodejs/Javascript?

謝謝

回答

1

你存儲的似乎很容易壓縮的數據,你爲什麼不使用內置的MySQL COMPRESS功能

原料:

mysql> select length('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000');                                                
332 

用壓縮空氣

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

工作。謝謝。一旦壓縮,我的9216長串變長只有629。 :d – sam

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; 
    } 
+0

問題是,BINARY的最大長度與VARCHAR相同,這對於我的地圖來說太小了。 – sam

+0

沒辦法。你可以儲存8次。當只需要1位時,「1」佔用8個字節。您的64個字符需要512個字節,但可以用8個字節表示。例如1010001111000000101000000000000000000000000000000000000000000000可以用數字0xA3C0A00000000000 –

+0

來表示如果你沒有將這些數據存儲爲一個字符串,那麼你就不需要轉換它。 –