Skip to content

Instantly share code, notes, and snippets.

@vinaykudari
Last active August 11, 2023 14:00
Show Gist options
  • Save vinaykudari/a95bf9793d25c03aee5b9c82c00738b7 to your computer and use it in GitHub Desktop.
Save vinaykudari/a95bf9793d25c03aee5b9c82c00738b7 to your computer and use it in GitHub Desktop.
Create and Insert into DataBase
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sqlalchemy as db\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating Database and Table"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"engine = db.create_engine('sqlite:///test.sqlite') #Create test.sqlite automatically\n",
"connection = engine.connect()\n",
"metadata = db.MetaData()\n",
"\n",
"emp = db.Table('emp', metadata,\n",
" db.Column('Id', db.Integer()),\n",
" db.Column('name', db.String(255), nullable=False),\n",
" db.Column('salary', db.Float(), default=100.0),\n",
" db.Column('active', db.Boolean(), default=True)\n",
" )\n",
"\n",
"metadata.create_all(engine) #Creates the table"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inserting Data"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"#Inserting record one by one\n",
"query = db.insert(emp).values(Id=1, name='naveen', salary=60000.00, active=True) \n",
"ResultProxy = connection.execute(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Inserting many records at ones\n",
"query = db.insert(emp) \n",
"values_list = [{'Id':'2', 'name':'ram', 'salary':80000, 'active':False},\n",
" {'Id':'3', 'name':'ramesh', 'salary':70000, 'active':True}]\n",
"ResultProxy = connection.execute(query,values_list)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Id</th>\n",
" <th>name</th>\n",
" <th>salary</th>\n",
" <th>active</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>vinay</td>\n",
" <td>60000.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>satvik</td>\n",
" <td>60000.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>naveen</td>\n",
" <td>60000.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>rahul</td>\n",
" <td>80000.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Id name salary active\n",
"0 1 vinay 60000.0 True\n",
"1 1 satvik 60000.0 True\n",
"2 1 naveen 60000.0 True\n",
"3 2 rahul 80000.0 False"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = connection.execute(db.select([emp])).fetchall()\n",
"df = pd.DataFrame(results)\n",
"df.columns = results[0].keys()\n",
"df.head(4)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment