{"id":116208,"date":"2020-08-02T10:30:45","date_gmt":"2020-08-02T10:30:45","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11636"},"modified":"2020-08-02T10:30:45","modified_gmt":"2020-08-02T10:30:45","slug":"how-to-create-a-database-table-with-pyodbc-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/08\/02\/how-to-create-a-database-table-with-pyodbc-in-python\/","title":{"rendered":"How to Create a Database Table with pyodbc in Python"},"content":{"rendered":"<p>In this article, you will get the necessary information on how to <strong>create a table in SQLite<\/strong> using the package pyodbc in Python.\u00a0 Example code is included and a link to a Github repo will be provided in the references section.<\/p>\n<h2>Prerequisites<\/h2>\n<p>To use <code><a href=\"https:\/\/pypi.org\/project\/pyodbc\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/pypi.org\/project\/pyodbc\/\">pyodbc<\/a><\/code> in this article some prerequisites need to be met.<\/p>\n<ol>\n<li><strong>ODBC Driver<\/strong> \u2013 you need to download and install the ODBC driver for the database provider you are using. In this article, I am using SQLite.\u00a0 Downloaded an ODCB driver for sqlite3 from<a href=\"http:\/\/www.ch-werner.de\/sqliteodbc\/\"> http:\/\/www.ch-werner.de\/sqliteodbc\/<\/a>,<\/li>\n<li><strong>SQLite3 <\/strong>&#8211; this article assumes that you have SQLite3 in your system and have created a database called <code>testDB.db<\/code>.<\/li>\n<li><strong>Database DDL<\/strong> \u2013 different database providers use different data definition languages.\u00a0 DDL is the language needed to create tables in a database.\u00a0 You must be familiar with this syntax to successfully create your table.\u00a0 Creating tables in SQLite can be found here:<a href=\"https:\/\/sqlite.org\/lang_createtable.html\"> https:\/\/sqlite.org\/lang_createtable.html<\/a><\/li>\n<li><strong>Design your table<\/strong> \u2013 This is the table that will be implemented in the example below:<\/li>\n<\/ol>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td>Column<\/td>\n<td>Data Type<\/td>\n<td>Constraint<\/td>\n<td>Default Value<\/td>\n<\/tr>\n<tr>\n<td>PersonId<\/td>\n<td>Integer<\/td>\n<td>PRIMARY KEY<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>FirstName<\/td>\n<td>Text<\/td>\n<td>NOT NULL<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>LastName<\/td>\n<td>Text<\/td>\n<td>NOT NULL<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>Age<\/td>\n<td>Ineger<\/td>\n<td>NULL<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>CreatedAt<\/td>\n<td>Text<\/td>\n<td>NOT NULL<\/td>\n<td>Now<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Here is an example of why you need to know the <strong><em>Data Definition Language<\/em><\/strong> of your database.\u00a0 The following information was taken from<a href=\"https:\/\/www.sqlite.org\/datatype3.html\"> https:\/\/www.sqlite.org\/datatype3.html<\/a><\/p>\n<hr class=\"wp-block-separator\"\/>\n<p>\u201cSQLite does not have a storage class set aside for storing dates and\/or times. Instead, the built-in<a href=\"https:\/\/www.sqlite.org\/lang_datefunc.html\"> Date And Time Functions<\/a> of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:<\/p>\n<ul>\n<li><strong>TEXT<\/strong> as ISO8601 strings (<code>\"YYYY-MM-DD HH:MM:SS.SSS\"<\/code>).<\/li>\n<li><strong>REAL<\/strong> as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.<\/li>\n<li><strong>INTEGER<\/strong> as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.<\/li>\n<\/ul>\n<p>Applications can choose to store dates and times in any of these formats and freely convert between formats using the built-in<a href=\"https:\/\/www.sqlite.org\/lang_datefunc.html\"> date and time functions<\/a>.\u201d<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p>SQLite Create Table Data Definition Language for the PeopleInfo table:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE PeopleInfo ( PersonId INTEGER PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, Age INTEGER NULL, CreatedAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL );\n<\/pre>\n<p>Let&#8217;s dive into the steps to create a table in SQLite using pyodbc in Python.<\/p>\n<h2>Step 1: Install the pyodbc Package<\/h2>\n<p>Install the<a href=\"https:\/\/github.com\/mkleehammer\/pyodbc\/wiki\"> Pyodbc<\/a> package using the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install pyodbc<\/pre>\n<p>For Anaconda use the following command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">conda install -c anaconda pyodbc<\/pre>\n<h2>Step 2: Connect Your Python Script to SQLite<\/h2>\n<p>Next, you will need to connect your script to SQLite.<\/p>\n<p>You may use this template to perform the connection:<\/p>\n<p>Remember to import the <code>pyodbc<\/code> package first. Connect to the database using the connect method with the ODBC connection string for SQLite.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\nconn = pyodbc.connect('Driver={SQLite3 ODBC Driver};' 'Server=server_name;' 'Database=database_name;' 'Trusted_Connection=yes;')\n<\/pre>\n<h2>Step 3: Create Cursor from Your Connection<\/h2>\n<p>The cursor object created by <code>cursor()<\/code> allows you to execute queries.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor = conn.cursor()<\/pre>\n<h2>Step 4: Create the Table in SQLite<\/h2>\n<p>Now you will be able to create your table in SQLite<\/p>\n<p>For our example, here is the code that I used to create the table in SQL Server using Python:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor.execute(''' CREATE TABLE PeopleInfo ( PersonId INTEGER PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, Age INTEGER NULL, CreatedAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL ); ''')\n<\/pre>\n<h2>Step 5: Commit the Transaction<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">conn.commit()<\/pre>\n<p>Committing the transaction is an important topic to discuss.&nbsp; The statement above explicitly commits the transaction.&nbsp; If you do not commit the transaction, the database will not create the table.&nbsp; The transaction will be rolled back.<\/p>\n<p>&nbsp;Other options to consider are:<\/p>\n<p>&nbsp;1 &#8211; Add the autocommit parameter to connect.&nbsp; This will not require a manual commit.&nbsp; For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">con = pyodbc.connect(your_connection_string, autocommit = True)<\/pre>\n<p>2 \u2013 Use a <code><a href=\"https:\/\/blog.finxter.com\/python-one-line-with-statement\/\" title=\"Python One Line With Statement\">with<\/a><\/code> block and anything will be committed before the connection is terminated at the end of the <code>with<\/code> block.\u00a0 For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">with pyodbc.connect(your_connection_string) as con: CREATE_TABLE_CODE<\/pre>\n<\/p>\n<h2>Step 6: Insert Records to Verify Your Table is Configured Correctly.<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor.execute(''' INSERT INTO PeopleInfo (PersonId, FirstName, LastName, Age) VALUES (1,'Bob','Smith', 55), (2, 'Jenny','Smith', 66) ''')\nconn.commit()\n<\/pre>\n<h2>Step 7: Run a SELECT Query to Retrieve the Records.<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cursor.execute('SELECT * FROM PeopleInfo')\nfor row in cursor: print(row)\n<\/pre>\n<h2>Step 8: Close Your Connection If It Isn&#8217;t Needed.<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">conn.close()<\/pre>\n<p>Database connections are an expensive resource and there might be limited connections available to your database.&nbsp; Remember to close your connection explicitly if you are not using a \u201cwith\u201c block as explained in Step 5.<\/p>\n<p><strong>Complete code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\nconn = pyodbc.connect('Driver={SQLite3 ODBC Driver};' 'Server=localhost;' 'Database=testDB.db;' 'Trusted_Connection=yes;')\ncursor = conn.cursor()\ncursor.execute(''' CREATE TABLE PeopleInfo ( PersonId INTEGER PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, Age INTEGER NULL, CreatedAt TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL ); ''')\nconn.commit() cursor.execute(''' INSERT INTO PeopleInfo (PersonId, FirstName, LastName, Age) VALUES (1,'Bob','Smith', 55), (2, 'Jenny','Smith', 66) ''')\nconn.commit() cursor.execute('SELECT * FROM PeopleInfo ')\nfor row in cursor: print(row) conn.close()\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(1, 'Bob', 'Smith', 55, '2020-08-01 20:37:04')\n(2, 'Jenny', 'Smith', 66, '2020-08-01 20:37:04')<\/pre>\n<h2>References<\/h2>\n<p>1 &#8211; Pyodbc Github repo: <a href=\"https:\/\/github.com\/mkleehammer\/pyodbc\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/mkleehammer\/pyodbc<\/a><\/p>\n<p>2 &#8211; Pyodbc Getting Started: <a href=\"https:\/\/code.google.com\/archive\/p\/pyodbc\/wikis\/GettingStarted.wiki\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/code.google.com\/archive\/p\/pyodbc\/wikis\/GettingStarted.wiki<\/a><\/p>\n<p>3 &#8211; Create tables with SQLite: <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-create-table\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.sqlitetutorial.net\/sqlite-create-table\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will get the necessary information on how to create a table in SQLite using the package pyodbc in Python.\u00a0 Example code is included and a link to a Github repo will be provided in the references section. Prerequisites To use pyodbc in this article some prerequisites need to be met. ODBC [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-116208","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=116208"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116208\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=116208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=116208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=116208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}