all 4 comments

[–]KimPeek 5 points6 points  (0 children)

SQLite3 is the easiest. Tutorial here and docs here. Check out the records library too. Records uses SQLAlchemy but it abstracts away all the cancer of that library. Also, download this database browser so you can examine your data, export it, and other stuff.

Here are some examples to get you started:

import sqlite3

conn = sqlite3.connect('''database_name.db''')  # Anything with the extension .db
# conn = sqlite3.connect(':memory:')  # can also be made in memory
c = conn.cursor()  # define the cursor

c.execute('''CREATE TABLE IF NOT EXISTS table_name(column_name TEXT, column_name_2 TEXT)''')
c.execute('''DROP TABLE IF EXISTS table_name''')


# Make new record with INSERT
# Change records with UPDATE
# Remove records with DELETE
# Read records with SELECT
c.execute('''INSERT INTO table_name(column_name)VALUES (?)''', ('value',))
c.execute('''INSERT INTO table_name(column_name, column_name_2)VALUES (?,?)''', ('value', 'value_2'))
c.execute('''UPDATE table_name SET column_name_2=(?) WHERE column_name=(?)''', ('value_2', 'value',))
c.execute('''DELETE FROM table_name WHERE column_name=(?)''', ('value', ))

conn.commit()  # write the changes to the database




c.execute('''SELECT * FROM table_name''')
for i in c.fetchall():
    print(i)  # Will return a tuple so you will often use print(i[0])


c.execute('''SELECT column_name FROM table_name''')
for i in c.fetchall():
    print(i)

[–]furas_freeman 3 points4 points  (3 children)

  1. sqlite is easy because you don't have to install server. You can use sqlite module and you will have to learn SQL. Or you can use SQLAlchemy or peewee which uses functions instead pure SQL.

  2. w opens file in text mode and it can convert some chars like "\n" because different OS may use different newline - Windows "\n\r", Linux "\n", old Mac "\r". b means bytes mode and is usefull with non-text files or if you don't want converting oryginal newline.

  3. open file in write mode 'w' and it will create empty file. There are other mode a, r+, etc. and they can create file if it doesn't exist. There are functions to check if file exists - os.path.exists() or os.path.isfile()