I imported a csv file into Python and tried using SQLLite. But when I created the table, the columns and values were changed. For example, in the overall column the values "asin" appeared. The reviewerID column received the values "overall" and so on.
How to fix it?
class csvrd(object):
def csvFile(self):
self.readFile('reviews.csv')
def readFile(self, filename):
conn = sqlite3.connect('amazonReviews.db')
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS amazonReviews(reviewerID varchar, asin INT,reviewerName varchar,reviewText varchar, overall INT,summary varchar,unixReviewTime INTEGER,reviewTime INTEGER,helpful INT,total INT)""")
filename.encode('utf-8')
print("Amazon Reviews table executed")
with open(filename) as f:
reader = csv.reader(f)
for field in reader:
cur.execute("INSERT INTO amazonReviews VALUES (?,?,?,?,?,?,?,?,?,?);", field)
print("CSV Loaded into SQLite")
conn.commit()
conn.close()
c = csvrd().csvFile()
con = sqlite3.connect('amazonReviews.db')
pd.read_sql_query("SELECT * FROM amazonReviews LIMIT 5", con)
EXPECTED:
reviewerID asin reviewerName reviewText overall summary unixReviewTime reviewTime helpful total
0 A1EE2E3N7PW666 B000GFDAUG Aaron L. Allen "Orgazmo1009" CA Lewsi' review should be removed. he's revie... 5 Stupid 1202256000 02 6, 2008 0 0
1 AGZ8SM1BGK3CK B000GFDAUG Mind's Clay I truly love the humor of South Park. It's soc... 5 "More Moist Than Should Be" Humor 1198195200 12 21, 2007 1 1
ACTUAL:
asin overall reviewText reviewTime reviewerID reviewerName summary unixReviewTime helpful total
0 A1EE2E3N7PW666 B000GFDAUG Aaron L. Allen "Orgazmo1009" CA Lewsi' review should be removed. he's revie... 5 Stupid 1202256000 02 6, 2008 0 0
1 AGZ8SM1BGK3CK B000GFDAUG Mind's Clay I truly love the humor of South Park. It's soc... 5 "More Moist Than Should Be" Humor 1198195200 12 21, 2007 1 1[enter image description here][1]
This would happen if the table already exists with the columns in the "ACTUAL" result order. Table will not be created because of "IF NOT EXISTS" in the "CREATE" sql. The data will be loaded without "type" complaint because of manifest typing.
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
I wish to count the number of males and females in a specific city that the user chose. Here's a gist of the .db file:
example_table
CODE AGE SEX CITY
---- --- --- ----
E101 25 M New York
E102 42 F New York
E103 31 M Chicago
E104 67 F Chicago
This is what I've coded so far based on the references I've read:
city=input("Input city: ")
import sqlite3
db = sqlite3.connect('covid.db')
cursor = db.cursor()
sql = 'SELECT Sex, COUNT(Code) FROM example_table GROUP BY Sex'
data = cursor.execute(sql).fetchall()
for sex, cases in data:
print(sex, ':', cases)
cursor.close()
So far, that prints the overall number of males and females in the data. I'd like to ask how could I print the exact number of males and females in a city? Say I input "New York", the result would be:
M : 1
F : 1
Add a WHERE clause restricting to a certain city, e.g.
sql = 'SELECT Sex, COUNT(Code) FROM example_table WHERE CITY = ? GROUP BY Sex'
params = ('New York',)
data = cursor.execute(sql, params).fetchall()
for sex, cases in data:
print(sex, ':', cases)
This question is duplicated I think, but I couldn't understand other answers...
Original table looks like this.
NAME AGE SMOKE
John 25 None
Alice 23 None
Ken 26 None
I will update SMOKE column in these rows,
But I need to use output of function which was coded in python check_smoke(). If I input name to check_smoke(), then it returns "Smoke" or "Not Smoke".
So final table would look like below:
NAME AGE SMOKE
John 25 Smoke
Alice 23 Not Smoke
Ken 26 Not Smoke
I'm using sqlite3 and python3.
How can I do it? Thank you for help!
You could use 1 cursor to select rows and another one to update them.
Assuming that the name of the table is smk (replace it by your actual name) and that con is an established connection to the database, you could do:
curs = con.cursor()
curs2 = con.cursor()
batch = 64 # size of a batch of records
curs.execute("SELECT DISTINCT name FROM smk")
while True:
names = curs.fetchmany(batch) # extract a bunch of rows
if len(names) == 0: break
curs2.executemany('UPDATE smk SET smoke=? WHERE name=?', # and update them
[(check_smoke(name[0]), name[0]) for name in names])
con.commit()
I am quite new to SQLITE3 as well as python. I a complete beginner in SQLite. I don't understand much. I am right now learning as a go for my project.I am working on a project where I have one database with about 20 tables inside of it. One table is for user input and the other tables are pre-loaded with values. How can I compare and match which values that are in the pre-loaded table with the user table?? For example:
Users Table:
Barcode: Item:
1234 milk
4321 cheese
5678 butter
8765 water
9876 sugar
Pre-Loaded Table:
Barcode: Availability:
1234 1
5678 1
9876 1
1111 1
Now, I want to be able to compare each row in the Pre-Loaded Table to each row in the Users Table. They both have the Barcode column in common to be able to compare. As a result, during the query process, it should check each row:
1234 - milk - 1 (those columns are equal )
5678 - butter - 1 ( those columns are equal)
9876 - sugar - 1 (those columns are equal)
1100 - - 1 ( this barcode does not exist in the Users Table)
so when a Barcode, in this case, 1100 doesn't exist in the Users Table, the code should print: You don't have all the items for the Pre-Loaded Table. How can I get the code to this?
so far I have this: This code does work by the way.
import sqlite3 as sq
connect = sq.connect('Food_Data.db')
con = connect.cursor()
sql = ("SELECT Users_Food.Barcode, Users_Food.Item, Recipe1.Ham_Swiss_Omelet FROM Users_Food INNER JOIN Recipe1 ON Users_Food.Barcode = Recipe1.Barcode WHERE Recipe1.Ham_Swiss_Omelet = '1'")
con.execute(sql)
data = con.fetchall()
print("You can make: Ham Swiss Omelet")
formatted_row = '{:<10} {:<9} {:>9} '
print(formatted_row.format("Barcode", "Ingredients", "Availability"))
for row in data:
print(formatted_row.format(*row))
#print (row[:])
#connect.commit()
It prints:
You can make: Ham Swiss Omelet
Barcode Ingredients Availability
9130849874 butter 1
2870896881 eggs 1
5501066727 water 1
1765023029 salt 1
9118188735 pepper 1
4087256674 ham 1
3009527296 cheese 1
The SQLite code:
sql = ("SELECT Users_Food.Barcode, Users_Food.Item, Recipe1.Ham_Swiss_Omelet FROM Users_Food INNER JOIN Recipe1 ON Users_Food.Barcode = Recipe1.Barcode WHERE Recipe1.Ham_Swiss_Omelet = '1'")
It combines the two tables with the Barcode in common and and the corresponding food names and availability. However, If one of the barcode values is not present in the Pre-Loaded table, when I compare how can I go about coding to know that it is not there while still displaying what is there in common between those two tables? It is like checking to see if the tables are identical.