Call SQLite Databases with Python | Python Bits | Kovolff

Опубликовано: 27 Ноябрь 2020
на канале: kovolff
82
0

Step 1: import sqlite3

Step 2: Create variable to hold the database path
i.e. db_path = ‘E:\\demo_sector\\unit_converter.db’

When working with Windows use double backslashes \\ instead of the usual single backslash.\
This is to avoid having the backslash act as an escape character.

Step 3: Create your database connection, connecting the sqlite module to your database.
i.e. database_connection = sqlite.connect(db_path)

Step 4: Set up your database cursor,
i.e. database_cursor = database_connection.cursor(()

This acts similar to the cursor we know from text editors

Step 5: Execute the SQL code you need to get the outputs you require
i.e. database_cursor.execute(‘SELECT * FROM [conversion_factors];’)

Step 6: Create variable to hold the data returned by the database
i.e. found_data = database_cursor.fetchall()

The data returned by the database is a list of tuples.
i.e. [('1', 'length', 'kilometre', 'km', 'kilometre', 'km', 1.0), ('2', 'length', 'metre', 'm', 'kilometre', 'km', 0.001), …]

Each tuple denotes a row of data.

To access any tuple or element in a tuple, use the indices of the found set
i.e. to get the 7th element of each tuple:
for i in range(len(found_data)):
print(found_data[i][6])

#python #sqlite #programming #coding #databases #sql


На этой странице сайта вы можете посмотреть видео онлайн Call SQLite Databases with Python | Python Bits | Kovolff длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь kovolff 27 Ноябрь 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 82 раз и оно понравилось 0 зрителям. Приятного просмотра!