zoukankan      html  css  js  c++  java
  • SQLite 数据库访问

    SQLite是一个很轻量的数据库。详细介绍我就不多说了,这个东西应该在嵌入式里面用得比较多。根据我看到的资料(几个月以前看的),现在安卓应用应该也会用这个,HTML5 也支持sqlite。

    SQLite也支持 SQL 语句。它的每一个数据库都是一个后缀为db的文件

    我手里有一个这样的文件,里面已经有一些数据了。

    怎么访问这个数据?

    1. 最简单的在 windows 平台下下载一个 SQLiteSpy 程序 

      a. File->Open Database, 选择要打开的 .db 文件

    可以看到

    执行一条简单的sql

    2. 使用 Java JDBC 访问 SQLite 数据库

    需要添加驱动依赖

    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.23.1</version>
        </dependency>

    java代码

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class App {
        
        public static void main(String[] args) {
            String url = "jdbc:sqlite:sqlDb.db";
            Connection conn = null;
            PreparedStatement pStatement = null;
            ResultSet resultSet = null;
            
            
            try {
                Class.forName("org.sqlite.JDBC");
                conn = DriverManager.getConnection(url);
                pStatement = conn.prepareStatement("select * from `AccLvl`");
                resultSet = pStatement.executeQuery();
                while(resultSet.next()) {
                    System.out.println("AccLvlNum:" + resultSet.getInt("AccLvlNum") + " " + "timeZoneTableId: " + resultSet.getInt("timeZoneTableId") + " " + "lastModified: " + resultSet.getTimestamp("lastModified"));
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                
                try {
                    if(resultSet != null) {
                        resultSet.close();
                    }
                    if(pStatement != null) {
                        pStatement.close();
                    }
                    if(conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }

    程序执行结果

    AccLvlNum:0 timeZoneTableId: 0 lastModified: 1970-01-19 01:55:17.192
    AccLvlNum:1 timeZoneTableId: 1 lastModified: 1970-01-19 01:55:17.192
    AccLvlNum:2 timeZoneTableId: 2 lastModified: 1970-01-19 01:55:17.192
    AccLvlNum:3 timeZoneTableId: 3 lastModified: 1970-01-19 01:55:17.192
    AccLvlNum:4 timeZoneTableId: 4 lastModified: 1970-01-19 01:55:17.193
    AccLvlNum:5 timeZoneTableId: 5 lastModified: 1970-01-19 01:55:17.193
    AccLvlNum:6 timeZoneTableId: 6 lastModified: 1970-01-19 01:55:17.193
    AccLvlNum:7 timeZoneTableId: 7 lastModified: 1970-01-19 01:55:17.193
    AccLvlNum:8 timeZoneTableId: 8 lastModified: 1970-01-19 01:55:17.193
    AccLvlNum:9 timeZoneTableId: 9 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:10 timeZoneTableId: 10 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:11 timeZoneTableId: 11 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:12 timeZoneTableId: 12 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:13 timeZoneTableId: 13 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:14 timeZoneTableId: 14 lastModified: 1970-01-19 01:55:17.194
    AccLvlNum:15 timeZoneTableId: 15 lastModified: 1970-01-19 01:55:17.195
    AccLvlNum:16 timeZoneTableId: 16 lastModified: 1970-01-19 01:55:17.195
    AccLvlNum:17 timeZoneTableId: 17 lastModified: 1970-01-19 01:55:17.195

    3. linux 访问(Ubuntu)

    首先安装 sqlite3

    sudo apt-get install sqlite3

    然后创建一个数据库,之前说了sqlite 的数据库就是一个 db 文件。 然后就可以进行数据库操作了,大体语法跟mysql是一样的,有细微的区别

    vmware@ubuntu:~/frank$ sqlite3 test.db
    SQLite version 3.11.0 2016-02-15 17:29:24
    Enter ".help" for usage hints.
    sqlite> .table
    sqlite> create table t_user(
       ...> id integer primary key autoincrement,
       ...> name varchar(20) not null,
       ...> password varchar(20) not null
       ...> );
    sqlite> insert into t_user(name, password) values ('jordan','1234'),('kobe', '3456'),('james', '7890');
    sqlite> select * from t_user;
    1|jordan|1234
    2|kobe|3456
    3|james|7890
    sqlite> select * from t_user where name='kobe';
    2|kobe|3456
    sqlite> 

    用到的sql

    .table
    create table t_user(
    id integer primary key autoincrement,
    name varchar(20) not null,
    password varchar(20) not null
    );
    insert into t_user(name, password) values ('jordan','1234'),('kobe', '3456'),('james', '7890');
    select * from t_user
    select * from t_user where name='kobe';
  • 相关阅读:
    poj 2777 Count Color
    poj 3264 Balanced Lineup
    hdu 1754 I hate it
    poj 3468 A Simple Problem with Integers
    noip 2013 提高组 Day2 部分题解
    [c/c++]指针(2)
    [c/c++]指针(1)
    vijos 1360 八数码问题
    [复习]快速幂算法
    noip 2013 提高组 day1
  • 原文地址:https://www.cnblogs.com/yeyeck/p/9765750.html
Copyright © 2011-2022 走看看