Main Content

SQLite JDBC forLinux

This tutorial shows how to set up a data source and connect to an SQLite database using the Database Explorer app or the command line. The tutorial uses the SQLite JDBC 3.7.2 Driver to connect to an SQLite Version 3.7.17 database.

Step 1. Verify the driver installation.

If the JDBC driver for SQLite is not installed on your computer, find the link on theDriver Installationpage to install the driver. Follow the instructions to download and install this driver on your computer.

If you do not want to install a driver and want to store relational data quickly, you can use the MATLAB®interface to SQLite. For details, seeInteract with Data in SQLite Database Using MATLAB Interface to SQLite

Step 2. Set up the data source.

You set up a data source using the Database Explorer app or the command line.

Set Up Data Source Using Database Explorer

  1. Open the Database Explorer app by clicking theAppstab on the MATLAB Toolstrip. Then, on the right of theAppssection, click theShow morearrow to open the apps gallery. UnderDatabase Connectivity and Reporting, clickDatabase Explorer.另外,输入databaseExplorerat the command line.

  2. In theData Sourcesection, selectConfigure Data Source>Configure JDBC data source

    Configure Data Source selection with the selected Configure JDBC data source

    The JDBC Data Source Configuration dialog box opens.

  3. In theNamebox, enter a name for your data source. (This example uses a data source namedSQLite.) You use this name to establish a connection to your database.

  4. From theVendorlist, selectOther

    JDBC Data Source Configuration dialog box with the selected Other vendor

  5. In theDriver Locationbox, enter the full path to the JDBC driver file.

  6. In theDriverbox, enter the SQLite driver Java®class object. Here, useorg.sqlite.JDBC

    Note

    Your entries forDriverandURLcan vary depending on the type and version of the JDBC driver and your database. For details, see the JDBC driver documentation for your database.

  7. Connect to the SQLite database by creating a URL string using the formatjdbc:subprotocol:subname.Thejdbcpart of this string remains constant for any JDBC driver.subprotocolis a database type, in this case,sqlite.For SQLite,subnamecontains the location of the database. For example, your URL string isjdbc:sqlite:dbpath, wheredbpathis the full path to your SQLite database on your computer. Enter your string in theURLbox and pressEnter

  8. UnderConnection Options, in theNamecolumn, enter the name of an additional driver-specific option. Then, in theValuecolumn, enter the value of the driver-specific option. Click the plus sign+to specify additional driver-specific options.

  9. ClickTest.The Test Connection dialog box opens. Enter the user name and password for your database, or leave these boxes blank if your database does not require them. ClickTest

    If your connection succeeds, the Database Explorer dialog box displays a message indicating the connection is successful. Otherwise, it displays an error message.

  10. ClickSave.The JDBC Data Source Configuration dialog box displays a message indicating the data source is saved successfully. Close this dialog box.

Set Up Data Source Using Command Line

  1. Create a JDBC data source for an SQLite database.

    vendor ="Other"; opts = databaseConnectionOptions("jdbc",vendor);
  2. Set the JDBC connection options. For example, this code assumes that you are connecting to a JDBC data source namedSQLite, full path of the SQLite driver location/home/user/Drivers/sqlite-jdbc-3.8.11.2.jar, SQLite driver Java class objectorg.sqlite.JDBC, and URL stringjdbc:sqlite:/home/user/Databases/sqlite.db

    opts = setoptions(opts,...'DataSourceName',"SQLite",...'JDBCDriverLocation',"/home/user/Drivers/sqlite-jdbc-3.8.11.2.jar",...'Driver',"org.sqlite.JDBC",...'URL',"jdbc:sqlite:/home/user/Databases/sqlite.db");
  3. Test the database connection by specifying the user nameusernameand passwordpwd, or leave these arguments blank if your database does not require them.

    username ="username"; password ="pwd"; status = testConnection(opts,username,password);
  4. Save the JDBC data source.

    saveAsDataSource(opts)

After you complete the data source setup, connect to the SQLite database using the Database Explorer app or the JDBC driver and command line.

Step 3. Connect using the Database Explorer app or the command line.

Connect to SQLite Using Database Explorer App

  1. On theDatabase Explorertab, in theConnectionssection, clickConnectand select the data source for the connection.

  2. In the connection dialog box, enter a user name and password, or leave these boxes blank if your database does not require them. ClickConnect

    The app connects to the database and displays its tables in theData Browserpane. A data source tab appears to the right of the pane. The title of the data source tab is the data source name that you defined during the setup. The data source tab contains emptySQL QueryandData Previewpanes.

  3. Select tables in theData Browserpane to query the database.

  4. Close the data source tab to close the SQL query. In theConnectionssection, close the database connection by clickingClose Connection

    Note

    If multiple connections are open, close the database connection of your choice by selecting the corresponding data source from theClose Connectionlist.

Connect to SQLite Using JDBC Driver and Command Line

  1. Connect to an SQLite database using the configured JDBC data source, user nameusername, and passwordpwd

    datasource ="SQLite"; username ="username"; password ="pwd"; conn = database(datasource,username,password);
  2. Close the database connection.

    close(conn)

See Also

Apps

Functions

Related Topics