Skip to content
← Back to Community
Python SQL
Profile icon
xolyon

Can someone quickly explain SQL to me, how to use it and why or if its better than external files, thanks

IK its database but that's it, I need this for a project

Answered by Codejira [earned 5 cycles]
View Answer
Voters
Profile icon
niveditabhalot
Profile icon
xolyon
Comments
hotnewtop
Profile icon
Codejira

Well, there is no quick way, I guess.
But here are some thoughts:

For many projects SQLite should do the trick. It's even in Python's Standard Libraries (sqlite3)
One advantage: SQLite doesn't require an SQL server.
Disadvantage: It lacks some advanced functionality.

It is in fact a single external file, but you don't need to manage it by yourself.

When you feel the urge to put your data into spreadsheets, SQLite might be the solution.

Very quick intro:
Start with import sqlite3

Create a database file and connect to it: connection = sqlite3.connect('myfirst.db')

Create a cursor object: crs = connection.cursor()

Make the cursor execute some tasks:

crs.execute("CREATE TABLE sometable ... crs.execute("INSERT INTO sometable VALUES ...

Save the changes and close the connection:

connection.commit() connection.close()
Profile icon
xolyon

cool but how can i like take data from a database and does it require and external file? @Codejira

Profile icon
Codejira

The database file is created upon connection, if it doesn't already exist.
A simple SQL query in Python looks like this:

query = crs.execute("select name, address from personal_data").fetchall() for name, address in query: print(name, address)

Did this from memory, maybe not 100% correct
@xolyon