{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating and inserting data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import ibis\n",
    "\n",
    "ibis.options.interactive = True\n",
    "\n",
    "connection = ibis.sqlite.connect(os.path.join('data', 'geography.db'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating new tables from Ibis expressions\n",
    "\n",
    "\n",
    "Suppose you have an Ibis expression that produces a table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = connection.table('countries')\n",
    "\n",
    "continent_name = (countries.continent\n",
    "                           .case()\n",
    "                           .when('AF', 'Africa')\n",
    "                           .when('AN', 'Antarctica')\n",
    "                           .when('AS', 'Asia')\n",
    "                           .when('EU', 'Europe')\n",
    "                           .when('NA', 'North America')\n",
    "                           .when('OC', 'Oceania')\n",
    "                           .when('SA', 'South America')\n",
    "                           .else_(countries.continent)\n",
    "                           .end()\n",
    "                           .name('continent_name'))\n",
    "\n",
    "expr = countries[countries.continent, continent_name].distinct()\n",
    "expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To create a table in the database from the results of this expression, use the connection's `create_table` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "connection.create_table('continents', expr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "continents = connection.table('continents')\n",
    "continents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tables can be similarly dropped with `drop_table`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "connection.drop_table('continents')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inserting data into existing tables\n",
    "\n",
    "\n",
    "Some backends support inserting data into existing tables from expressions. This can be done using `connection.insert('table_name', expr)`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
