从一个 sql 文件,我正在尝试读取 sql 查询并使用以下 java 代码创建一个 mysqlite db:
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
public class ReadMySqlDump {
static String sep = System.getProperty("line.separator");
public ReadMySqlDump(){
try{
String s = "";
String buff="";
ArrayList<String> creates = new ArrayList<String> ();
ArrayList<String> inserts = new ArrayList<String> ();
boolean createStart = false;
String currentCreate = "";
BufferedReader br = new BufferedReader(new FileReader("mydb.sql.sql"));
while((buff=br.readLine()) != null){
buff = buff.trim();
if(buff.length() == 0)continue;
if(buff.startsWith("/*"))continue;
if(buff.startsWith("--"))continue;
if(createStart && buff.startsWith(");")){
// System.out.println("before: " + currentCreate);
currentCreate = currentCreate.trim();
if(currentCreate.endsWith(","))currentCreate = currentCreate.substring(0, currentCreate.length()-1);
//currentCreate = currentCreate.substring(0, currentCreate.length()-2);
// System.out.println("after: " + currentCreate);
currentCreate += " " + buff + sep;
createStart = false;
creates.add(currentCreate);
currentCreate = "";
continue;
}
if(createStart){
currentCreate += " " + buff +sep;
continue;
}
if(!createStart && buff.startsWith("CREATE")){
createStart = true;
currentCreate += buff + sep;
continue;
}
if(buff.startsWith("INSERT")) {
inserts.add(buff);
continue;
}
}
br.close();
for(String ss: creates){
System.out.println(ss);
}
System.out.println("");
System.out.println("");
for(String ss: inserts){
System.out.println(ss);
}
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:testNew5.db");
Statement stat = conn.createStatement();
for(String ss: creates){
System.out.println("SQL command: " + ss);
stat.executeUpdate(ss);
}
// ResultSet rs = stat.executeQuery("select * from tbl_passion_attributes");
// while(rs.next()){
// }
for (String ss : inserts){
System.out.println("ss: " + ss);
int n = stat.executeUpdate(ss);
System.out.println("n: " + n);
}
conn.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
new ReadMySqlDump();
}
}
create 语句完美运行,并创建了表;但是当插入语句开始时,第一个 INSERT 语句本身出现异常。以下是例外情况:
ss: INSERT INTO acos (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
java.sql.SQLException: near "VALUES": syntax error
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
at ReadMySqlDump.<init>(ReadMySqlDump.java:94)
at ReadMySqlDump.main(ReadMySqlDump.java:113)
以下是 mydb.sql.sql 的内容
begin;
-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 30, 2013 at 11:55 AM
-- Server version: 5.5.24-log
-- PHP Version: 5.3.13
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: abc
--
-- --------------------------------------------------------
--
-- Table structure for table acos
--
CREATE TABLE IF NOT EXISTS acos (
created datetime NOT NULL,
modified datetime NOT NULL,
id integer NOT NULL ,
parent_id integer DEFAULT NULL,
model varchar(255) DEFAULT NULL,
foreign_key integer DEFAULT NULL,
alias varchar(255) DEFAULT NULL,
lft integer DEFAULT NULL,
rght integer DEFAULT NULL,
);
--
-- Dumping data for table acos
--
INSERT INTO acos (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, NULL, NULL, NULL, 'controllers', 1, 1124),
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 2, 1, NULL, NULL, 'Pages', 2, 15);
-- --------------------------------------------------------
--
-- Table structure for table aros
--
CREATE TABLE IF NOT EXISTS aros (
created datetime NOT NULL,
modified datetime NOT NULL,
id integer NOT NULL ,
parent_id integer DEFAULT NULL,
model varchar(255) DEFAULT NULL,
foreign_key integer DEFAULT NULL,
alias varchar(255) DEFAULT NULL,
lft integer DEFAULT NULL,
rght integer DEFAULT NULL
);
--
-- Dumping data for table aros
--
INSERT INTO aros (created, modified, id, parent_id, model, foreign_key, alias, lft, rght) VALUES
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 1, NULL, 'Group', 1, NULL, 1, 12),
('0000-00-00 00:00:00', '0000-00-00 00:00:00', 2, NULL, 'Group', 2, NULL, 13, 18);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
commit;
我从这里使用 jdbc jar
如果有人可以帮助解决这个问题,将不胜感激。