Documentation/How Tos/Using SQLite With OpenOffice.org
- 1 Introduction
- 2 Installing the ODBC Driver for SQLite
- 3 Creating a Data File
- 4 Configuring ODBC
- 5 Configuring the Data Source in OpenOffice.org
- 6 Creating Database Tables in OpenOffice.org
- 7 Using a Database with OpenOffice.org
The aim of this guide is to help OpenOffice.org users to take advantage of the SQLite database engine as a data source.
What is SQLite?
SQLite is a basic database engine that implements most of the features of SQL92. Unlike PostgreSQL and MySQL, SQLite stores a whole data base with all its tables a single file. Other benefits are: database access requires no database server, database files can be freely shared between machines with different byte orders and databases can be up to 2 terabytes (241 bytes) in size. Plus it is fast (twice as much as PostgreSQL and MySQL for most operations) and has a small memory footprint.
Data management can be achieved in the following ways:
- Via a C/C++ Linux library or Windows DLL.
- Via an in-line program (sqlite: available under Linux and Windows) that makes it possible to create and to manage the files of data bases.
- Via the SQLite PHP module or, if you have, PHP version 5 internally to a SQLite database.
- Via ODBC (Linux and Windows) which allows any application supporting this standard to reach a SQLite database.
- Using the experimental SDBC SQLite driver
This guide addresses ODBC which OpenOffice.org uses to attach to databases and SQLite, in particular.
Why use SQLite with OpenOffice.org?
The use of SQLite has many advantages:
- The data base is in only one file, in contrast to dBASE which creates a file per table, making it easy to exchange databases between various users.
- Java JRE or SDK is not required to support SQLite.
- No server is needed (local or remote) to access a database.
- ODBC drivers exist for *NIX and for Windows, thus enabling users of both environments to access data and exchange them.
- A new SDBC driver (native to OpenOffice.org) is available for experimentation.
- Sophisticated graphic interfaces, such as those of msaccess, can be easily developed using the forms, queries, states and macros of OpenOffice.org. These interfaces are stored separately in OpenOffice separate and can also be exchanged between users under UNIX or Windows.
and some disadvantages:
- Modifying the structure of a SQLite table after its creation is not directly possible. This means that you cannot easily add fields nor to modify their properties once that the table is created. Therefore you must prepare the structure of your tables before creating them.
- OpenOffice.org requires the use of the ODBC driver for accessing a SQLite database thus making this operation somewhat complicated. First it must be declared in ODBC and then in OpenOffice.org. The native SDBC driver should correct this situation when it is production ready.
- The initial database file cannot be created with OpenOffice.org but by using other tools.
In short, the principal advantages of the use of SQLite with OpenOffice.org are the format of data storage and the portability from one environment to another. In the list of disadvantages, the two last are not specific to SQLite, but are requirements of using ODBC. However, these are not as awkward as they do not take place during the initial creation of the database.
Installing the ODBC Driver for SQLite
For OpenOffice.org to access a SQLlite database in production, it is recommened that you install the ODBC driver. ODBC drivers for SQLite are independent of the SQLite project. However you can obtain a driver specifically developed by Christian Werner for OpenOffice.org from his site. This ODBC driver wraps the SQLite library and is available for Linux and Windows (for other UNIX, use the sources).
In order to be able to install the SQLite ODBC driver, unixODBC and SQLite must be installed first.
You will find most of the needed packages pre-installed in the majority of Linux distributions. For Mandrake, for example, install the following rpm files:
libunixODBC2-2.2.x-xmdk.i586.rpm libunixODBC2-devel-2.2.x-xmdk.i586.rpm libunixODBC2-qt-2.2.x-xmdk.i586.rpm unixODBC-2.2.x-xmdk.i586.rpm unixODBC-GUI-qt-2.2.x-xmdk.i586.rpm.
|Check that your version of unixODBC is at least version 2.2.5. An older version will cause OpenOffice.org to hang when using the ODBC for SQLite driver. If your version is one of the older versions which is the case for Debian or Mandrake 9.1, it will be necessary for you to download and install a more recent version of unixODBC.
If you do not find packages that are sufficiently recent in your distribution, download and compile the sources. Compiling from the sources does not install the graphic utility ODBCConfig. Thus you will have to manually configure ODBC.
Before starting, users of Mandrake, RedHat and other distributions using rpm distribution, should check that the distribution does not currently have the SQLite rpm already installed.
SQLite evolves very quickly and the packages present in any Linux distribution often provide a version of SQLite which is dated, (for example Mandrake 10.0 integrates SQLite 2.8.6). At the time of writing, the latest versions of the ODBC driver may include a more recent version of SQLite (2.8.13 for version 0.6.2 of the driver); however, it is to better to get and install a more recent version than that in your distribution. In theory, the SQLite driver for ODBC can be compiled with SQLite 2.8.6, but the using this version will introduce some limitations , such as no support auto-incremented fields.
Download and install the packages sqlite-x.x.x-x.i386.rpm and sqlite-devel-x.x.x-x.i386.rpm. This will also install the commandline version of sqlite.
For Mandrake (9.1, 9.2 or 10.0) users, the installation of these rpm will issue warnings that libreadline.so.4 is missing even though it is installed. This is because the rpm referred to above does not look for this library where Mandrake installs it. Check initially with rpmdrake that the library libreadline4 is installed (if not install it), then install the rpm sqlite from the commandline and force the no dependencies check:
$ rpm -ivh sqlite-devel-2.8.x-x.i386.rpm –nodeps $ rpm -ivh sqlite-2.8.x-x.i386.rpm --nodeps
If you have Debian or another distribution that will not install the rpm, [download http://www.sqlite.org/download.html http://www.sqlite.org/download.html] sqlite-x.x.x.tar.gz and compile the sources.
Since there are no precompiled binary packages (RPM or other) of sqliteODBC, download the source code from the author's site. Download a version at least equal to the 0.6.1, to avoid limitations with OpenOffice.org. If you have downloaded the .tar.gz, uncompress and unpack it using tar xvzf <filename>, which will create a directory sqliteODBC-0.6.x containing all the files. Change into this directory and type the following commands for compilation and installation:
$ ./configure –prefix=/usr $ make $ su $ make install
--prefix=/usr is optional, but is especially useful with Mandrake distributions, to install the driver in /usr/lib rather than in the default location, /usr/local/lib.
To complete installation, the driver should be integrated into unixODBC. To do this, launch the ODBCConfig utility as root in an open terminal window or xterm and type what follows:
$ su $ ODBCConfig
This command brings up the following window:
Click the Drivers tab and choose Add... Then fill the fields in the property window exactly as on the following graphic:
Then confirm using the checkmark icon on the left in top of the window. Your new driver will now appear in the Drivers tab:
The driver installation under Linux is now finished and you can exit ODBCConfig.
For those who do not have ODBCConfig or who prefer to carry out this configuration manually, it is necessary to edit the file /etc/odbcinst.ini as root and add the following lines:
[SQLite] Description= ODBC for SQLite Driver= /usr/lib/libsqliteODBC.so Setup= /usr/lib/libsqliteODBC.so FileUsage= 1 CPTimeout= CPReuse=
If you are comfortable compiling, the easy way is to get the source rpm of sqliteODBC. Once you have the srpm, it is easy to create a rpm for your distribution by typing the following instruction:
$ rpmbuild --rebuild sqliteODBC-0.60-1.src.rpm
You now should have a pre-compiled binary rpm ready for installation that, according to the author, self-configures the driver in ODBC thus avoiding the above manual procedures.
Under Windows installation and configuration is easier. Simply download the file sqliteODBC.exe and execute it. The driver installs automatically and is also configured automatically in ODBC. You will be able to check it by launching data Source ODBC from the control panel:
Creating a Data File
An initial database file cannot be created directly in OpenOffice.org but, rather, this is accomplished by using either the utility “sqlite” on the command line, or by using the tool sqlitebrowser which is graphic, very simple and available for both Linux and for Windows. If you wish to use sqlitebrowser, you will find it as the third entry in the list at: http://www.sqlite.org/cvstrac/wiki?p=SqliteTools,
The utility "sqlite" should be already installed if you use Linux and have installed SQLite from rpm. For other *NIX systems, you must build SQLite from source which you can find at http://www.sqlite.org/download.html .
Performance and usage under Linux and Windows is identical.
Open a terminal window or xterm and change to the directory where you want to create your database and then enter the following instructions:
$ sqlite <name of the database> SQLite version 2.8.12 Enter ".help" for instructions sqlite> .database sqlite> .exit
You can then check that the file was, indeed, created in your filesystem. The name of the file does not require a file extension, but you can give it the extension db so that is easy to locate as a database.
Using sqlite on the command line program also makes it possible to create tables and indices, to enter and post data, and to make queries providing you know the SQL language ; however, it is more practical for beginners to carry out these operations using OpenOffice.org.
Using SQLite Database Browser
After unpacking the file, launch sqlitebrowser and select File/New Database or the corresponding icon:
Choose the directory in which to place file and then enter name of the database file and click on Save.
Using this program, you can also create the tables, the fields and the indices, to access data and to visualize them. While sqlitebrowswer is suitable for these actions, there is the disadvantage that it does not show the full list of field types during field creation. In this case, it may be preferable to do it later using OpenOffice.org.
This step demonstrates how to define your new database in ODBC and to set that the driver to be used should be SQLite so that OpenOffice.org can access it. This operation has to be repeated for each new SQLite database that must be accessed through ODBC.
Launch ODBCConfig, go in the tab User DSN and choose Add.... A window appears then in which you must choose the SQLite driver. In the next window, as shown below, you enter a name for this connection (for example the name of your database) and enter the complete access path to the file which you created previously.
Validate the data on the screen, click the checkmark and you should see your new data source in the tab User DSN.
|: If in the place of this window you obtain an error message ("Could not construct has property list for (SQLite)"), your version of the driver is too old. Install version 0.6.1, or higher, which corrects this bug.|
If you do not have ODBCConfig or simply prefer to configure manually, launch your favorite editor, open (or create) the file odbc.ini in your home directory, and add the following lines to it:
[My Base] Description= My test database Driver= SQLite Database= /home/<user>/basename.db Timeout= 1000 StepAPI= No
You are now ready to access your data base with OpenOffice.org!
Open data Source ODBC and go to the tab Source data user. Choose To add... and start by selecting the driver SQLite ODBC Driver. In the following window, enter the name of this data source (Data Source Name) and the complete access path to your data file (Database Name), or select it by using the Browse.... button
Validate the data and exit ODBC. Nothing more is needed and you may launch OpenOffice.org and be able to reach your database!
Configuring the Data Source in OpenOffice.org
Now you can finally start using OpenOffice.org with SQLite. In order to make use of your new SQLite database and to add tables, it is necessary to configure the data source. To do this, use the menu Tools > Data Sources....
In the window shown below, click on New data source:
- In the Name field, replace Bibliography with the name of your data source.
- In Database type, select ODBC.
- For Data source URL, select the button with the three dots (on the right-hand side of the field). This opens a window in which you see all the data sources already declared in ODBC. Choose that which you created in the preceding chapter.
Voilà, your SQLite database is now accessible from OpenOffice.org!
Now click on Apply without leaving this window, so that we can create the tables and the fields of the database as described in the next section.
Creating Database Tables in OpenOffice.org
In the window, Data Source Administration, go in the tab Tables. To create your first table, select New icon in Table Designer to get a window in which create the fields for the new table:
- Enter the name of the new field in the Field Name column.
- Select its field type from the drop-down list.
- In the Field Properties zone, set the Length as necessary, default value of this field and choose the format.
- Repeat steps 1-3 for all fields.
Fields defined as type "integer", automatically increment the value of the field at the addition of each new record in this table,
Once you have created all your fields, checked that all is good and selected your primary key, save your new table by clicking on the Save icon and enter the name of your table
|Once you have saved your table, you cannot modify it! I.e. you will not be able to modify the properties of the fields any more, nor add or remove!|
This rather awkward constraint is specific to SQLite, which does not handle the SQL command ALTER COUNTS, and not the ODBC driver. This limitation is designed in by the authors in order to make the basic database engine as light as possible. However, this limitation may be eliminated in a future version. Therefore you are advised to check your table structure very carefully before recording it.
If you must modify a table, the only solution is to create a new table and save it under different name. By doing do, you can transfer the data from the original table to the new table.
Once a table is created, you can create an index to speed up queries and sorting on large volumes of data. To do this, select your table from the tab Tables in the Data Source Administration window and click on the icon "To publish the table". You are again in the window which posts the fields of your table. Select the fourth icon then "Outlines index..." and you will obtain the following window:
Click on the first icon "New index" and enter a name for this index. In the column Index Field, choose the field(s) to be indexed, the order of sorting, and check the Single box. Save this by clicking on the fourth icon To record the active index.
Now your database structure is created and you can use OpenOffice.org to connect, add data et cetera.
Using a Database with OpenOffice.org
This chapter is not specific to SQLite databases but its purpose is to succinctly present the various existing possibilities and how thy relates to the limitations and possibilities of SQLite.
Accessing databases via Data Sources
The simplest method to access your databases in OpenOffice.org is to open Data Sources dialogue using the icon on the Main toolbar which is the vertical bar on the left in Writer and Calc.
You can find the name of your database, named as you declared it in OpenOffice.org, in the left part of the panel by traversing the tree structure while clicking on "+". You will find a heading "Tables" from which you can select your table and have the contents displayed in the panel to the right. In this panel, you will be able to modify each record directly in the table, or to create new records by adding them in the line with the "*" symbol.
|This feature of auto-incrementing primary keys with type "integer" is not possible unless you use a version of the sqliteODBC driver that is, at least, version 0.6.0 or higher.|
Creating queries can also be done through the data sources panel. To post the heading Queries of your base in the panel of right-hand side and click above with the right button of the mouse. In the contextual menu, choose New Query (Design View) and you will open a new window:
You must start by selecting all the tables, that are to be included in your query, by choosing them from the list then while clicking Add. Then you can create connections by dragging one field of a table on to the corresponding field in the other table.
To define your query, select the fields which are to be part of the query and drag them to the table located in the lower part of the window, one-at-a-time. For each field added to the query, this table will enable you to choose the order of sorting, grouping, criteria, et cetera.
The various icons will then enable you to post the result of your query, to save it, publish it in mode SQL.
Remember creating queries offers so many possibilities that it is not possible to detail them here; however, a good guide to using SQL should help.
|The possibility of adding several tables during creation of a query on a SQLite database, and thus of querying multi-tables, is not possible with sqliteODBC driver, version 0.6.0.|
Creating forms with Writer or Calc is a very powerful feature of OpenOffice.org that makes it possible to create a true graphic presentation from your data base. Form features and applications are so numerous that it is not possible to describe them in this document. However, you will find information on how to make forms in OpenOffice.org Help and on http://documentation.openoffice.org/.
By creating subforms your options are multiplied tenfold.
|To create subforms in forms, it is necessary to use "named parameters " in order to establish the link between the various tables. For this you will have to use a macro whose installation and use are described in the following document: http://dba.openoffice.org/howto/param_subst.html.|
Exchanging databases between users
Thanks to the availability of driver sqliteODBC driver for both Linux and Windows (just as for OpenOffice.org, of course!), a SQLite database SQLite can be easily exchanged between users of the two environments. It is necessary, of course, that each has an installed ODBC driver that is aware of the target SQLite file and which is defined as a data source in OpenOffice.org. Likewise, Writer and Calc files (sxw and sxc) containing forms and macros can also be exchanged between users of the two environments.
|If forms are to function correctly for each user, it is absolutely necessary that each user applies exactly the same name to the data source during its creation.|
|Content on this page is licensed under the Public Documentation License (PDL).|