SQLite 通过.sql文件导入导出数据

原链接

(1)创建数据库文件

无需手动创建db文件,直接在命令行执行:

~/test_sqlite$ sqlite3 test.db

进入SQLIte命令行,直接使用标准SQL建表:

sqlite> create table test_table (id integer primary key, name text, description text);
sqlite> .exit

看到对应目录下,db文件“test.db”已经被自动创建。再打开,检查一下刚刚创建的表:

~/test_sqlite$ sqlite3 test.db
SQLite version 3.9.2 2015-11-02 18:31:45
Enter “.help” for usage hints.
sqlite> .tables
test_table

当然,还可以用schema命令查看一下更详细的信息:

sqlite> .schema test_table
CREATE TABLE test_table (id integer primary key, name text, description text);

向test_table中插入一些数据:

sqlite> insert into test_table (id, name, description) values (0, ‘name0’, ‘des0’);
sqlite> insert into test_table (id, name, description) values (1, ‘name1’, ‘des1’);
sqlite> insert into test_table (id, name, description) values (2, ‘name2’, ‘des2’);
sqlite>
sqlite>
sqlite> select * from test_table;
0|name0|des0
1|name1|des1
2|name2|des2

(2)利用dump命令导出数据库表到文件

首先,重定向sqlite输出到文件。同样不需要手动创建文件,直接给出文件名即可:

sqlite> .output test_table.sql

利用dump命令,将数据导出到文件。如果dump不带参数,则导出整个数据库:

sqlite> .dump test_table

重定向输出回标准命令行输出:

sqlite> .output stdout

查看test_table.sql文件内容:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE test_table (id integer primary key, name text, description text);
INSERT INTO “test_table” VALUES(0,‘name0’,‘des0’);
INSERT INTO “test_table” VALUES(1,‘name1’,‘des1’);
INSERT INTO “test_table” VALUES(2,‘name2’,‘des2’);
COMMIT;

dump命令将数据库对象导出成SQL格式:数据库定义语言(DDL)和数据操作语言(DML)命令,这些信息可以帮助我们重建数据库对象和其中的数据,可以说是完整地重现整个表。

从test_table.sql文件内容可以看到,其中包含了DDL——create table,DML——insert values,并且在一个数据库事务中完成。

(3)利用read命令导入数据

利用(2)中导出的数据库dump信息,批量执行其中的SQL:

sqlite> BEGIN TRANSACTION;
CREATE TABLE test_table (id integer primary key, name text, description text);
INSERT INTO “test_table” VALUES(0,‘name0’,‘des0’);
INSERT INTO “test_table” VALUES(1,‘name1’,‘des1’);
INSERT INTO “test_table” VALUES(2,‘name2’,‘des2’);sqlite>
COMMIT;

查看表情况:

sqlite> .tables
test_table
sqlite> select * from test_table;
0|name0|des0
1|name1|des1
2|name2|des2

看到表test_table被克隆恢复。

可以用read命令来直接读取(2)中dump导出的文件test_table.sql,完成数据库导入:

sqlite> drop table test_table;
sqlite> .tables
sqlite>
sqlite> .read test_table.sql
sqlite> .tables
test_table
sqlite> select * from test_table;
0|name0|des0
1|name1|des1
2|name2|des2

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Java中导入导出SQLite文件,你可以使用以下步骤: 1. 导入SQLite驱动程序 你需要从SQLite官方网站下载SQLite JDBC驱动程序。下载后,将其添加到你的Java项目中。 2. 连接SQLite数据库 使用以下代码连接到SQLite数据库: ```java Class.forName("org.sqlite.JDBC"); Connection connection = DriverManager.getConnection("jdbc:sqlite:/path/to/database.db"); ``` 这将创建一个连接到指定SQLite数据库的连接对象。 3. 导出SQLite文件导出SQLite文件,你可以使用以下代码: ```java String sql = "SELECT * FROM table_name"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); PrintWriter writer = new PrintWriter(new File("/path/to/output.sql")); while (resultSet.next()) { // 获取数据并写入输出文件 writer.println(resultSet.getString("column_name")); } writer.close(); ``` 这将执行一个查询,将结果写入到指定的输出文件中。 4. 导入SQLite文件导入SQLite文件,你可以使用以下代码: ```java String sql = "INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); Scanner scanner = new Scanner(new File("/path/to/input.sql")); while (scanner.hasNextLine()) { // 获取数据并插入到数据库中 String line = scanner.nextLine(); String[] data = line.split(","); statement.setString(1, data[0]); statement.setString(2, data[1]); statement.setString(3, data[2]); statement.executeUpdate(); } ``` 这将读取指定的输入文件,将数据插入到指定的表格中。 注意:在实际使用中,你需要根据具体情况进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值