原文: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 ...
随机推荐
- DBMS_STATS.GATHER_TABLE_STATS
由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要! 作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直方图信 ...
- php文件操作基本使用方法
<?php /* $fp=fopen("tmp.html","r"); $str=fread($fp,filesize("tmp.html&qu ...
- Visual C++学习笔记1:一定要注意ANSI和UNICODE差额
最近的研究VC++.下载VS2013,根据<Visual C++开发实战系列>首先hello我写了一个常规样品,结果显示乱码编辑框.夜已经折腾型转变.然后总结很明显ANSI和UNICODE ...
- Left Menu抽屉效果ScrollView姿态共存冲突
我们有一个小项目,需要做主页ScrollView嵌套TableView. 再就是Left与Right抽屉效果. 课前有眼似的,然后完成忘. 非常糟糕的记忆.真应了那句话:好记性不如烂博客. 由于我首页 ...
- 开源Math.NET基础数学类库使用(16)C#计算矩阵秩
原文:[原创]开源Math.NET基础数学类库使用(16)C#计算矩阵秩 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...
- dbus 和 policykit 实例篇(python) ()转
使用policykit 的程序一般都有一个dbus daemon程序来完成相关操作,这个dbus daemon 会在系统注册一个system bus 服务名,用于响应要求root privileged ...
- Spring Boot 基础
Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...
- SQL 修改排序规则的问题 sql_latin1_general_cp1_ci_as
在一个项目中遇到:用原来的数据库生成的脚本,然后部署到新的服务器上,数据库的SQL_Latin1_General_CP1_CI_AS 怎么查询出来汉字都是乱码了. 遂查解决方法. 需要执行这个 ALT ...
- 微软研究院的分布式云计算框架orleans
orleans Orleans 客户端请求的消息流转以及消息在Silo中再路由机制 Witte 2015-04-29 21:58 阅读:196 评论:0 一种基于Orleans的分布式Id ...
- 在SQL2008中,如何让id自动生成并自动递增?如何让时间默认生成?
id自动递增: 如果是用语句操作,这样定义:ID INT IDENTITY,如果是要生成一对数字,这样定义:ID INT IDENTITY(1,1) 如果要在SQL Server的表中设置 ...