sqlite 建库建表等

 

使用 sqlite 建库建表操作

1. 使用可视化的工具建立. 我常用的是 SQLite Expert Personal ...

http://www.sqliteexpert.com/ 可下载

 

2. 通过代码建立

import java.sql.*;

public class Test {
  public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection conn =
      DriverManager.getConnection("jdbc:sqlite:test.db");
    Statement stat = conn.createStatement();
    stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    PreparedStatement prep = conn.prepareStatement(
      "insert into people values (?, ?);");

    prep.setString(1, "Gandhi");
    prep.setString(2, "politics");
    prep.addBatch();
    prep.setString(1, "Turing");
    prep.setString(2, "computers");
    prep.addBatch();
    prep.setString(1, "Wittgenstein");
    prep.setString(2, "smartypants");
    prep.addBatch();

    conn.setAutoCommit(false);
    prep.executeBatch();
    conn.setAutoCommit(true);

    ResultSet rs = stat.executeQuery("select * from people;");
    while (rs.next()) {
      System.out.println("name = " + rs.getString("name"));
      System.out.println("job = " + rs.getString("occupation"));
    }
    rs.close();
    conn.close();
  }
}

 

注意:

1. Connection conn =  DriverManager.getConnection("jdbc:sqlite:test.db");
如果指定名称所代表的数据库不存在则自动创建.如果存在则直接使用.

 

2. stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
通过代码来创建删除表. 没啥好说的了.

 

其他的也没什么太多要注意的了.

 

 

 

 

你可能感兴趣的:(sql,sqlite,jdbc)