【移動GIS】sqlite的使用(一)

先粘貼下百度百科的介紹:  

   SQLite,是一款輕型的數據庫,是遵守ACID[1]的關係型數據庫管理系統,它包含在一個相對小的C庫中,不像常見的客戶-服務器範例,SQLite引擎不是個程序與之通信的獨立進程,而是連接到程序中成爲它的一個主要部分。所以主要的通信協議是在編程語言內的直接API調用。這在消耗總量、延遲時間和整體簡單性上有積極的作用。整個數據庫(定義、表、索引和數據本身)都在宿主主機上存儲在一個單一的文件中。它的簡單的設計是通過在開始一個事務的時候鎖定整個數據文件而完成的。

  

說到底,SQLite這個數據庫特點是:

          1.小。不僅是佔用空間小,而且在使用過程中效率也很高,佔用內存小。

          2.方便。Sqlite數據生成之後是單個文件,此文件包含了數據庫所有的東西,你可用U盤什麼的隨便拷走。

          3.跨平臺。因爲人家是用C寫的,而且是開源的,所有什麼windows linux  macos等操作系統都支持。

          4.支持常用SQL。如果你用過mysql sqlserver 或oracle,那麼使用sqlite會非常容易上手,它支持一些基本的sql語句,還支持事務哦。

          5.沒有安全認證。sqltite生成一個數據庫文件之後,任何人都可以查看裏面的表信息,它不需要輸入用戶名或密碼什麼的,這是缺點也是優點,做了太多了驗證就不方便了

 

那麼在普通用java操作需要下載sqlite JDBC,那麼該如何操作呢?看了下面的例子你就會明白了:

 

           

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample
{
  public static void main(String[] args) throws ClassNotFoundException
  {
    // load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");

    Connection connection = null;
    try
    {
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.

      statement.executeUpdate("drop table if exists person");
      statement.executeUpdate("create table person (id integer, name string)");
      statement.executeUpdate("insert into person values(1, 'leo')");
      statement.executeUpdate("insert into person values(2, 'yui')");
      ResultSet rs = statement.executeQuery("select * from person");
      while(rs.next())
      {
        // read the result set
        System.out.println("name = " + rs.getString("name"));
        System.out.println("id = " + rs.getInt("id"));
      }
    }
    catch(SQLException e)
    {
      // if the error message is "out of memory", 
      // it probably means no database file is found
      System.err.println(e.getMessage());
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }
      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章