1. 處理 sql server 導出的 datetime 類型的字段java
在進行sql server向mysql等其餘數據進行遷移數據時,會發現使用sql server導出的datetime類型的結果是16進製表示的二進制的結果,相似於:CAST(0x00009E0E0095524F AS DateTime),這樣形式的datetime是沒法向其餘數據庫插入的,因此須要將這種表現形式進行轉換。搜索了好久,纔在在stackoverflow上找到正確的轉換方法。在網上看到不少人都這個問題都不知道解決辦法,本文采用Java語言根據stackoverflow介紹的原理,進行編碼實現轉換。mysql
注意,由於datetime分爲了4字節形式(SmallDateTime)和8字節形式(DateTime),全部對應的又兩種轉換方法。另外本轉義方法,只能精確到秒級別,毫秒級別是不精確的。若是須要徹底精確的轉換,請採用其餘工具進行轉換,好比sqlyog, mysql workbench等工具。正則表達式
下面是具體的代碼:sql
import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * sql server數據庫在向mysql等數據庫遷移數據時,會遇到一個麻煩,sql server導出的datetime的結果是16進制形式的二進制結果, * 相似於 CAST(0x00009E0E0095524F AS DateTime),這樣的導出結果是沒法直接向mysql數據庫中導入的,因此須要對sql server * 導出的腳本中的全部的 datetime 字段類型進行轉換,轉換成mysql等數據庫承認的結果:2010-10-13 09:03:39 * 才能正確的完成sql server數據向mysql等數據庫的遷移。 * 注意本方法只能精確到秒級別,毫秒級別是不精確的。 * 具體轉換原理,參見:http://stackoverflow.com/questions/12033598/cast-hex-as-datatime-how-to-get-date * @author digdeep@126.com */ public class SqlServerDateTimeUtils { private static final Calendar cal = Calendar.getInstance(); static{ cal.set(Calendar.YEAR, 1900); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); // 1900-01-01 00:00:00 } /** * 將sql server導出的16進制datetime字段: CAST(0x00009E0E0095524F AS DateTime) 形式轉換成字符串形式: * 2010-10-13 09:03:39 ,以利於 sql server 向 mysql, oracle 遷移數據 * @param rawDateTime * @return * reference: http://stackoverflow.com/questions/12033598/cast-hex-as-datatime-how-to-get-date */ public static String convertRawDateTimeToStr(String rawDateTime){ return convertRawToStr(rawDateTime, false); } /** * 將sql server導出的datetime字段結果 CAST(0x00009E0E0095524F AS DateTime),轉換成 * 2010-10-13 09:03:39 或者 2010-10-13 09:03:39.394 注意毫秒部分不精確 * @param rawDateTime * @param millisecondFlag 結果是否須要帶毫秒部分 * @return * reference: http://stackoverflow.com/questions/12033598/cast-hex-as-datatime-how-to-get-date */ public static String convertRawToStr(String rawDateTime, boolean millisecondFlag) { if (rawDateTime == null || rawDateTime.trim().equals("")) return null; String rawData = rawDateTime.substring("CAST(".length(), rawDateTime.toUpperCase().indexOf(" AS DATETIME")); if (rawData == null || rawData.trim().equals("")) return null; rawData = rawData.trim(); String result = null; if(rawData.length() <= 10){ // rowData = "0x993a02CE" result = getDateTimeStr4Bytes(rawData); } if(rawData.length() > 10 && rawData.length() <= 18){ // rowData = "0x00009E0E0095524F" result = getDateTimeStr8Bytes(rawData, millisecondFlag); } return result; } /** * sql server 利用 SmallDateTime 4個字節 * select CAST(0x993902CE as SmallDateTime); * 2007-05-25 11:58:00 (只精確到分鐘???) * * mysql: * SELECT "0x993902CE" INTO @raw_data; * SELECT conv(substr(@raw_data, 3, 4), 16, 10) INTO @days; * SELECT conv(substr(@raw_data, 7, 4), 16, 10) INTO @minutes; * SELECT "1900-01-01 00:00:00" INTO @start_date; * SELECT date_add(@start_date, interval @days DAY) INTO @date_plus_years; * SELECT date_add(@date_plus_years, interval @minutes MINUTE) INTO @final_date; * select @final_date; * 2007-05-25 11:58:00 * @param rawData * @return */ private static String getDateTimeStr4Bytes(String rawData){ String day = rawData.substring(2, 2 + 4); // rowData = "0x993a02CE" String minutes = rawData.substring(6, 6 + 4); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(cal.getTimeInMillis()); // 1900-01-01 00:00:00 calendar.add(Calendar.DATE, Integer.parseInt(day, 16)); calendar.add(Calendar.MINUTE, Integer.parseInt(minutes, 16)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(calendar.getTime()); } /** * sql server DateTime 利用 8 字節表示, 該轉換精確到秒,無毫秒部分 * @param rawData * @param millisecondFlag 結果是否須要帶毫秒部分 * @return * */ private static String getDateTimeStr8Bytes(String rawData, boolean millisecondFlag){ String day = rawData.substring(2, 2 + 8); // 4字節表示距離1900年的天數 String fraction = rawData.substring(10, 10 + 8); // 4字節表示剩餘的部分 faction int millis = (int)(Integer.parseInt(fraction, 16) * 3.33333); // faction*3.3333 以後表示的是毫秒數 int seconds= millis / 1000; // 獲得秒數 Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(cal.getTimeInMillis()); // 1900-01-01 00:00:00 calendar.add(Calendar.DATE, Integer.parseInt(day, 16)); // 加上天數 calendar.add(Calendar.SECOND, seconds); // 加上秒數 // calendar.add(Calendar.MILLISECOND, millis); // 採用 Calendar.MILLISECOND 計算會致使在秒級別的出現偏差 SimpleDateFormat sdf = null; if(millisecondFlag) sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // 毫秒部分是不精確的,毫秒部分每次運行的結果不相同! else sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 不帶毫秒部分,秒級別是精確的,每次運行的結果相同 return sdf.format(calendar.getTime()); } public static void main(String[] args) { //sql server: select CAST(0x00009E0E0095524F AS DateTime) == 2010-10-13 09:03:39.783 正確的日期值 String str = "CAST(0x00009E0E0095524F AS DateTime)"; System.out.println(convertRawToStr(str, true)); // 2010-10-13 09:03:39.374 毫秒部分不精確 System.out.println(convertRawDateTimeToStr(str)); // 2010-10-13 09:03:39 秒級別是精確的 try { // 這裏的字符集通常是 StandardCharsets.UTF_16 或者 StandardCharsets.UTF_8,具體看你導出時採用的是那種字符集 BufferedReader reader = Files.newBufferedReader(Paths.get("F:\\Members.sql"), StandardCharsets.UTF_16); BufferedWriter writer = Files.newBufferedWriter(Paths.get("F:\\Members_mysql.sql"), StandardCharsets.UTF_8); String line = null; Matcher matcher = null; String matcherStr = null; String reg = ".*((?i)CAST\\(0x[0-9-a-f-A-F]+ AS DateTime\\)).*"; // ( 爲特殊字符,表示普通的( 須要用 \\( 來轉義表示 Pattern pattern = Pattern.compile(reg); while ((line = reader.readLine()) != null) { while (line.matches(reg)) { matcher = pattern.matcher(line); matcherStr = null; if (matcher.find()) { matcherStr = matcher.group(1); // matcherStr = "CAST(0x00009E0E0095524F AS DateTime)" if (matcherStr != null) { String mysqlStr = convertRawDateTimeToStr(matcherStr); if(mysqlStr != null) line = line.replace(matcherStr, " '"+mysqlStr+"' "); // mysqlStr = '2010-10-13 09:03:39' } } else { break; // break inner while loop } } writer.write(line); writer.newLine(); } } catch (Exception e) { e.printStackTrace(); } System.out.println("done."); } }
正則表達式 String reg = ".*((?i)CAST\\(0x[0-9-a-f-A-F]+ AS DateTime\\)).*"; 其中的 (?i) 表示不區分字符的大小寫,相似於js中的 /xx/i數據庫
在 F:\\Members.sql 中保存的是sql server導出的insert語句形式的數據:oracle
INSERT [Members] ([g_MemberID], [FK_GroupId], [FK_UserId], [FK_C_UserId], [g_Member_State], [g_Member_Jobtitle], [g_Member_Open], [g_Member_Nickname], [g_Member_FirstnamePY], [g_Member_Firstname], [g_Member_LastnamePY], [g_Member_Lastname], [g_Member_sex], [g_Member_Birthday], [g_Member_MaritalStatus], [FK_City], [g_Member_FacePath], [g_Member_Address], [g_Member_Mobile], [g_Member_hometel], [g_Member_Email], [g_Member_QQ], [g_Member_Wangwang], [g_Member_Msn], [g_Member_Note], [g_Member_JoinTime], [FK_User_AttnUserID], [g_push], [g_Member_Order], [gmt_last_modified]) VALUES (1, 1, 4, 4, 40, NULL, 30, N'大手機', N'', N'', N'總', N'總', N'M', N'2013-11-31', NULL, 11, N'20131231/2013123115MF0DD3.jpg', N'', N'13900000000', N'', N'', N'', N'', N'', N'', NULL, NULL, NULL, NULL, CAST(0x0000A2A500FC2E4F AS DateTime)) INSERT [Members] ([g_MemberID], [FK_GroupId], [FK_UserId], [FK_C_UserId], [g_Member_State], [g_Member_Jobtitle], [g_Member_Open], [g_Member_Nickname], [g_Member_FirstnamePY], [g_Member_Firstname], [g_Member_LastnamePY], [g_Member_Lastname], [g_Member_sex], [g_Member_Birthday], [g_Member_MaritalStatus], [FK_City], [g_Member_FacePath], [g_Member_Address], [g_Member_Mobile], [g_Member_hometel], [g_Member_Email], [g_Member_QQ], [g_Member_Wangwang], [g_Member_Msn], [g_Member_Note], [g_Member_JoinTime], [FK_User_AttnUserID], [g_push], [g_Member_Order], [gmt_last_modified]) VALUES (2, 2, 4, 4, 40, N'錦鯉', 30, N'', N'', N'測試', N'總', N'陳', N'M', N'2013-11-31', 0, 11, N'20140324/2014032409Xva2Ix.jpg', N'', N'13900000000', N'', N'', N'', N'', N'', N'', NULL, NULL, NULL, NULL, CAST(0x0000A2F80095DF52 AS DateTime))
轉換以後的結果保存在 F:\\Members_mysql.sql 中,相似於:工具
INSERT [Members] ([g_MemberID], [FK_GroupId], [FK_UserId], [FK_C_UserId], [g_Member_State], [g_Member_Jobtitle], [g_Member_Open], [g_Member_Nickname], [g_Member_FirstnamePY], [g_Member_Firstname], [g_Member_LastnamePY], [g_Member_Lastname], [g_Member_sex], [g_Member_Birthday], [g_Member_MaritalStatus], [FK_City], [g_Member_FacePath], [g_Member_Address], [g_Member_Mobile], [g_Member_hometel], [g_Member_Email], [g_Member_QQ], [g_Member_Wangwang], [g_Member_Msn], [g_Member_Note], [g_Member_JoinTime], [FK_User_AttnUserID], [g_push], [g_Member_Order], [gmt_last_modified]) VALUES (1, 1, 4, 4, 40, NULL, 30, N'大手機', N'', N'', N'總', N'總', N'M', N'2013-11-31', NULL, 11, N'20131231/2013123115MF0DD3.jpg', N'', N'13900000000', N'', N'', N'', N'', N'', N'', NULL, NULL, NULL, NULL, '2013-12-31 15:18:09' ) INSERT [Members] ([g_MemberID], [FK_GroupId], [FK_UserId], [FK_C_UserId], [g_Member_State], [g_Member_Jobtitle], [g_Member_Open], [g_Member_Nickname], [g_Member_FirstnamePY], [g_Member_Firstname], [g_Member_LastnamePY], [g_Member_Lastname], [g_Member_sex], [g_Member_Birthday], [g_Member_MaritalStatus], [FK_City], [g_Member_FacePath], [g_Member_Address], [g_Member_Mobile], [g_Member_hometel], [g_Member_Email], [g_Member_QQ], [g_Member_Wangwang], [g_Member_Msn], [g_Member_Note], [g_Member_JoinTime], [FK_User_AttnUserID], [g_push], [g_Member_Order], [gmt_last_modified]) VALUES (2, 2, 4, 4, 40, N'錦鯉', 30, N'', N'', N'測試', N'總', N'陳', N'M', N'2013-11-31', 0, 11, N'20140324/2014032409Xva2Ix.jpg', N'', N'13900000000', N'', N'', N'', N'', N'', N'', NULL, NULL, NULL, NULL, '2014-03-24 09:05:40.358' )
能夠看到oop
CAST(0x0000A2A500FC2E4F AS DateTime) 被轉換成了:'2013-12-31 15:18:09' 測試
CAST(0x0000A2F80095DF52 AS DateTime) 被轉換成了:'2014-03-24 09:05:40'編碼
固然要達到直接運行插入mysql數據庫中,還要去掉 [g_MemberID] 中的 [] 中括號,還有每一行的末尾要加上逗號 , 再者 INSERT [Members] ([g_MemberID], [FK_GroupId], ...) 語句只須要出現一次,採用 insert into tab(col_a,col_b,col_c) values (1,2,3), (4,5,6) ... 這樣的插入形式,能夠明顯提升插入速度。
另外,若是sql server導出時,若是選擇導出到excel時,datetime字段的值是不須要進行轉換的。在數據量很小時,能夠選擇這種方式。
2. 處理 sql server 導出的 decimal 類型字段 和 insert 語句
sql server在導出 decimal 類型的字段時,導出的結果是相似於:CAST(5.00 AS Decimal(6, 2)) 這樣的形式,咱們須要將其轉換成 5.00,還有上面說到的要去掉[g_MemberID] 中的 [] 中括號, 每一行的末尾要加上逗號,還有要轉換成 insert into tab(col_a,col_b,col_c) values (1,2,3), (4,5,6), ....; 形式的結果。下面給出一個相對完整的處理代碼:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * sql server數據庫在向mysql等數據庫遷移數據時,會遇到幾個麻煩 * 1) datetime 類型字段的導出結果須要轉換; * 2) decimal 類型的字段的導出結果須要轉換; * 3) insert 語句須要去掉 [和 ], 去掉多餘的 insert, 最後變成 insert into user(id,name) values(1,'aa'), (2,'bb'); 的形式 * @author digdeep@126.com */ public class SqlServerConverter { private static final int insertSQLcount = 10; private static String reg = ".*((?i)CAST\\(0x[0-9-a-f-A-F]+ AS DateTime\\)).*"; // CAST(0x00009E0E0095524F AS DateTime) private static Pattern pattern = Pattern.compile(reg); private static String sqlInertReg = ".*((?i)INSERT .* \\(.*\\) VALUES )\\(.*\\).*"; //insert member (id,name) values (xxx) private static Pattern sqlInertPattern = Pattern.compile(sqlInertReg); private static String decimalReg = ".*((?i)CAST\\(.* AS Decimal\\(.*\\)\\)).*";; //CAST(5.00 AS Decimal(6, 2)) private static Pattern decimalPattern = Pattern.compile(decimalReg); /** * 將每一條insert語句中的 "CAST(0x00009E0E0095524F AS DateTime)" 轉換成對應的:'2010-10-13 09:03:39' * @param line * @return */ public static String dealWithDateTime(String line){ Matcher matcher = null; String matcherStr = null; while (line.matches(reg)) { matcher = pattern.matcher(line); matcherStr = null; if (matcher.find()) { matcherStr = matcher.group(1); // matcherStr = "CAST(0x00009E0E0095524F AS DateTime)" if (matcherStr != null) { String mysqlStr = SqlServerDateTimeUtils.convertRawDateTimeToStr(matcherStr); if(mysqlStr != null) line = line.replace(matcherStr, " '"+mysqlStr+"' "); // mysqlStr = '2010-10-13 09:03:39' } } else { break; // break inner while loop } } return line; } /** * 將每一條insert語句中的 "CAST(5.00 AS Decimal(6, 2))" 轉換成對應的:5.00 * @param line * @return */ public static String dealWithDecimal(String line){ Matcher matcher = null; String matcherStr = null; while (line.matches(decimalReg)) { matcher = decimalPattern.matcher(line); matcherStr = null; if (matcher.find()) { matcherStr = matcher.group(1); // matcherStr = CAST(5.00 AS Decimal(6, 2)) if (matcherStr != null) { String result = matcherStr.substring("CAST(".length(), matcherStr.toUpperCase().indexOf(" AS DECIMAL(")); line = line.replace(matcherStr, result); } } else { break; // break inner while loop } } return line; } /** * @param sqlServerPath 輸入文件 * @param sqlCharset 輸入文件的編碼 * @param mysqlPath 輸出文件,以utf-8格式輸出 * @return */ public static boolean converterSqlServerToMySQL(String sqlServerFile, Charset sqlServerCharset, String mysqlFile){ Path inPath = Paths.get(sqlServerFile); Path outPath = Paths.get(mysqlFile); // writer 採用 StandardCharsets.UTF_8 try(BufferedReader reader = Files.newBufferedReader(inPath, sqlServerCharset); BufferedWriter writer = Files.newBufferedWriter(outPath, StandardCharsets.UTF_8);){ String line = null; Matcher matcher = null; String matcherStr = null; int i = 0; while ((line = reader.readLine()) != null) { // 1. 轉換 datetime: CAST(0x00009E0E0095524F AS DateTime) 轉換成 '2010-10-13 09:03:39' line = dealWithDateTime(line); // 2. 轉換decimal: CAST(5.00 AS Decimal(6, 2)) 轉換成 5.00 line = dealWithDecimal(line); // 3. 去掉 [ 和 ], 去掉多餘的 insert, 最後變成 insert into user(id,name) values(1,'aa'), (2,'bb'); 的形式 matcher = sqlInertPattern.matcher(line); if(matcher.find()){ i++; matcherStr = matcher.group(1); // matcherStr == insert [user]([id], [name]) values (1,'aa') if(i % insertSQLcount == 1){ // 每 insertSQLcount 條 insert 做爲一組進行提交 if(i > 1){ writer.write(";"); // 加入 ; 進行提交 writer.newLine(); } line = line.replace(matcherStr, " "); // line == (1,'aa') matcherStr = matcherStr.replace("[", ""); matcherStr = matcherStr.replace("]", ""); matcherStr = matcherStr.toLowerCase().replace("insert", "insert into "); line = matcherStr + "\n" + line; // line == insert into user(id,name) \n values(1,'aa') }else{ line = line.replace(matcherStr, ","); // line == ,values(2,'aa') } writer.write(line); writer.newLine(); } } if(i % insertSQLcount != 1 && i > 0) // 當末尾沒有封號(;), 時才添加一個封號(;),避免在末尾出現兩個封號(;;) writer.write(";"); writer.newLine(); writer.flush(); // 注意這裏必定要 flush ,否則文檔的最後面的最後一兩條數據不會完整寫到文件中的!!! } catch (Exception e) { e.printStackTrace(); return false; } System.out.println("OK."); return true; } public static void main(String[] args) { // 這裏的字符集通常是 StandardCharsets.UTF_16 或者 StandardCharsets.UTF_8,具體看你導出時採用的是那種字符集 // 若是sql server導出時選擇了 unicode,那麼這裏就應該使用 StandardCharsets.UTF_16 SqlServerConverter.converterSqlServerToMySQL("F:\\Members.sql", StandardCharsets.UTF_16, "F:\\Members_mysql.sql"); System.out.println("-------------------------------------------------"); SqlServerConverter.converterSqlServerToMySQL("F:\\model_choise.sql", StandardCharsets.UTF_16, "F:\\model_choise_mysql.sql"); System.out.println("done."); } }
轉換以前 F:\\model_choise.sql 的文件內容以下所示:
SET IDENTITY_INSERT [model_choise] ON INSERT [model_choise] ([id], [gid], [member_id], [user_id], [model_id], [status], [choise_time], [item_id], [pay_amount], [accuracy], [g_name], [g_mobile], [reation_id]) VALUES (992, NULL, NULL, 736, 481, 1, CAST(0x0000A49500C0DE5F AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL) INSERT [model_choise] ([id], [gid], [member_id], [user_id], [model_id], [status], [choise_time], [item_id], [pay_amount], [accuracy], [g_name], [g_mobile], [reation_id]) VALUES (993, NULL, NULL, 737, 559, 1, CAST(0x0000A49501420847 AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL) INSERT [model_choise] ([id], [gid], [member_id], [user_id], [model_id], [status], [choise_time], [item_id], [pay_amount], [accuracy], [g_name], [g_mobile], [reation_id]) VALUES (1009, NULL, NULL, 9, 528, 1, CAST(0x0000A496013F1119 AS DateTime), NULL, NULL, CAST(5.00 AS Decimal(6, 2)), NULL, NULL, NULL) INSERT [model_choise] ([id], [gid], [member_id], [user_id], [model_id], [status], [choise_time], [item_id], [pay_amount], [accuracy], [g_name], [g_mobile], [reation_id]) VALUES (1010, NULL, NULL, 4, 528, 1, CAST(0x0000A496013F9F76 AS DateTime), NULL, NULL, CAST(5.00 AS Decimal(6, 2)), NULL, NULL, NULL) // ... ...
轉換以後 F:\\model_choise_mysql.sql 內容以下所示:
insert into model_choise (id, gid, member_id, user_id, model_id, status, choise_time, item_id, pay_amount, accuracy, g_name, g_mobile, reation_id) values (992, NULL, NULL, 736, 481, 1, '2015-05-11 11:42:12' , NULL, NULL, NULL, NULL, NULL, NULL) ,(993, NULL, NULL, 737, 559, 1, '2015-05-11 19:32:28' , NULL, NULL, NULL, NULL, NULL, NULL) ,(1009, NULL, NULL, 9, 528, 1, '2015-05-12 19:21:41' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1010, NULL, NULL, 4, 528, 1, '2015-05-12 19:23:42' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1011, NULL, NULL, 4, 495, 1, '2015-05-12 19:24:38' , NULL, NULL, NULL, NULL, NULL, NULL) ,(1012, NULL, NULL, 4, 554, 1, '2015-05-12 19:25:23' , NULL, NULL, NULL, NULL, NULL, NULL) ,(1013, NULL, NULL, 566, 528, -1, '2015-05-12 20:33:24' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1014, NULL, NULL, 8, 495, -1, '2015-05-12 21:03:45' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1015, NULL, NULL, 8, 498, -1, '2015-05-12 21:04:15' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1016, NULL, NULL, 8, 558, -1, '2015-05-12 21:04:39' , NULL, NULL, 5.00, NULL, NULL, NULL) ; insert into model_choise (id, gid, member_id, user_id, model_id, status, choise_time, item_id, pay_amount, accuracy, g_name, g_mobile, reation_id) values (1017, NULL, NULL, 8, 417, -1, '2015-05-12 21:05:29' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1018, NULL, NULL, 8, 494, -1, '2015-05-12 21:29:20' , NULL, NULL, 4.00, NULL, NULL, NULL) ,(1019, NULL, NULL, 8, 528, -1, '2015-05-12 21:31:41' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1026, NULL, NULL, 566, 493, 1, '2015-05-13 02:14:09' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1027, NULL, NULL, 566, 532, 1, '2015-05-13 02:14:17' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1028, NULL, NULL, 566, 459, 1, '2015-05-13 09:16:44' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1029, NULL, NULL, 566, 421, -1, '2015-05-13 10:03:52' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1032, NULL, NULL, 9, 495, 1, '2015-05-13 13:27:32' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1033, NULL, NULL, 9, 554, 1, '2015-05-13 13:28:21' , NULL, NULL, 5.00, NULL, NULL, NULL) ,(1034, NULL, NULL, 9, 560, -1, '2015-05-13 17:10:21' , NULL, NULL, NULL, NULL, NULL, NULL) ;
// ... ...
能夠看到 CAST(0x0000A496013F1119 AS DateTime), 和 CAST(5.00 AS Decimal(6, 2)) 都被正確的轉換了。insert 語句的轉換也是徹底正確的。因此轉換以後的文件 F:\\model_choise_mysql.sql 是能夠以下使用 source 命令來直接插入mysql數據庫中:
mysql> source F:\\model_choise_mysql.sql Query OK, 10 rows affected (0.03 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.05 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.04 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.04 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.05 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.02 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.02 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 10 rows affected (0.02 sec) Records: 10 Duplicates: 0 Warnings: 0 Query OK, 8 rows affected (0.03 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> select count(*) from model_choise; +----------+ | count(*) | +----------+ | 88 | +----------+ 1 row in set (0.00 sec)
實際使用時,private static final int insertSQLcount = 10; insertSQLcount 應該調大一點,幾百,上千都是能夠的。
另外上面使用了 Java 1.7 中新引入的 try-with-recourse 的自動關閉資源的使用方法:
try(BufferedReader reader = Files.newBufferedReader(inPath, StandardCharsets.UTF_16); BufferedWriter writer = Files.newBufferedWriter(outPath, StandardCharsets.UTF_8);)