Getting started
SQLite is a common SQL database engine. In this blog, I will go over how you can connect and query a SQLite database in Python.
Let’s import the necessary libraries for this example, and establish a connection to a database. We will be using the sqlite3 library and pandas.
import sqlite3
import pandas as pd
conn = sqlite3.connect('supermarket.db') # connect to a supermarket database
To execute SQL commands, we need to create a cursor object. Let’s use the cursor to create a table called shops which will contain basic information about our supermarket stores (shop ID, size, postcode) . The store sizes are small, medium and large.
cursor = conn.cursor() # create a cursor
cursor.execute("CREATE TABLE shops(shopid VARCHAR(100) PRIMARY KEY, Store_size VARCHAR(30), Postcode VARCHAR(8))")
So, cursor.execute() executes SQL commands.
We can insert data into the table using the INSERT command. If we do this, use conn.commit() to save the data to the table.
However, in this example, we have a pandas DataFrame called shops_df that we wish to insert into our SQL table called shops. It has the same columns as our shops table. If you’re more familiar with pandas than SQL like me, I will use pandas to clean data first. Only after this will I want to store the data in an SQL database.
The to_sql() function allows us to write records in a DataFrame to an SQL database.
shops_df.to_sql('shops', conn, if_exists='append', index=False) # write shops_df to shops table
Queries
The cursor behaves like an iterator object. We can use a for loop to retrieve all rows from a SELECT statement. To make it easier to query in future, I’ll create a query function.
def query(q):
try:
for row in cursor.execute(q):
print(row)
except Exception as e:
print("Something went wrong... ", e)
query('SELECT * FROM shops')
We can also save the results of a query by assigning it to a variable. The variable will be a list of tuples. A tuple for each row.
q="SELECT shopid, Postcode FROM shops"
query_results = cursor.execute(q).fetchall() # save results as a list of tuples
Storing results from a query like this can allow you to represent table data with graphs. For example, if you had a table called sales in your database which has the shop profits, you could plot trends in sales. I will illustrate this now.

Let’s query the minimum, mean and maximum yearly sales…
c="""
SELECT shops.Store_size, sales.Year, MIN(sales.Sales), AVG(sales.Sales), MAX(sales.Sales)
FROM shops
JOIN sales
ON shops.shopid = sales.shopid
GROUP BY shops.Store_size, sales.Year
"""
query(c)
result = cursor.execute(c).fetchall()

…next I plot the mean sales for medium sized stores against the year…
years=[] # store the year
means = [] # store the mean sales
size = 'medium'
for results in result:
if results[0] == size:
means.append(results[3])
years.append(results[1])
plt.plot(years, means, linestyle = 'dashed', marker = 'o', ms= '5')
plt.title(f"{size} store mean sales")
plt.xlabel("year")
plt.ylabel("sales")
plt.savefig(f"{size}_store_mean_sales.png")
plt.show()

Now we are done, close the connection to the database.
conn.close() # don't forget to close the connection afterwards
I hope this blog was helpful for getting started with the sqlite3 library, to connect to a database, and use a cursor to execute SQL commands in python.
Spent some time on 8kbet456. Good selection of games, and withdrawals were pretty smooth. No complaints from me so far. Give it a shot: 8kbet456
Heard some good things about fc88vn, so I gave it a try. Layout’s not the best, but the games are solid. See for yourself: fc88vn
73betapp? I’ve been using it on my phone. Easy to navigate and haven’t had any crashes yet. Check it out if you’re looking for a mobile option: 73betapp
Very stable platform with a lot of exciting options. I feel safe depositing my funds here. Check out plataformapgwin for a secure bet.
Love the local vibe of this site. It feels tailored for us and the customer support is actually helpful. Glad I found phauronplay.
No more endless loading screens. The login is instant and the connection is stable even on my mobile data. A very seamless gaming experience at jili588casinologin
Fast registration and even faster withdrawals. It is refreshing to find a site that does not make you jump through hoops. Great job 288jililogin.
Great vibes and very fair odds. It is hard to find a site this honest these days. I’m sticking with bd8999vet from now on.
Found this site through a friend and it is legit. The bonuses are actually usable and not just for show. Give bdl8992 a try.
I had a great run yesterday and won big! The graphics are sleek and the gameplay is fair. I will definitely be coming back to korwin39
Really loving the interface here. The loading speed is fast and the games are super smooth. Definitely my new favorite spot to hang out. Check out dreamjili2login
The customer support is actually helpful and responsive which is rare these days. I felt safe depositing my money and the games are genuinely exciting. Love jiliuucasino!
If you are looking for a win, this is the place to be. Everything is transparent and the games are fair. Join solusiwinlogin today.
Found this site through a friend and I am glad I did. The customer support is very responsive and the winning streaks are real. Check out bdgv999 if you want a great experience.
Finally a site that doesn’t lag during peak hours. The login process is quick and everything just works perfectly on my phone. I always use jili30login for my daily gaming sessions.
Had a lucky streak here last night! The site is stable and the games load instantly. If you want a fun time, pkcr777 is the way to go.
This place has a great community and the support team is actually helpful. I had an issue with my account and it was fixed in minutes via ph1xjili.
I felt lucky the moment I joined. The odds are great and the community is very welcoming. Definitely my new favorite spot pkluckybet7
Finally an app that does not crash every five minutes. The login process is seamless and the graphics are top notch. Love how convenient it is to play on the go via evojililoginapp.
Quick installation and no lag at all. It is definitely the best way to get the game running on my phone. Visit pak33gamedownload now.
A hidden gem for gamers. The selection of apps is fantastic and everything works as advertised. Check out in7apkallapp for the best tools.
Quick and secure access every time. I appreciate the stability of this portal. For the fastest entry go with bosku777login.
The winning streaks here are insane. I tried a few others but this one feels way more rewarding. Just log into mahagacor77login and see for yourself.
Just stumbled upon this site and I am already hooked. The speed is amazing and the vibes are just right for a gamer. vn47tg88
I appreciate how secure this site feels. My transactions are always processed quickly without any hassle. Big thanks to 666jililogin
The app version is a game changer. I can play on the go without any hassle and the loading times are super quick. Give sg7772app a try if you like convenience.
I’ve tried a few different sites but this one actually feels fair and exciting. The payouts are decent and the games are addictive. Love m555game.
Had a great run tonight! The bonuses are legit and it is so easy to get started. If you are looking for some action, this is the place. lucky22login
Best experience I’ve had in a while. The customer support is helpful and the bonuses are actually worth it. Give phokjili a shot and see for yourself.
I’ve tried a few sites lately but this one really stands out. The games load fast and the payouts are legit. Highly recommend checking out fk77casino for a great experience.
I appreciate how transparent this platform is with their rules. No hidden tricks, just pure fun and fair play. Big thumbs up for vnd188.
Just what I was looking for. The stability of the app is impressive and everything runs just as it should. Keep it up bdck4444aps
Quick installation and the app is very light on memory. Everything is optimized for a great experience. Get it now at luky888download.
Really solid platform with a great community feel. Everything feels very transparent and fair. Cheers to brwinbrl for the awesome setup.
Easy to navigate and the odds are very competitive. If you are into sports and slots then autobolalogin is the way to go.