程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Java library data interception; Python Unicode encoding

編輯:Python

The field is too long when dropping the database , Two solutions :

  1. Database field extension
  2. Interception of storage information

If the information is not particularly important , For example, log information does not need to be extended to the database . Recently, I made a small optimization , It is to intercept and drop the super long fields , avoid mybatis Report errors , This field is defined in the database as 255 Characters .
substring I should think of the most direct way , The code in my mind is like this ( It turns out that he is the same .

 public static void main(String[] args) {
String a = "\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219";
System.out.println(a);
if (a.length() > 255) {
System.out.println(a.substring(0, 255));
} else {
System.out.println(a);
}
}

First, I write a very long field directly to the library , It is found that it can be directly dropped into the warehouse , And automatically intercept my super long fields , It doesn't matter whether it's a visualization tool or mysql The version of may cause automatic interception , Interested students can start the local database to try .
But when I tested it, I found that , The length of the data directly dropped into the database is different from the length I intercepted with the above code . The intermediate process is omitted , Draw a conclusion directly , Because the data we have stored is unicode code , I'm copying string To idea When testing the above code in idea Will automatically help me add translation characters , Note that after a little processing, the result will be normal .
One more thing to note is ,mybatis Maybe because of different versions , Cause interception 255 The length of , Automatically add some data ( To be examined ), So if it must be increased to 255 Words ( The maximum length of the field in the database ), Be sure to go through local tests first .
summary :

  • Although the code is very simple , Still need self-test
  • Pay attention to special values and symbols , For example, the translator

Finally, I recommend two that I use in this process python Method :

def rawUnicode():
s1 = '\u54c8'
# It can be counted directly uni After decoding str length
print(len(s1))
# How to use python obtain uni code
s2 = ' Ha '
ss = s2.encode('raw_unicode_escape')
sss = ss.decode()
print(sss)
rawUnicode()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved