{"id":290,"date":"2022-02-21T12:23:33","date_gmt":"2022-02-21T12:23:33","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/data-science\/?p=290"},"modified":"2022-02-21T12:23:33","modified_gmt":"2022-02-21T12:23:33","slug":"sqlalchemy-tutorial-1-connect-python-to-mysql-postgresql-sqlite-and-ms-sql","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/data-science\/sqlalchemy-tutorial-1-connect-python-to-mysql-postgresql-sqlite-and-ms-sql\/","title":{"rendered":"SQLAlchemy Tutorial 1 &#8211; Connect Python to MySQL, PostgreSQL, SQLite and MS SQL"},"content":{"rendered":"<p>In this tutorial, you will learn how to connect Python code to various SQL database servers including: SQLite, PostgreSQL,\u00a0MS SQL,\u00a0MySQL.<\/p>\n<ol>\n<li><a href=\"#t1\">Connect Python to SQLite Database<\/a><\/li>\n<li><a href=\"#t2\">Connect Python to MySQL Database<\/a><\/li>\n<li><a href=\"#t3\">Connect Python to PostgreSQL Database<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Connect Python to SQLite Database<\/strong><\/h4>\n<p>Let&#8217;s connect Python to a database in memory. Actually, we&#8217;ll create an in-memory database. Then we load this database using data imported from a CSV file.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Import the neccessary modules<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sqlalchemy<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> create_engine <span style=\"color: #008800; font-weight: bold;\">as<\/span> ce\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">pandas<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">pd<\/span>\r\n\r\n<span style=\"color: #888888;\"># Create the db engine for file database<\/span>\r\nfile_db <span style=\"color: #333333;\">=<\/span> ce(<span style=\"background-color: #fff0f0;\">'sqlite:\/\/\/datasets\/tutorial.db'<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Create the db engine for in-memory database<\/span>\r\nmemory_db <span style=\"color: #333333;\">=<\/span> ce(<span style=\"background-color: #fff0f0;\">'sqlite:\/\/\/:memory:'<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Import some data from a CSV file<\/span>\r\ndata <span style=\"color: #333333;\">=<\/span> pd<span style=\"color: #333333;\">.<\/span>read_excel(<span style=\"background-color: #fff0f0;\">\"datasets\/Telescope_data.xlsx\"<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Load the imported CSV into your database<\/span>\r\ndata<span style=\"color: #333333;\">.<\/span>to_sql(<span style=\"background-color: #fff0f0;\">'telescope_table'<\/span>, file_db)\r\n<\/pre>\n<p>I recommend you execute each part of the code separately.<\/p>\n<p>Once this code executes, the database would be available in the datasets directory with the name tutorial.db. To view this SQLite database you will need a tool like DB Browser for SQL Lite\u00a0 or DBeaver. You can download them from the links below<\/p>\n<ul>\n<li><a href=\"https:\/\/sqlitebrowser.org\/dl\/\" target=\"_blank\" rel=\"noopener\">Download DB Browser<\/a><\/li>\n<li><a href=\"https:\/\/dbeaver.io\/download\/\" target=\"_blank\" rel=\"noopener\">Download DBeaver<\/a><\/li>\n<\/ul>\n<p>Please watch the video for\u00a0 a detailed procedure.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Connect Python to My SQL Database<\/strong><\/h4>\n<p>You need to have <a href=\"https:\/\/www.mysql.com\/downloads\/\" target=\"_blank\" rel=\"noopener\">MySQL<\/a> Installed. You may also need <a href=\"https:\/\/dev.mysql.com\/downloads\/workbench\/\" target=\"_blank\" rel=\"noopener\">MySQL Workbench<\/a>.<\/p>\n<p>Let&#8217;s now connect to Python to MySQL Database. We would also use the same imports as with SQLite. Follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create an empty database in MySQL. You can either use the command line or the Workbench GUI (See the video for details). The command-line syntax is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">create database datasciencedb;\r\n<\/pre>\n<p><strong>Step 2<\/strong> &#8211; Use the code below to connect to MySQL and load the empty database with data.<\/p>\n<p><em><strong>Note<\/strong><\/em>: You&#8217;ll need to have <em>mysqlclient<\/em> installed using <em>pip<\/em> or <em>conda<\/em>.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Connect to MySQL<\/span>\r\nmysql_engine <span style=\"color: #333333;\">=<\/span> ce(<span style=\"background-color: #fff0f0;\">\"mysql:\/\/root:rootuser@localhost:3306\/datasciencedb\"<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Import some data from a CSV file<\/span>\r\ndata <span style=\"color: #333333;\">=<\/span> pd<span style=\"color: #333333;\">.<\/span>read_excel(<span style=\"background-color: #fff0f0;\">\"datasets\/Telescope_data.xlsx\"<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Load the imported CSV into your database<\/span>\r\ndata<span style=\"color: #333333;\">.<\/span>to_sql(<span style=\"background-color: #fff0f0;\">'telescope_table'<\/span>, mysql_engine)\r\n<\/pre>\n<p>You can not check the database to see that the table is created<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Connect Python to PostgreSQL and MS SQL<\/strong><\/h4>\n<p>For this, you need to have <a href=\"https:\/\/www.postgresql.org\/download\/\" target=\"_blank\" rel=\"noopener\">PostgreSQL<\/a> installed in your system.<\/p>\n<p><strong>Note<\/strong>: if you encounter missing modules error while running these commands, then install them! Also note that the database should exist. So you need to create empty database in PostgreSQL in advance.<\/p>\n<p>In case of PostgreSQL, you need to use the code below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Connect to PosgreSQL<\/span>\r\npg_engine <span style=\"color: #333333;\">=<\/span> ce(<span style=\"background-color: #fff0f0;\">\"postgresql:\/\/postgres:password@localhost:5432\/postgres\"<\/span>)\r\n<\/pre>\n<p>For MS SQL, you&#8217;ll use the code:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Connect using pyodbc<\/span>\r\nengine <span style=\"color: #333333;\">=<\/span> create_engine(<span style=\"background-color: #fff0f0;\">'mssql+pyodbc:\/\/scott:tiger@mydsn'<\/span>)\r\n\r\n<span style=\"color: #888888;\"># Connect using pymssql<\/span>\r\nengine <span style=\"color: #333333;\">=<\/span> create_engine(<span style=\"background-color: #fff0f0;\">'mssql+pymssql:\/\/scott:tiger@hostname:port\/dbname'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to connect Python code to various SQL database servers including: SQLite, PostgreSQL,\u00a0MS SQL,\u00a0MySQL. Connect Python to SQLite Database &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,72,43],"tags":[71,73,74,70,69,75],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-data-science","category-databases","category-python","tag-database","tag-ms-sql","tag-mysql","tag-sql","tag-sqlalchemy","tag-sqlite"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/posts\/290\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/data-science\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}