Home > Enterprise >  creating database based on two dataframes
creating database based on two dataframes

Time:10-25

I have two dataframes.

One is music.

name Date Edition Song_ID Singer_ID
LA 01.05.2009 1 1 1
Second 13.07.2009 1 2 2
Mexico 13.07.2009 1 3 1
Let's go 13.09.2009 1 4 3
Hello 18.09.2009 1 5 (4,5)
Don't give up 12.02.2010 2 6 (5,6)
ZIC ZAC 18.03.2010 2 7 7
Blablabla 14.04.2010 2 8 2
Oh la la 14.05.2011 3 9 4
Food First 14.05.2011 3 10 5
La Vie est.. 17.06.2011 3 11 8
Jajajajajaja 13.07.2011 3 12 9

And another dataframe called singer

Singer nationality Singer_ID
JT Watson USA 1
Rafinha Brazil 2
Juan Casa Spain 3
Kidi USA 4
Dede USA 5
Briana USA 6
Jay Ado UK 7
Dani Australia 8
Mike Rich USA 9

Now I would like to create a database called musicten from these two dataframes using sqlite3 but it says conn is not defined. What I done so far

import sqlite3
sqlite3.connect('musicten.db')
conn=sqlite3.connect('musicten.db')

c = conn.cursor()

c.execute('''
          CREATE TABLE IF NOT EXISTS singer
          ([Singer_ID] INTEGER PRIMARY KEY, [Singer] TEXT,[nationality] TEXT )
          ''')
          
c.execute('''
          CREATE TABLE IF NOT EXISTS music
          ([Song_ID] INTEGER PRIMARY KEY, [Singer_ID] INTEGER SECONDARY KEY, [name] TEXT, [DATE] DATE, [Edition] INTEGER)
          ''')
                     
conn.commit()

I would like to create a database.

CodePudding user response:

Define you connection as below:

import sqlite3
conn = sqlite3.connect('musicten.db')
  • Related