原文:C#和Java中执行SQL文件脚本的代码(非常有用)
我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析
去除其中的注释,还有把每一句sql语句取出来,然后再利用各个平台中的数据库相关执行它。
接下来放代码!
java版本的
004 |
import java.util.ArrayList; |
005 |
import java.util.Enumeration; |
006 |
import java.util.List; |
007 |
import java.util.Vector; |
014 |
public class SqlHelper { |
019 |
public static void main(String[] args) { |
021 |
String path=new String("d:\\zzadmin.sql"); |
022 |
String sql=GetText(path); |
023 |
String[] arr=getsql(sql); |
024 |
for(int i=0;i<arr.length;i++) |
025 |
System.out.println("第"+i+"句:"+arr[i]); |
028 |
public static String GetText(String path){ |
029 |
File file=new File(path); |
030 |
if(!file.exists()||file.isDirectory()) |
032 |
StringBuffer sb=new StringBuffer(); |
035 |
FileInputStream fis = new FileInputStream(path); |
036 |
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); |
037 |
BufferedReader br = new BufferedReader(isr); |
041 |
sb.append(temp+"\r\n"); |
044 |
} catch (Exception e) { |
047 |
return sb.toString(); |
055 |
public static String[] getsql(String sql) |
058 |
s=s.replace("\r\n","\r"); |
059 |
s=s.replace("\r", "\n"); |
060 |
String[] ret=new String[1000]; |
061 |
String[] sqlarray=s.split(";\n"); |
062 |
sqlarray=filter(sqlarray); |
064 |
for (String item : sqlarray) |
066 |
String ret_item = ""; |
067 |
String[] querys = item.trim().split("\n"); |
068 |
querys = filter(querys);//去空 |
069 |
for (String query : querys) |
071 |
String str1 = query.substring(0, 1); |
072 |
String str2 = query.substring(0, 2); |
073 |
if (str1.equals("#") || str2.equals("--") || str2.equals("/*") || str2.equals("//"))//去除注释的关键步奏 |
087 |
/// <param name="ss">数组</param> |
088 |
/// <returns></returns> |
089 |
public static String[] filter(String[] ss) |
091 |
List<String> strs = new ArrayList<String>(); |
092 |
for (String s : ss) { |
093 |
if (s != null && !s.equals("")) |
097 |
String[] result=new String[strs.size()]; |
098 |
for(int i=0;i<strs.size();i++) |
100 |
result[i]=strs.get(i).toString(); |
106 |
public void deletezs(String fileStr) |
109 |
Vector<String> vec=new Vector<String>(); |
110 |
String str="",tm="",mm=""; |
111 |
BufferedReader br = new BufferedReader( new FileReader(fileStr)); |
113 |
while( null != (str = br.readLine() ) ) |
115 |
if ((str.indexOf("/*")>=0)&&((bol==false))) |
117 |
if (str.indexOf("*/")>0) |
120 |
vec.addElement(str.substring(0,str.indexOf("/*"))+str.substring(str.indexOf("*/")+2,str.length())); |
125 |
mm=str.substring(0,str.indexOf("/*")); |
126 |
if (!(mm.trim().equals(""))) |
132 |
if (str.indexOf("*/")>=0) |
135 |
mm=str.substring(str.indexOf("*/")+2,str.length()); |
136 |
if (!mm.trim().equals("")) |
140 |
else if (str.indexOf("//")>=0) |
142 |
tm=str.substring(0,str.indexOf("//")); |
143 |
if (!tm.trim().equals("")) |
152 |
File fName=new File(fileStr); |
153 |
FileWriter in=new FileWriter(fName); |
155 |
Enumeration<String> ew=vec.elements(); |
157 |
while (ew.hasMoreElements()) { |
158 |
ssss= ew.nextElement().toString(); |
165 |
}catch(Exception ee){ |
166 |
ee.printStackTrace(); |
调用GetText就可以返回一个装满了sql语句的数组,循环执行其中的sql语句吧
c#版本的
001 |
//-------------------------第一种------------------------------------- |
003 |
/// 获取sql文件中的sql语句数组 第一种方法 |
005 |
/// <param name="sql"></param> |
006 |
/// <returns></returns> |
007 |
public static string[] sql_split(string sql) |
010 |
Regex reg = newRegex("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/"); |
011 |
reg.Replace(sql, "ENGINE=\\1 DEFAULT CHARSET=utf8"); |
012 |
s = s.Replace('\r', '\n'); |
013 |
string[] ret = new string[10000]; |
014 |
string[] sqlarray = StringSplit(s, ";\n"); |
016 |
foreach (string item in sqlarray) |
019 |
string[] queries = item.Split('\n'); |
020 |
queries = filter(queries); |
021 |
foreach (string query in queries) |
023 |
string str1 = query.Substring(0, 1); |
024 |
string str2 = query.Substring(0, 2); |
025 |
if (str1 != "#" && str2 != "--" && str2 != "/*"&& str2 != "//")//去除注释的关键步奏 |
039 |
/// <param name="ss"></param> |
040 |
/// <returns></returns> |
041 |
public static string[] filter(string[] ss) |
043 |
List<string> strs = new List<string>(); |
044 |
foreach (string s in ss) |
046 |
if (!string.IsNullOrEmpty(s)) strs.Add(s); |
048 |
string[] result = strs.ToArray(); |
054 |
/// <param name="strSource"></param> |
055 |
/// <param name="strSplit"></param> |
056 |
/// <returns></returns> |
057 |
public static string[] StringSplit(string strSource, string strSplit) |
059 |
string[] strtmp = new string[1]; |
060 |
int index = strSource.IndexOf(strSplit, 0); |
063 |
strtmp[0] = strSource; |
068 |
strtmp[0] = strSource.Substring(0, index); |
069 |
returnStringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp); |
076 |
/// <param name="strSource"></param> |
077 |
/// <param name="strSplit"></param> |
078 |
/// <param name="attachArray"></param> |
079 |
/// <returns></returns> |
080 |
private static string[] StringSplit(string strSource, stringstrSplit, string[] attachArray) |
082 |
string[] strtmp = new string[attachArray.Length + 1]; |
083 |
attachArray.CopyTo(strtmp, 0); |
085 |
int index = strSource.IndexOf(strSplit, 0); |
088 |
strtmp[attachArray.Length] = strSource; |
093 |
strtmp[attachArray.Length] = strSource.Substring(0, index); |
094 |
returnStringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp); |
098 |
//----------------------------------------------------- |
100 |
//-----------------------第二种------------------------------ |
102 |
/// 获取sql文件中的sql语句数组 第二种 |
104 |
/// <param name="sql"></param> |
105 |
/// <returns></returns> |
106 |
public string[] getsqls(string sql) |
109 |
s = s.Replace("\r\n", "\n"); |
110 |
s = s.Replace("\r","\n").Trim(); |
111 |
string[] ret = new string[1000]; |
113 |
string[] sqlarray= StringSplit(s, ";\n"); |
114 |
sqlarray = filter(sqlarray);//去空 |
117 |
foreach (string item in sqlarray) |
119 |
string ret_item = ""; |
120 |
string[] querys = item.Trim().Split('\n'); |
121 |
querys = filter(querys);//去空 |
123 |
foreach (string query in querys) |
125 |
string str1 = query.Substring(0, 1); |
126 |
string str2 = query.Substring(0, 2); |
127 |
if (str1 == "#" || str2 == "--" || str2 == "/*"|| str2 == "//")//去除注释的关键步奏 |
c#两个方法对sql文件解析都是一样的
- 使用java以及jdbc不使用第三方库执行sql文件脚本
使用java以及jdbc不使用第三方库执行sql文件脚本 2017年02月15日 15:51:45 阅读数:660 使用java执行sql脚本的方法 解析sql脚本,删除不必要的注释和空行 将语句按分 ...
- qt中执行 sql文件的方法
由于qt中没有原生的执行sql文件的方法.因此我们需要根据sql文件中的流的特点,将其分解成一个个语句单独执行. 1.首先通过Qfile读取sql文件 2.将sql文件中的内容通过“:”进行拆解 3. ...
- PHP执行.SQL文件的实例代码分享
介绍下使用PHP执行.SQL文件的代码一例,分享下. demo.php: <?php ) )) ) ENGINE) unsigned ) unsigned )) ) ENGINE) unsign ...
- 去掉utf-8的Bom头:使用java以及jdbc不使用第三方库执行sql文件脚本
package com.xxx.xxx.dao; import java.io.BufferedReader; import java.io.File; import java.io.FileInpu ...
- 运行执行sql文件脚本的例子
sqlcmd -s -d db_test -r -i G:\test.sql 黑色字体为关键命令,其他颜色(从左至右):服务器名称,用户名,密码,数据库,文件路径 通过select @@servern ...
- 在 PL/SQL Developer 中执行SQL文件的方法
打开 command Window SQL> @'D:\My Documents\Downloads\bde_chk_cbo.sql'; 整个路径及文件两边要有单引号哦!
- Java中执行.exe文件
public static void main(String args[]){ try { String command ="notepad"; // 笔记本 Process ch ...
- 在eclipse中执行sql的编码问题
症状-分析: 刚才在eclipse中执行sql文件,发现数据进入数据库的时候总是乱码 后来查看MySQL的编码设置,全是UTF8,没问题,sql文件本身也是UTF8的编码 并且,使用MySQL的CMD ...
- 如何在linux中运行sql文件
1.在linux中进入sql命令行 mysql -u root -p 输入密码 2.假设home下面有a.sql文件 先得use databasename,要不会报错 “No Database S ...
随机推荐
- [置顶] android系统如何在静音模式下关闭camera拍照声音(2)
之前写过一篇“android系统如何在静音模式下关闭camera拍照声音”的博客,今天来写他的续篇,继续探讨这个问题. 公司新需求,要求在camera应用中添加一个开关,可以进行拍照声音的关闭和开启. ...
- JAVA: httpclient 具体解释——第五章;
httpclient 具体解释--第一章: httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...
- Apache Curator获得真正的
Apache Curator获得真正的 Curator它是Netflix一家公司来源Zookeeper顾客,与Zookeeper相比于提供本地客户端,Curator的抽象层次更高,简化了Zookeep ...
- rsync 只是测试,请看下一篇
实现从客户服务器去同步资源服务器 1.解压 # tar -xzpvf rsync-2.5.6.tar.gz 编译安装 # cd rsync-2.5.6/ # ./configure --pref ...
- kernel 于ioctl申请书
ioctl经营无纸装置频繁使用的类型.同时这是一个非常实用的方法进程调试. 这里正在进行wdt该文章继续 static long at91_wdt_ioctl(struct file *file, u ...
- PDE_DATA 定义
PDE_DATA 定义 Location: /fs/proc/internal.h static inline struct proc_dir_entry *PDE(const struct inod ...
- CAS实现SSO单点登录原理(转)
1. CAS 简介 1.1. What is CAS ? CAS ( Central Authentication Service ) 是 Yale 大学发起的一个企业级的.开源的项目,旨 ...
- fiddler打开后 浏览器就上不了网的解决方法
fiddler设置一下即可 Tools fiddler options connections 不要选中 Act as system proxy on startup
- 矢量编程——随着MNIST案例
矢量编程使用的所有明确的矢量运算,而不是for周期. 上一节所用的是512*512*10的数据集非常小.我们取的patch非常小(8*8),学来的特征非常少(25).而我又凝视掉了梯度校验(偷懒),所 ...
- 排序算法门外汉理解-Shell排序
#include <stdio.h> /* 希尔排序 基本思想:希尔排序又称为缩小增量排序,对简单插入排序的优化. (外部分组gap,组内部插入排序! ! ) 特点:一种不稳定的排序 */ ...