原文: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 = new Regex( "/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 |
return StringSplit(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, string strSplit, 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 |
return StringSplit(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 ...
随机推荐
- shell脚本中的数学运算
shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法.以后用到的时候能够来看,呵呵 1.错误方法举例 a) var=1+1 echo $var 输出的结果是1+1 ...
- 玩转web之ajax(一)---使用表单的serialize()方法中文乱码解决
有时候我们需要使用ajax提交去提交form的值,这样就需要使用serialize()去获取form的值,但这样获取的值如果有中文,会乱码,原因和解决方法如下: 原因:.serialize()自动调用 ...
- sensor调试过程HAL层数据不能被转移到app
最近调试e-compass传感器,我遇到了一个奇怪的问题,驱动器正常报告数据.但该数据到HAL后该层已经无法上传app. 经debug,我发现这是一个供应商派的代码存在bug,open的fd没有设置N ...
- mysql float double 类型
1.float类型 float列类型默认长度查不到结果.必须指定精度. 比方 num float, insert into table (num) values (0.12); select * fr ...
- Git使用总结-so easy
一.Git的特性 Speed 速度(git是用c语言写的.一般都是提交到本地) Simple design Strong support for non-linear development (tho ...
- linux文件打开模式
文件打开 int open(const char *pathname, int flags, mode_t mode); 普通方式(Canonical mode) flags中没有设置O_SYN ...
- Directx11学习笔记【十四】 使用最新的Effect框架和SDK
由于之前一直在看directx11龙书学习,因此sdk一直用的Microsoft DirectX SDK (June 2010) 版本,最近在stackoverflow上问dx11相关问题时,一直被大 ...
- 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)
原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 单例模式(Singleton Pattern) 作者:weba ...
- c++头
头文件c/c++独特的概念. 首先解释声明和定义的区别. extern int x;这是一个可变x声明,void fun();这是函数fun()声明.class a;这是类a声明. int x;变量x ...
- Object-C 新手教程
大纲 開始吧 下载这篇教学 设定环境 前言 编译 hello world 创建 Classes @interface @implementation 把它们凑在一起 具体说明... 多重參数 建构子( ...