Database


RDMS(Relational Database Management System

Open Sourse

  • MySQL(php and web application)
  • PostgreSQl(huge web applications)
  • SQLITE (android applications)

Proprietary

  • MSSQL
  • Oracle

In [9]:
import sqlite3
#import the driver
##psycopg2 for protsgeSQL
# pymysql for MySQL

In [4]:
conn = sqlite3.connect('example.sqlite3')
#connecting to sqlite 3 and makes a new database file if file not already present

In [5]:
cur = conn.cursor()
#makes a file cursor we can make multiple cursors as well

In [6]:
cur.execute('CREATE TABLE countries (id integer, name text, iso3 text)')
#creating a new table


Out[6]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [7]:
cur.execute('SELECT * FROM countries')
#


Out[7]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [8]:
cur.fetchall()
#fetching the whole


Out[8]:
[]

In [10]:
cur.execute('INSERT INTO countries (id,name,iso3) VALUES (1, "Nepal", "NEP")')


Out[10]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [11]:
cur.execute('SELECT * FROM countries')


Out[11]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [12]:
cur.fetchall()


Out[12]:
[(1, 'Nepal', 'NEP')]

In [15]:
sql = '''INSERT INTO countries (id,name,iso3) VALUES (?,?,?)'''

cur.executemany(sql , [(2, 'India','INA'),
                       (3, 'Bhutan','BHU'),
                       (4, 'Afghanistan','AFG')])


Out[15]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [18]:
cur.execute('SELECT * FROM countries')


Out[18]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [19]:
cur.fetchall()


Out[19]:
[(1, 'Nepal', 'NEP'),
 (2, 'India', 'INA'),
 (3, 'Bhutan', 'BHU'),
 (4, 'Afghanistan', 'AFG')]

In [23]:
sql = 'INSERT INTO countries (id,name,iso3) VALUES (4, "PAKISTAN", "PAK")'
cur.execute(sql)


Out[23]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [24]:
cur.execute('SELECT * FROM countries')
cur.fetchall()


Out[24]:
[(1, 'Nepal', 'NEP'),
 (2, 'India', 'INA'),
 (3, 'Bhutan', 'BHU'),
 (4, 'Afghanistan', 'AFG'),
 (1, 'PAKISTAN', 'PAK'),
 (4, 'PAKISTAN', 'PAK')]

In [40]:
sql = 'UPDATE countries SET id = 5 WHERE iso3 = "4"'
cur.execute(sql)


Out[40]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [41]:
cur.execute('SELECT * FROM countries')
cur.fetchall()


Out[41]:
[(6, 'Nepal', 'NEP'),
 (5, 'India', 'INA'),
 (4, 'Bhutan', 'BHU'),
 (3, 'Afghanistan', 'AFG'),
 (2, 'Srilanka', 'SRI'),
 (1, 'Pakistan', 'PAK')]

In [42]:
sql = 'UPDATE countries '

In [43]:
conn.commit()

In [44]:
cur.execute('SELECT * FROM countries WHERE id > 2 ')
cur.fetchall()


Out[44]:
[(6, 'Nepal', 'NEP'),
 (5, 'India', 'INA'),
 (4, 'Bhutan', 'BHU'),
 (3, 'Afghanistan', 'AFG')]

In [46]:
cur.execute('SELECT *FROM countries WHERE name LIKE "%an"')
cur.fetchall()


Out[46]:
[(4, 'Bhutan', 'BHU'), (3, 'Afghanistan', 'AFG'), (1, 'Pakistan', 'PAK')]

In [47]:
cur.execute('SELECT *FROM countries WHERE name LIKE "%an%"')
cur.fetchall()


Out[47]:
[(4, 'Bhutan', 'BHU'),
 (3, 'Afghanistan', 'AFG'),
 (2, 'Srilanka', 'SRI'),
 (1, 'Pakistan', 'PAK')]

In [49]:
cur.execute('SELECT *FROM countries WHERE name LIKE "Pa%"')
cur.fetchall()


Out[49]:
[(1, 'Pakistan', 'PAK')]

In [51]:
cur.execute('DELETE FROM countries')


Out[51]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [52]:
cur.execute('SELECT *FROM countries')
cur.fetchall()


Out[52]:
[]

In [53]:
conn.commit()

In [54]:
import csv

In [63]:
sql = 'INSERT INTO countries (id,name,iso3) VALUES(?,?,?)'
_id = 1
with open('countries.txt','r') as datafile:
    csvfile = csv.DictReader(datafile)
    for row in csvfile:
        if row ['Common Name'] and row['ISO 3166-1 3 Letter Code']:
            cur.execute(sql, (_id, row['Common Name'], row['ISO 3166-1 3 Letter Code']))
            _id +=1       
        
conn.commit()

In [78]:
cur.execute('DELETE FROM country_list')


Out[78]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [ ]:


In [79]:
cur.execute('SELECT *FROM country_list')
cur.fetchall()


Out[79]:
[]

In [67]:
sql = '''CREATE TABLE
country_list(id integer primary key autoincrement,
country_name text not null,
iso3 text not null unique)'''
cur.execute(sql)


Out[67]:
<sqlite3.Cursor at 0xf1a7fb60a0>

In [81]:
sql = 'INSERT INTO country_list (country_name,iso3) VALUES(?,?)'

with open('countries.txt','r') as datafile:
    csvfile = csv.DictReader(datafile)
    for row in csvfile:
        if row ['Formal Name'] and row['Formal Name']:
            cur.execute(sql, (row['Formal Name'], row['Formal Name']))
              
        
conn.commit()

In [82]:
cur.execute('SELECT *FROM country_list')
cur.fetchall()


Out[82]:
[(563, 'Islamic State of Afghanistan', 'Islamic State of Afghanistan'),
 (564, 'Republic of Albania', 'Republic of Albania'),
 (565,
  "People's Democratic Republic of Algeria",
  "People's Democratic Republic of Algeria"),
 (566, 'Principality of Andorra', 'Principality of Andorra'),
 (567, 'Republic of Angola', 'Republic of Angola'),
 (568, 'Argentine Republic', 'Argentine Republic'),
 (569, 'Republic of Armenia', 'Republic of Armenia'),
 (570, 'Commonwealth of Australia', 'Commonwealth of Australia'),
 (571, 'Republic of Austria', 'Republic of Austria'),
 (572, 'Republic of Azerbaijan', 'Republic of Azerbaijan'),
 (573, 'Commonwealth of The Bahamas', 'Commonwealth of The Bahamas'),
 (574, 'Kingdom of Bahrain', 'Kingdom of Bahrain'),
 (575, "People's Republic of Bangladesh", "People's Republic of Bangladesh"),
 (576, 'Republic of Belarus', 'Republic of Belarus'),
 (577, 'Kingdom of Belgium', 'Kingdom of Belgium'),
 (578, 'Republic of Benin', 'Republic of Benin'),
 (579, 'Kingdom of Bhutan', 'Kingdom of Bhutan'),
 (580, 'Republic of Bolivia', 'Republic of Bolivia'),
 (581, 'Republic of Botswana', 'Republic of Botswana'),
 (582, 'Federative Republic of Brazil', 'Federative Republic of Brazil'),
 (583, 'Negara Brunei Darussalam', 'Negara Brunei Darussalam'),
 (584, 'Republic of Bulgaria', 'Republic of Bulgaria'),
 (585, 'Republic of Burundi', 'Republic of Burundi'),
 (586, 'Kingdom of Cambodia', 'Kingdom of Cambodia'),
 (587, 'Republic of Cameroon', 'Republic of Cameroon'),
 (588, 'Republic of Cape Verde', 'Republic of Cape Verde'),
 (589, 'Republic of Chad', 'Republic of Chad'),
 (590, 'Republic of Chile', 'Republic of Chile'),
 (591, "People's Republic of China", "People's Republic of China"),
 (592, 'Republic of Colombia', 'Republic of Colombia'),
 (593, 'Union of Comoros', 'Union of Comoros'),
 (594, 'Democratic Republic of the Congo', 'Democratic Republic of the Congo'),
 (595, 'Republic of the Congo', 'Republic of the Congo'),
 (596, 'Republic of Costa Rica', 'Republic of Costa Rica'),
 (597, "Republic of Cote d'Ivoire", "Republic of Cote d'Ivoire"),
 (598, 'Republic of Croatia', 'Republic of Croatia'),
 (599, 'Republic of Cuba', 'Republic of Cuba'),
 (600, 'Republic of Cyprus', 'Republic of Cyprus'),
 (601, 'Kingdom of Denmark', 'Kingdom of Denmark'),
 (602, 'Republic of Djibouti', 'Republic of Djibouti'),
 (603, 'Commonwealth of Dominica', 'Commonwealth of Dominica'),
 (604, 'Republic of Ecuador', 'Republic of Ecuador'),
 (605, 'Arab Republic of Egypt', 'Arab Republic of Egypt'),
 (606, 'Republic of El Salvador', 'Republic of El Salvador'),
 (607, 'Republic of Equatorial Guinea', 'Republic of Equatorial Guinea'),
 (608, 'State of Eritrea', 'State of Eritrea'),
 (609, 'Republic of Estonia', 'Republic of Estonia'),
 (610,
  'Federal Democratic Republic of Ethiopia',
  'Federal Democratic Republic of Ethiopia'),
 (611, 'Republic of the Fiji Islands', 'Republic of the Fiji Islands'),
 (612, 'Republic of Finland', 'Republic of Finland'),
 (613, 'French Republic', 'French Republic'),
 (614, 'Gabonese Republic', 'Gabonese Republic'),
 (615, 'Republic of The Gambia', 'Republic of The Gambia'),
 (616, 'Republic of Georgia', 'Republic of Georgia'),
 (617, 'Federal Republic of Germany', 'Federal Republic of Germany'),
 (618, 'Republic of Ghana', 'Republic of Ghana'),
 (619, 'Hellenic Republic', 'Hellenic Republic'),
 (620, 'Republic of Guatemala', 'Republic of Guatemala'),
 (621, 'Republic of Guinea', 'Republic of Guinea'),
 (622, 'Republic of Guinea-Bissau', 'Republic of Guinea-Bissau'),
 (623, 'Co-operative Republic of Guyana', 'Co-operative Republic of Guyana'),
 (624, 'Republic of Haiti', 'Republic of Haiti'),
 (625, 'Republic of Honduras', 'Republic of Honduras'),
 (626, 'Republic of Hungary', 'Republic of Hungary'),
 (627, 'Republic of Iceland', 'Republic of Iceland'),
 (628, 'Republic of India', 'Republic of India'),
 (629, 'Republic of Indonesia', 'Republic of Indonesia'),
 (630, 'Islamic Republic of Iran', 'Islamic Republic of Iran'),
 (631, 'Republic of Iraq', 'Republic of Iraq'),
 (632, 'State of Israel', 'State of Israel'),
 (633, 'Italian Republic', 'Italian Republic'),
 (634, 'Hashemite Kingdom of Jordan', 'Hashemite Kingdom of Jordan'),
 (635, 'Republic of Kazakhstan', 'Republic of Kazakhstan'),
 (636, 'Republic of Kenya', 'Republic of Kenya'),
 (637, 'Republic of Kiribati', 'Republic of Kiribati'),
 (638,
  "Democratic People's Republic of Korea",
  "Democratic People's Republic of Korea"),
 (639, 'Republic of Korea', 'Republic of Korea'),
 (640, 'State of Kuwait', 'State of Kuwait'),
 (641, 'Kyrgyz Republic', 'Kyrgyz Republic'),
 (642, "Lao People's Democratic Republic", "Lao People's Democratic Republic"),
 (643, 'Republic of Latvia', 'Republic of Latvia'),
 (644, 'Lebanese Republic', 'Lebanese Republic'),
 (645, 'Kingdom of Lesotho', 'Kingdom of Lesotho'),
 (646, 'Republic of Liberia', 'Republic of Liberia'),
 (647,
  "Great Socialist People's Libyan Arab Jamahiriya",
  "Great Socialist People's Libyan Arab Jamahiriya"),
 (648, 'Principality of Liechtenstein', 'Principality of Liechtenstein'),
 (649, 'Republic of Lithuania', 'Republic of Lithuania'),
 (650, 'Grand Duchy of Luxembourg', 'Grand Duchy of Luxembourg'),
 (651, 'Republic of Macedonia', 'Republic of Macedonia'),
 (652, 'Republic of Madagascar', 'Republic of Madagascar'),
 (653, 'Republic of Malawi', 'Republic of Malawi'),
 (654, 'Republic of Maldives', 'Republic of Maldives'),
 (655, 'Republic of Mali', 'Republic of Mali'),
 (656, 'Republic of Malta', 'Republic of Malta'),
 (657, 'Republic of the Marshall Islands', 'Republic of the Marshall Islands'),
 (658, 'Islamic Republic of Mauritania', 'Islamic Republic of Mauritania'),
 (659, 'Republic of Mauritius', 'Republic of Mauritius'),
 (660, 'United Mexican States', 'United Mexican States'),
 (661, 'Federated States of Micronesia', 'Federated States of Micronesia'),
 (662, 'Republic of Moldova', 'Republic of Moldova'),
 (663, 'Principality of Monaco', 'Principality of Monaco'),
 (664, 'Republic of Montenegro', 'Republic of Montenegro'),
 (665, 'Kingdom of Morocco', 'Kingdom of Morocco'),
 (666, 'Republic of Mozambique', 'Republic of Mozambique'),
 (667, 'Union of Myanmar', 'Union of Myanmar'),
 (668, 'Republic of Namibia', 'Republic of Namibia'),
 (669, 'Republic of Nauru', 'Republic of Nauru'),
 (670, 'Kingdom of the Netherlands', 'Kingdom of the Netherlands'),
 (671, 'Republic of Nicaragua', 'Republic of Nicaragua'),
 (672, 'Republic of Niger', 'Republic of Niger'),
 (673, 'Federal Republic of Nigeria', 'Federal Republic of Nigeria'),
 (674, 'Kingdom of Norway', 'Kingdom of Norway'),
 (675, 'Sultanate of Oman', 'Sultanate of Oman'),
 (676, 'Islamic Republic of Pakistan', 'Islamic Republic of Pakistan'),
 (677, 'Republic of Palau', 'Republic of Palau'),
 (678, 'Republic of Panama', 'Republic of Panama'),
 (679,
  'Independent State of Papua New Guinea',
  'Independent State of Papua New Guinea'),
 (680, 'Republic of Paraguay', 'Republic of Paraguay'),
 (681, 'Republic of Peru', 'Republic of Peru'),
 (682, 'Republic of the Philippines', 'Republic of the Philippines'),
 (683, 'Republic of Poland', 'Republic of Poland'),
 (684, 'Portuguese Republic', 'Portuguese Republic'),
 (685, 'State of Qatar', 'State of Qatar'),
 (686, 'Russian Federation', 'Russian Federation'),
 (687, 'Republic of Rwanda', 'Republic of Rwanda'),
 (688,
  'Federation of Saint Kitts and Nevis',
  'Federation of Saint Kitts and Nevis'),
 (689, 'Independent State of Samoa', 'Independent State of Samoa'),
 (690, 'Republic of San Marino', 'Republic of San Marino'),
 (691,
  'Democratic Republic of Sao Tome and Principe',
  'Democratic Republic of Sao Tome and Principe'),
 (692, 'Kingdom of Saudi Arabia', 'Kingdom of Saudi Arabia'),
 (693, 'Republic of Senegal', 'Republic of Senegal'),
 (694, 'Republic of Serbia', 'Republic of Serbia'),
 (695, 'Republic of Seychelles', 'Republic of Seychelles'),
 (696, 'Republic of Sierra Leone', 'Republic of Sierra Leone'),
 (697, 'Republic of Singapore', 'Republic of Singapore'),
 (698, 'Slovak Republic', 'Slovak Republic'),
 (699, 'Republic of Slovenia', 'Republic of Slovenia'),
 (700, 'Republic of South Africa', 'Republic of South Africa'),
 (701, 'Kingdom of Spain', 'Kingdom of Spain'),
 (702,
  'Democratic Socialist Republic of Sri Lanka',
  'Democratic Socialist Republic of Sri Lanka'),
 (703, 'Republic of the Sudan', 'Republic of the Sudan'),
 (704, 'Republic of Suriname', 'Republic of Suriname'),
 (705, 'Kingdom of Swaziland', 'Kingdom of Swaziland'),
 (706, 'Kingdom of Sweden', 'Kingdom of Sweden'),
 (707, 'Swiss Confederation', 'Swiss Confederation'),
 (708, 'Syrian Arab Republic', 'Syrian Arab Republic'),
 (709, 'Republic of Tajikistan', 'Republic of Tajikistan'),
 (710, 'United Republic of Tanzania', 'United Republic of Tanzania'),
 (711, 'Kingdom of Thailand', 'Kingdom of Thailand'),
 (712,
  'Democratic Republic of Timor-Leste',
  'Democratic Republic of Timor-Leste'),
 (713, 'Togolese Republic', 'Togolese Republic'),
 (714, 'Kingdom of Tonga', 'Kingdom of Tonga'),
 (715, 'Republic of Trinidad and Tobago', 'Republic of Trinidad and Tobago'),
 (716, 'Tunisian Republic', 'Tunisian Republic'),
 (717, 'Republic of Turkey', 'Republic of Turkey'),
 (718, 'Republic of Uganda', 'Republic of Uganda'),
 (719, 'United Arab Emirates', 'United Arab Emirates'),
 (720,
  'United Kingdom of Great Britain and Northern Ireland',
  'United Kingdom of Great Britain and Northern Ireland'),
 (721, 'United States of America', 'United States of America'),
 (722, 'Oriental Republic of Uruguay', 'Oriental Republic of Uruguay'),
 (723, 'Republic of Uzbekistan', 'Republic of Uzbekistan'),
 (724, 'Republic of Vanuatu', 'Republic of Vanuatu'),
 (725, 'State of the Vatican City', 'State of the Vatican City'),
 (726, 'Bolivarian Republic of Venezuela', 'Bolivarian Republic of Venezuela'),
 (727, 'Socialist Republic of Vietnam', 'Socialist Republic of Vietnam'),
 (728, 'Republic of Yemen', 'Republic of Yemen'),
 (729, 'Republic of Zambia', 'Republic of Zambia'),
 (730, 'Republic of Zimbabwe', 'Republic of Zimbabwe'),
 (731, 'Republic of Abkhazia', 'Republic of Abkhazia'),
 (732, 'Republic of China', 'Republic of China'),
 (733, 'Nagorno-Karabakh Republic', 'Nagorno-Karabakh Republic'),
 (734,
  'Turkish Republic of Northern Cyprus',
  'Turkish Republic of Northern Cyprus'),
 (735,
  'Pridnestrovian Moldavian Republic',
  'Pridnestrovian Moldavian Republic'),
 (736, 'Republic of Somaliland', 'Republic of Somaliland'),
 (737, 'Republic of South Ossetia', 'Republic of South Ossetia'),
 (738,
  'Territory of Ashmore and Cartier Islands',
  'Territory of Ashmore and Cartier Islands'),
 (739, 'Territory of Christmas Island', 'Territory of Christmas Island'),
 (740,
  'Territory of Cocos (Keeling) Islands',
  'Territory of Cocos (Keeling) Islands'),
 (741, 'Coral Sea Islands Territory', 'Coral Sea Islands Territory'),
 (742,
  'Territory of Heard Island and McDonald Islands',
  'Territory of Heard Island and McDonald Islands'),
 (743, 'Territory of Norfolk Island', 'Territory of Norfolk Island'),
 (744,
  'Overseas Country of French Polynesia',
  'Overseas Country of French Polynesia'),
 (745,
  'Departmental Collectivity of Mayotte',
  'Departmental Collectivity of Mayotte'),
 (746, 'Collectivity of Saint Barthelemy', 'Collectivity of Saint Barthelemy'),
 (747, 'Collectivity of Saint Martin', 'Collectivity of Saint Martin'),
 (748,
  'Territorial Collectivity of Saint Pierre and Miquelon',
  'Territorial Collectivity of Saint Pierre and Miquelon'),
 (749,
  'Collectivity of the Wallis and Futuna Islands',
  'Collectivity of the Wallis and Futuna Islands'),
 (750,
  'Territory of the French Southern and Antarctic Lands',
  'Territory of the French Southern and Antarctic Lands'),
 (751, 'Bailiwick of Guernsey', 'Bailiwick of Guernsey'),
 (752, 'Bailiwick of Jersey', 'Bailiwick of Jersey'),
 (753,
  'Commonwealth of The Northern Mariana Islands',
  'Commonwealth of The Northern Mariana Islands'),
 (754, 'Commonwealth of Puerto Rico', 'Commonwealth of Puerto Rico'),
 (755, 'Territory of American Samoa', 'Territory of American Samoa'),
 (756, 'Territory of Guam', 'Territory of Guam'),
 (757, 'United States Virgin Islands', 'United States Virgin Islands'),
 (758,
  'Hong Kong Special Administrative Region',
  'Hong Kong Special Administrative Region'),
 (759,
  'Macau Special Administrative Region',
  'Macau Special Administrative Region'),
 (760, 'Overseas Region of Guiana', 'Overseas Region of Guiana'),
 (761, 'Overseas Region of Guadeloupe', 'Overseas Region of Guadeloupe'),
 (762, 'Overseas Region of Martinique', 'Overseas Region of Martinique'),
 (763, 'Overseas Region of Reunion', 'Overseas Region of Reunion')]

Data base for library management system


In [84]:
connn = sqlite3.connect("Library Management System.txt")

In [85]:
curs = connn.cursor()

In [91]:
sql = '''CREATE TABLE 
books(book_id text,
isbn integer not null unique,
book_name text)'''
curs.execute(sql)


---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-91-3b8cfdd5e857> in <module>()
      3 isbn integer not null unique,
      4 book_name text)'''
----> 5 curs.execute(sql)

OperationalError: table books already exists

In [ ]:


In [92]:
sql2 = '''CREATE TABLE
Student( roll number integer not null unique,
name text not null,
faculty text)'''
curs.execute(sql2)


Out[92]:
<sqlite3.Cursor at 0xf1a32d62d0>

In [94]:
sql3 = '''CREATE TABLE
Teacher(name text not null,
faculty text)
'''
curs.execute(sql3)


Out[94]:
<sqlite3.Cursor at 0xf1a32d62d0>
class Temperature:
    temp = 10

    def __intit__(self,temp):
        self.temp = temp

    def get_temperature(self):
        return self.temp

    @classmethod
    def get_class_temperature(cls):
        return cls.temp

    @staticmethod
    def get_added_method(inst):
        return inst.temp +10


t = Temperature (20)
#t = Temperature() --> Temperature.__init__(t,20)
t.get_temperature() #20
#Temperature.get_temperature(t)
Temperature.get_class_temperature() #10
#Temperature.get_class_temperature(Temperature) 
Temperature.get_added_temperature(t) #30
#Temperature.get_added_temperature(t)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: