Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to create a data base and store retrieve data from the data base in python?

Description

To create a data base to store our data using python.

Process

   Install sqlalchemy package from anaconda.

  Import create_engine object from sqlalchemy.

  Read the data set.

  Create data base engine by using create_engine method.

  Store our data set to the data base using data.to_sql().

  Retrieving the data using sqlite3 query.

Sample Code

#import libraries and sub-packages

from sqlalchemy import create_engine

import pandas as pd

#read the sample data set

data=pd.read_csv(‘/home/soft27/soft27/

Sathish/Pythonfiles/Employee.csv’)

print(“Actual data/n”,data)

#Create the dtatbase engine

engine = create_engine(‘sqlite:///:memory:’)

#store the data as a table

data.to_sql(‘data_table’, engine)

#Query 1

result1 = pd.read_sql_query(‘SELECT * FROM data_table’, engine)

print(‘result1’)

print(result1)

#Query 2

result2 = pd.read_sql_query(‘SELECT name,sum(salary) FROM data_table group by name’, engine)

print(‘result2’)

print(result2)

Screenshots
create data base and store retrieve data
read the sample data set
Create the dtatbase engine