Convert Figma logo to code with AI

simonw logosqlite-utils

Python CLI utility and library for manipulating SQLite databases

1,623
111
1,623
111

Top Related Projects

Official home of the DB Browser for SQLite (DB4S) project. Previously known as "SQLite Database Browser" and "Database Browser for SQLite". Website at:

Web-based SQLite database browser written in Python

2,439

CLI for SQLite Databases with auto-completion and syntax highlighting

11,975

Postgres CLI with autocompletion and syntax highlighting

Quick Overview

sqlite-utils is a Python library that provides a set of utilities for working with SQLite databases. It simplifies common tasks such as creating, querying, and manipulating SQLite databases, making it easier to integrate SQLite into Python-based applications.

Pros

  • Simplifies SQLite Interactions: The library abstracts away many of the low-level details of working with SQLite, allowing developers to focus on their application logic rather than database management.
  • Supports Multiple Data Formats: sqlite-utils can import and export data in various formats, including CSV, JSON, and SQLite databases, making it easier to work with data from different sources.
  • Provides Powerful Querying Capabilities: The library offers a rich set of query functions, including support for complex SQL queries, table manipulation, and data transformation.
  • Integrates with Other Python Libraries: sqlite-utils can be easily integrated with other popular Python libraries, such as Pandas and Datasette, expanding the range of use cases.

Cons

  • Limited to SQLite: The library is specifically designed for working with SQLite databases, and may not be suitable for applications that require more advanced database features or support for other database engines.
  • Steep Learning Curve for Beginners: While the library aims to simplify SQLite interactions, it may still have a learning curve for developers who are new to working with databases or SQL.
  • Potential Performance Limitations: For large or complex databases, the performance of sqlite-utils may not be as optimal as working directly with the SQLite library or other database-specific tools.
  • Dependency on External Libraries: The library relies on several external dependencies, which may increase the complexity of managing the project's dependencies.

Code Examples

Here are a few examples of how to use sqlite-utils:

  1. Creating a SQLite Database and Table:
import sqlite_utils

db = sqlite_utils.Database("example.db")
db["users"].insert_all([
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"},
    {"id": 3, "name": "Charlie"}
])
  1. Querying Data from a SQLite Database:
users = db["users"].rows
for user in users:
    print(f"{user['id']}: {user['name']}")
  1. Importing Data from a CSV File:
db.import_csv("data.csv", table_name="data")
  1. Exporting Data to a JSON File:
db["users"].to_json("users.json")

Getting Started

To get started with sqlite-utils, follow these steps:

  1. Install the library using pip:
pip install sqlite-utils
  1. Import the sqlite_utils module and create a Database object:
import sqlite_utils

db = sqlite_utils.Database("example.db")
  1. Use the various methods provided by the Database and Table objects to interact with your SQLite database. For example, you can create a new table, insert data, and query the database:
# Create a new table
db["users"].insert_all([
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"},
    {"id": 3, "name": "Charlie"}
])

# Query the table
users = db["users"].rows
for user in users:
    print(f"{user['id']}: {user['name']}")
  1. Explore the project's documentation for more advanced usage examples and features.

Competitor Comparisons

Official home of the DB Browser for SQLite (DB4S) project. Previously known as "SQLite Database Browser" and "Database Browser for SQLite". Website at:

Pros of SQLite Browser

  • SQLite Browser provides a graphical user interface (GUI) for working with SQLite databases, making it easier for non-technical users to interact with the data.
  • The application supports a wide range of SQLite features, including creating, editing, and querying databases.
  • SQLite Browser includes a SQL editor with syntax highlighting and code completion, improving the user experience.

Cons of SQLite Browser

  • SQLite-utils is a Python library, which may be more suitable for developers who prefer to work with code rather than a GUI.
  • SQLite Browser may have a steeper learning curve for users who are more comfortable with command-line tools or programming languages.
  • The SQLite Browser project is less actively maintained compared to the sqlite-utils project, with fewer recent commits and issues.

Code Comparison

SQLite-utils:

import sqlite_utils

db = sqlite_utils.Database("example.db")
db["users"].insert_all([
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"},
], pk="id")

SQLite Browser:

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT
);

INSERT INTO users (id, name) VALUES
    (1, 'Alice'),
    (2, 'Bob');

The SQLite-utils code demonstrates how to create a database, a table, and insert data using a Python library, while the SQLite Browser code shows the equivalent SQL statements to achieve the same result.

Web-based SQLite database browser written in Python

Pros of sqlite-web

  • Provides a web-based interface for browsing and managing SQLite databases, making it easier to visualize and interact with data.
  • Supports multiple database connections, allowing users to work with different databases simultaneously.
  • Offers a range of features, including SQL query execution, table management, and data export/import.

Cons of sqlite-web

  • Requires a separate server process to be running, which may add complexity to the deployment process.
  • The web interface may not be as feature-rich or customizable as some desktop-based SQLite management tools.
  • The project has not been actively maintained for a few years, which could impact its long-term viability and compatibility with newer versions of SQLite.

Code Comparison

Here's a brief comparison of the code for creating a new SQLite database using both sqlite-utils and sqlite-web:

sqlite-utils:

import sqlite_utils
db = sqlite_utils.Database("example.db")
db["users"].insert_all([
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
])

sqlite-web:

from sqlite_web.app import create_app
app = create_app("example.db")
app.run()

The sqlite-utils code directly interacts with the SQLite database, allowing for programmatic creation and manipulation of tables and data. In contrast, the sqlite-web code sets up a web application that provides a user interface for managing the database.

2,439

CLI for SQLite Databases with auto-completion and syntax highlighting

Pros of litecli

  • litecli provides a more user-friendly command-line interface for interacting with SQLite databases, with features like auto-completion, syntax highlighting, and pretty-printing of query results.
  • litecli supports multiple database engines, including SQLite, MySQL, and PostgreSQL, making it a more versatile tool.
  • litecli has a smaller footprint and faster startup time compared to sqlite-utils, which may be preferable for certain use cases.

Cons of litecli

  • sqlite-utils provides a more comprehensive set of features and utilities for working with SQLite databases, including the ability to create, modify, and query databases programmatically.
  • sqlite-utils has a larger and more active community, with more documentation and third-party integrations available.
  • sqlite-utils may be more suitable for complex or automated workflows, where its programmatic capabilities are more valuable.

Code Comparison

sqlite-utils:

import sqlite_utils

db = sqlite_utils.Database("example.db")
db["users"].insert_all([
    {"name": "Alice", "email": "alice@example.com"},
    {"name": "Bob", "email": "bob@example.com"}
])

litecli:

import sqlite3

conn = sqlite3.connect("example.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (name TEXT, email TEXT)")
cursor.execute("INSERT INTO users VALUES (?, ?)", ("Alice", "alice@example.com"))
cursor.execute("INSERT INTO users VALUES (?, ?)", ("Bob", "bob@example.com"))
conn.commit()
11,975

Postgres CLI with autocompletion and syntax highlighting

Pros of pgcli

  • Provides a more feature-rich and interactive command-line interface for PostgreSQL, including auto-completion, syntax highlighting, and query history.
  • Supports multiple database connections and allows switching between them.
  • Offers advanced querying capabilities, such as the ability to execute SQL scripts and view query plans.

Cons of pgcli

  • Specific to PostgreSQL, while sqlite-utils supports a wider range of database engines, including SQLite, MySQL, and PostgreSQL.
  • May have a steeper learning curve for users who are more familiar with basic SQL clients.

Code Comparison

sqlite-utils:

import sqlite_utils

db = sqlite_utils.Database("example.db")
db["users"].insert_all([
    {"name": "Alice", "email": "alice@example.com"},
    {"name": "Bob", "email": "bob@example.com"}
])

pgcli:

import pgcli.main

pgcli = pgcli.main.PGCli()
pgcli.connect(database="example", user="myuser", password="mypassword")
pgcli.run_cli()

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

sqlite-utils

PyPI Changelog Python 3.x Tests Documentation Status codecov License discord

Python CLI utility and library for manipulating SQLite databases.

Some feature highlights

Read more on my blog, in this series of posts on New features in sqlite-utils and other entries tagged sqliteutils.

Installation

pip install sqlite-utils

Or if you use Homebrew for macOS:

brew install sqlite-utils

Using as a CLI tool

Now you can do things with the CLI utility like this:

$ sqlite-utils memory dogs.csv "select * from t"
[{"id": 1, "age": 4, "name": "Cleo"},
 {"id": 2, "age": 2, "name": "Pancakes"}]

$ sqlite-utils insert dogs.db dogs dogs.csv --csv
[####################################]  100%

$ sqlite-utils tables dogs.db --counts
[{"table": "dogs", "count": 2}]

$ sqlite-utils dogs.db "select id, name from dogs"
[{"id": 1, "name": "Cleo"},
 {"id": 2, "name": "Pancakes"}]

$ sqlite-utils dogs.db "select * from dogs" --csv
id,age,name
1,4,Cleo
2,2,Pancakes

$ sqlite-utils dogs.db "select * from dogs" --table
  id    age  name
----  -----  --------
   1      4  Cleo
   2      2  Pancakes

You can import JSON data into a new database table like this:

$ curl https://api.github.com/repos/simonw/sqlite-utils/releases \
    | sqlite-utils insert releases.db releases - --pk id

Or for data in a CSV file:

$ sqlite-utils insert dogs.db dogs dogs.csv --csv

sqlite-utils memory lets you import CSV or JSON data into an in-memory database and run SQL queries against it in a single command:

$ cat dogs.csv | sqlite-utils memory - "select name, age from stdin"

See the full CLI documentation for comprehensive coverage of many more commands.

Using as a library

You can also import sqlite_utils and use it as a Python library like this:

import sqlite_utils
db = sqlite_utils.Database("demo_database.db")
# This line creates a "dogs" table if one does not already exist:
db["dogs"].insert_all([
    {"id": 1, "age": 4, "name": "Cleo"},
    {"id": 2, "age": 2, "name": "Pancakes"}
], pk="id")

Check out the full library documentation for everything else you can do with the Python library.

Related projects

  • Datasette: A tool for exploring and publishing data
  • csvs-to-sqlite: Convert CSV files into a SQLite database
  • db-to-sqlite: CLI tool for exporting a MySQL or PostgreSQL database as a SQLite file
  • dogsheep: A family of tools for personal analytics, built on top of sqlite-utils