Let's say i have two simple .csv files which i want to import into a SQLite database:
id
state
1
State1
2
State2
3
State3
id
district
values
state_fk
1
District1
123
1
2
District2
456
2
3
District3
789
3
I receive an updated version of the district file every 2 weeks. I simply read the csv files and import/update them via df.to_sql('table', con, if_exists='replace').
Is there a way in pandas to set a PK-FK relation between states.id and districts.state_fk?
I would like to regulary update the district table with new values and don't set the relation manually again after each update.
I don't think it's possible to create foreign keys with pandas. That is not what the module is for.
Instead of replacing the database table each time could you not first empty/truncate it using the sqlite3 module and then import your data using the if_exists='append' option ? Would look something like that :
import sqlite3
con = sqlite3.connect('example.db')
cur = con.cursor()
cur.execute('''TRUNCATE TABLE table''')
con.commit()
con.close()
df.to_sql('table', con, if_exists='append')
Related
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)
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
Now I would like to create a database called musicten from these two dataframes using sqlite3.
What I done so far:
import sqlite3
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()
import sqlite3
conn = sqlite3.connect('musicten.db')
c = conn.cursor()
c.execute('''
INSERT INTO singer (Singer_ID, Singer,nationality)
VALUES
(1,'JT Watson',' USA'),
(2,'Rafinha','Brazil'),
(3,'Juan Casa','Spain'),
(4,'Kidi','USA'),
(5,'Dede','USA')
''')
c.execute('''
INSERT INTO music (Song_ID,Singer_ID, name, Date,Edition)
VALUES
(1,1,'LA',01/05/2009,1),
(2,2,'Second',13/07/2009,1),
(3,1,'Mexico',13/07/2009,1),
(4,3,'Let's go',13/09/2009,1),
(5,tuple([4,5]),'Hello',18/09/2009,1)
''')
conn.commit()
But this code seems not work to insert values to the dataframe.
SO my goal is to INSERT VALUES to the Table that the database has two tables with values.
First, do not import sqlite3 the second time. Also, you still have an open connection.
Two issues with the SQL:
'Let''s go' (single quote character must be doubled/escaped
tuple([4,5]) => '(4,5)'
I have to do some work with a SQL Database. I have done a bit of research on with video but I got very confused
Could you please explain to me how I insert python lists index into a SQL database?
I'm trying to make the Location a foreign key from its own table. This will allow for easier access in the future so that I can call all the cities from one country with a WHERE statement. I'm doing this so that I can scale this in the future to something else but I want to know how to do it with this example.
LocationID - United Kingdom = 1 France = 2 Denmark = 3 Germany = 4
Place:
London + LocationID1
This is how I want it to look.
This is what I have so far:
import sqlite3
Location = ["United Kingdom","Germany","France","Denmark"]
Places = ["London","Tottenham","Munich","Paris","Frankfurt"]
conn = sqlite3.connect("Location.db")
c = conn.cursor()
c.execute("INSERT INTO Places Location[0])
conn.commit()
conn.close()
This returns an error for me
I don't know if I need to provide the database file. Just ask me if you need it to assist me
Hi Below is the code what i am trying to do the connection string is fine , when i run this No Error pops out and neither it updating the DB
Can you Please help me identify the error.
and i have Two sheet inside that excel data is same just one more column in second sheet name as gender
sample Dataframe which i want to insert into DB is
NAME AGE
0 x 21
1 y 22
2 z 23
3 m 24
4 t 35
import cx_Oracle
from sqlalchemy import create_engine
engine = Create_engine('oracle+cx_oracle://<Connection Detail>')
DB_Connection = cx_Oracle.connect(<Connection Detail)
mycursor = DB_Connection.cursor()
sample_excel = pd.ExcelFile(r<file Path>)
sample_excel_sheet = sample_excel.sheet_names
for i in sample_excel_sheet:
sheet_data = pd.read_excel(r<file Path>,sheet_name=i)
sheet_namee = i.upper()
with engine.connect() as connection:
sheet_data.to_sql(name='sheet_namee',con=engine, if_exists='replace')
DB_Connection.commit()
You only need to change
name='sheet_namee'
with
name=sheet_namee
since sheet_namee = i.upper() already returns the sheet name with inverted commas.
I'm using Python and SQLite to manipulate a database.
I have a SQLite table Movies in database Data that looks like this:
| ID | Country
+----------------+-------------
| 1 | USA, Germany, Mexico
| 2 | Brazil, Peru
| 3 | Peru
I have a table Countries in the same database that looks like this
| ID | Country
+----------------+-------------
| 1 | USA
| 1 | Germany
| 1 | Mexico
| 2 | Brazil
| 2 | Peru
| 3 | Peru
I want to insert from database Data all movies from Peru into a new database PeruData that looks like this
| ID | Country
+----------------+-------------
| 2 | Peru
| 3 | Peru
I'm new to SQL and having trouble programming the right query.
Here's my attempt:
con = sqlite3.connect("PeruData.db")
cur = con.cursor()
cur.execute("CREATE TABLE Movies (ID, Country);")
cur.execute("ATTACH DATABASE 'Data.db' AS other;")
cur.execute("\
INSERT INTO Movies \
(ID, Country) \
SELECT ID, Country
FROM other.Movies CROSS JOIN other.Countries\
WHERE other.Movies.ID = other.Countries.ID AND other.Countries.Country = 'Peru'\
con.commit()
con.close()
Clearly, I'm doing something wrong because I get the error
sqlite3.OperationalError: no such table: other.Countries
Here's a workaround which successfully got the result you wanted.
Instead of having to write con = sqlite3.connect("data.db") and then having to write con.commit() and con.close(), you can shorten your code to written like this:
with sqlite3.connect("Data.db") as connection:
c = connection.cursor()
This way you won't have to commit the changes and close each time you're working with a database. Just a nifty shortcut I learned. Now onto your code...
Personally, I'm unfamiliar with the SQL statement ATTACH DATABASE. I would incorporate your new database at the end of your program instead that way you can avoid any conflicts that you aren't knowledgeable of handling (such as your OperationalError given). So first I would begin to get the desired result and then insert that into your new table. Your third execution statement can be rewritten like so:
c.execute("""SELECT DISTINCT Movies.ID, Countries.Country
FROM Movies
CROSS JOIN Countries
WHERE Movies.ID = Countries.ID AND Countries.Country = 'Peru'
""")
This does the job, but you need to use fetchall() to return your result set in a list of tuples which can then be inserted into your new table. So you'd type this:
rows = c.fetchall()
Now you can open a new connection by creating the "PeruData.db" database, creating the table, and inserting the values.
with sqlite3.connect("PeruData.db") as connection:
c = connection.cursor()
c.execute("CREATE TABLE Movies (ID INT, Country TEXT)")
c.executemany("INSERT INTO Movies VALUES(?, ?)", rows)
That's it.
Hope I was able to answer your question!
The current error is probably caused by a typo or other minor problem. I could create the databases described here and successfully do the insert after fixing minor errors: a missing backslash at an end of line and adding qualifiers for the selected columns.
But I also advise your to use aliases for the tables in multi-table selects. The code that works in my test is:
cur.execute("\
INSERT INTO Movies \
(ID, Country) \
SELECT m.ID, c.Country\
FROM other.Movies m CROSS JOIN other.Countries c \
WHERE m.ID = c.ID AND c.Country = 'Peru'")
I have a SQLite database created with SQLAlchemy which has the format below
Code Names Amt1 Amt2 Amt3 Amt4
502 Real Management 11.4 1.3 - -
5TG 80Commercial 85.8 152 4.845 4.12%
AZG Equipment Love 11.6 117.1 - -
But when I tried to read this into a pandas dataframe by using
pandas.read_sql('sqlite_table', con=engine)
It returns me with an error ValueError: could not convert string to float: '-'
I get that pandas can't register - in the Dataframe, but how can I work around this? Is it possible to read it - as 0 or something??
Update all rows in Amt3 with - (if you have set up your login auths and defined cursor) will be something like this:
cur.execute("UPDATE sqlite_table SET Amt3 = 0 WHERE Amt3 = '-'")
This seems to work fine for me, even with -, what is the type of your Atm3?
import pandas as pd
import sqlite3
con = sqlite3.connect(r"/Users/hugohonorem/Desktop/mytable.db") #create database
conn = sqlite3.connect('mytable.db') #connect to database
c = con.cursor() #set
c.execute('''CREATE TABLE mytable
(Code text, Names text, Atm1 integer, Atm2 integer, Atm3 integer, Atm4 integer)''')
c.execute("INSERT INTO mytable VALUES ('502', 'Real Management', '11.4', '1.3', '-' , '-')")
conn.commit()
So we have replicated your table, we can now remove your -
c.execute("UPDATE mytable SET Atm3 = NULL WHERE Atm3 = '-'") #set it to null
df = pd.read_sql("SELECT * from mytable", con)
print df
This will give us the output:
Code Names Atm1 Atm2 Atm3 Atm4
502 Real Management 11.4 1.3 None -
However, as you can see I can retrieve the table with Atm4 being -
I know this question is old but I ran into the same problem recently and this was the first result on google. What solved my problem was just to change pandas.read_sql('sqlite_table', con=engine) to the full query pandas.read_sql('SELECT * FROM sqlite_table', con=engine) as it seems to bypass the conversion attempt and it wasn't needed to edit the table.