SqliteStudio工具中无法逐条执行Sql,所以只能用程序执行了。写一个记录一下。
代码:
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlsExcutor {
private static final String DB_PATH = SqlsExcutor.class.getResource("").getPath() + "";
public static void main(String[] args) {
String filePath = SqlsExcutor.class.getResource("").getPath() + "sql.txt";
Connection connection;
Statement statement;
try {
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
} catch (SQLException sQLException) {
sQLException.printStackTrace();
return;
}
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)))) {
String lineString;
while ((lineString = bufferedReader.readLine()) != null) {
if("".equals(lineString)){
continue;
}
statement = connection.createStatement();
statement.execute(lineString);
System.out.println(lineString + " 已被执行");
}
} catch (IOException iOException) {
iOException.printStackTrace();
} catch (SQLException sQLException) {
sQLException.printStackTrace();
}
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hao.tool</groupId>
<artifactId>sqls</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.github.willena</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.18.0</version>
</dependency>
</dependencies>
</project>