{"id":20975506,"url":"https://github.com/fsoky/sqlite3_lesson","last_synced_at":"2025-12-27T15:57:12.878Z","repository":{"id":138058592,"uuid":"459335948","full_name":"Fsoky/sqlite3_lesson","owner":"Fsoky","description":"Исходный код с ролика: Как работать с базой данных в Python","archived":false,"fork":false,"pushed_at":"2022-02-17T18:49:09.000Z","size":5,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-20T05:44:18.125Z","etag":null,"topics":["python","python-sqlite","python-sqlite3","sqlite","sqlite3"],"latest_commit_sha":null,"homepage":"https://www.youtube.com/watch?v=y0YWRqrhTBY","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Fsoky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-14T21:47:15.000Z","updated_at":"2024-05-31T13:29:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"3445137a-e0d1-4390-a1b6-12e706d7d0f5","html_url":"https://github.com/Fsoky/sqlite3_lesson","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fsoky%2Fsqlite3_lesson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fsoky%2Fsqlite3_lesson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fsoky%2Fsqlite3_lesson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fsoky%2Fsqlite3_lesson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fsoky","download_url":"https://codeload.github.com/Fsoky/sqlite3_lesson/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243374326,"owners_count":20280661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","python-sqlite","python-sqlite3","sqlite","sqlite3"],"created_at":"2024-11-19T04:46:03.761Z","updated_at":"2025-12-27T15:57:12.814Z","avatar_url":"https://github.com/Fsoky.png","language":"Python","readme":"# [SQLite 3 | Как работать с базой данных в Python?](https://youtu.be/y0YWRqrhTBY)\nИсходный код с ролика.\n\n\n### Основные методы для работы с БД\n```py\nimport sqlite3\n\nwith sqlite3.connect(\"thebest.db\") as db:\n\tcursor = db.cursor()\n\n\tcursor.execute(\"\"\"CREATE TABLE IF NOT EXISTS articles(\n\t\tid INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\tauthor VARCHAR,\n\t\ttopic VARCHAR,\n\t\tcontent TEXT\n\t)\"\"\")\n\n\tvalues = [\n\t\t(\"Фипик\", \"Что будет завтра?\", \"Всё будет хорошо, не грусти.\"),\n\t\t(\"Летта\", \"Хорошие новости\", \"У тебя всё получится!\"),\n\t\t(\"Эрвик\", \"Совет дня\", \"Не обращай внимания на негатив!\")\n\t]\n\n\tcursor.executemany(\"INSERT INTO articles(author, topic, content) VALUES(?, ?, ?)\", values)\n\n\t\"\"\"\n\t.fetchone() - Возвращает первую, единственную запись.\n\t.fetchall() - Возвращает список с записями.\n\t.fetchmany(size) - Возвращает список записей с указаным количеством (size).\n\t\"\"\"\n\n\tcursor.execute(\"SELECT * FROM articles\")\n\tprint(cursor.fetchone())\n\n\tcursor.execute(\"\"\"CREATE TABLE IF NOT EXISTS email(\n\t\t`from` VARCHAR,\n\t\t`to` VARCHAR,\n\t\tsubject VARCHAR,\n\t\tcontent TEXT\n\t)\"\"\")\n\n\tcursor.execute(\"INSERT INTO email VALUES('Фсоки', 'Зритель', 'Фантастический денёк', 'Знай, ты самый лучший!')\")\n\n\tfor data in cursor.execute(\"SELECT * FROM email\"):\n\t\tprint(f\"\\nОт: {data[0]}\\nКому: {data[1]}\\nТема: {data[2]}\\nСообщение: {data[3]}\")\n```\n\n### Регистрация/авторизация и казино\n```py\nimport sqlite3\nimport hashlib\nimport random\n\n\ndef md5sum(value):\n\treturn hashlib.md5(value.encode()).hexdigest()\n\n\nwith sqlite3.connect(\"database.db\") as db:\n\tcursor = db.cursor()\n\n\tquery = \"\"\"\n\tCREATE TABLE IF NOT EXISTS users(\n\t\tid INTEGER PRIMARY KEY,\n\t\tname VARCHAR(30),\n\t\tage INTEGER(3),\n\t\tsex INTEGER NOT NULL DEFAULT 1,\n\t\tbalance INTEGER NOT NULL DEFAULT 2000,\n\t\tlogin VARCHAR(15),\n\t\tpassword VARCHAR(20)\n\t);\n\tCREATE TABLE IF NOT EXISTS casino(\n\t\tname VARCHAR(50),\n\t\tdescription TEXT(300),\n\t\tbalance BIGINT NOT NULL DEFAULT 10000\n\t)\n\t\"\"\"\n\n\tcursor.executescript(query)\n\n\ndef registration():\n\tname = input(\"Name: \")\n\tage = int(input(\"Age: \"))\n\tsex = int(input(\"Sex: \"))\n\tlogin = input(\"Login: \")\n\tpassword = input(\"Password: \")\n\n\ttry:\n\t\tdb = sqlite3.connect(\"database.db\")\n\t\tcursor = db.cursor()\n\n\t\tdb.create_function(\"md5\", 1, md5sum)\n\n\t\tcursor.execute(\"SELECT login FROM users WHERE login = ?\", [login])\n\t\tif cursor.fetchone() is None:\n\t\t\tvalues = [name, age, sex, login, password]\n\n\t\t\tcursor.execute(\"INSERT INTO users(name, age, sex, login, password) VALUES(?, ?, ?, ?, md5(?))\", values)\n\t\t\tdb.commit()\n\t\telse:\n\t\t\tprint(\"Такой логин уже существует!\")\n\t\t\tregistration()\n\texcept sqlite3.Error as e:\n\t\tprint(\"Error\", e)\n\tfinally:\n\t\tcursor.close()\n\t\tdb.close()\n\n\ndef log_in():\n\tlogin = input(\"Login: \")\n\tpassword = input(\"Password: \")\n\n\ttry:\n\t\tdb = sqlite3.connect(\"database.db\")\n\t\tcursor = db.cursor()\n\n\t\tdb.create_function(\"md5\", 1, md5sum)\n\n\t\tcursor.execute(\"SELECT login FROM users WHERE login = ?\", [login])\n\t\tif cursor.fetchone() is None:\n\t\t\tprint(\"Такого логина не существует!\")\n\t\telse:\n\t\t\tcursor.execute(\"SELECT password FROM users WHERE login = ? AND password = md5(?)\", [login, password])\n\t\t\tif cursor.fetchone() is None:\n\t\t\t\tprint(\"Пароль неверный!\")\n\t\t\telse:\n\t\t\t\tplay_casino(login)\n\texcept sqlite3.Error as e:\n\t\tprint(\"Error\", e)\n\tfinally:\n\t\tcursor.close()\n\t\tdb.close()\n\n\ndef play_casino(login):\n\tprint(\"\\nCASINO 🥰😳\")\n\n\ttry:\n\t\tdb = sqlite3.connect(\"database.db\")\n\t\tcursor = db.cursor()\n\n\t\tcursor.execute(\"SELECT age FROM users WHERE login = ? AND age \u003e= ?\", [login, 18])\n\t\tif cursor.fetchone() is None:\n\t\t\tprint(\"Вам недостаточно лет!\")\n\t\telse:\n\t\t\tbet = int(input(\"Bet: \"))\n\t\t\tnumber = random.randint(1, 100)\n\n\t\t\tbalance = cursor.execute(\"SELECT balance FROM users WHERE login = ?\", [login]).fetchone()[0]\n\t\t\tif balance \u003c bet:\n\t\t\t\tprint(\"Недостаточно средств, гуляй работать! 😉💖\")\n\t\t\telif balance \u003c= 0:\n\t\t\t\tprint(\"Недостаточно средств, гуляй работать! 😉💖\")\n\t\t\telse:\n\t\t\t\tif number \u003c 50:\n\t\t\t\t\tcursor.execute(\"UPDATE users SET balance = balance - ? WHERE login = ?\", [bet, login])\n\t\t\t\t\tcursor.execute(\"UPDATE casino SET balance = balance + ?\", [bet])\n\n\t\t\t\t\tprint(\"You lose! ❤\")\n\t\t\t\telse:\n\t\t\t\t\tcursor.execute(\"UPDATE users SET balance = balance + ? WHERE login = ?\", [bet, login])\n\t\t\t\t\tcursor.execute(\"UPDATE casino SET balance = balance - ?\", [bet])\n\n\t\t\t\t\tprint(\"You win! 😉😳\")\n\n\t\t\t\tdb.commit()\n\t\t\t\tplay_casino(login)\n\texcept sqlite3.Error as e:\n\t\tprint(\"Error\", e)\n\tfinally:\n\t\tcursor.close()\n\t\tdb.close()\n\n\nregistration()\nlog_in()\n```\n\n### Работа с изображениями в SQLite 3\n```py\nimport sqlite3\nimport io\n\nfrom PIL import Image\n\nwith sqlite3.connect(\"users-avatar.db\") as db:\n\tcursor = db.cursor()\n\tcursor.row_factory = sqlite3.Row\n\n\tcursor.execute(\"CREATE TABLE avatars(photo BLOB)\")\n\n\twith open(\"clover.png\", \"rb\") as file:\n\t\tcursor.execute(\"INSERT INTO avatars VALUES(?)\", [file.read()])\n\n\tdata = cursor.execute(\"SELECT photo FROM avatars\").fetchone()[\"photo\"]\n\timg = Image.open(io.BytesIO(data))\n\timg.show()\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsoky%2Fsqlite3_lesson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffsoky%2Fsqlite3_lesson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffsoky%2Fsqlite3_lesson/lists"}