![]() |
|
[Tut] How to Create a Database Table with pyodbc in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Create a Database Table with pyodbc in Python (/thread-96528.html) |
[Tut] How to Create a Database Table with pyodbc in Python - xSicKxBot - 08-03-2020 How to Create a Database Table with pyodbc in Python In this article, you will get the necessary information on how to create a table in SQLite using the package pyodbc in Python. Example code is included and a link to a Github repo will be provided in the references section. Prerequisites To use Code: pyodbc
Column | Data Type | Constraint | Default Value PersonId | Integer | PRIMARY KEY | FirstName | Text | NOT NULL | LastName | Text | NOT NULL | Age | Ineger | NULL | CreatedAt | Text | NOT NULL | Now Here is an example of why you need to know the Data Definition Language of your database. The following information was taken fromhttps://www.sqlite.org/datatype3.html “SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-inDate And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
Applications can choose to store dates and times in any of these formats and freely convert between formats using the built-indate and time functions.” SQLite Create Table Data Definition Language for the PeopleInfo table: Code: 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 );Let’s dive into the steps to create a table in SQLite using pyodbc in Python. Step 1: Install the pyodbc Package Install thePyodbc package using the following command: Code: pip install pyodbcFor Anaconda use the following command: Code: conda install -c anaconda pyodbcNext, you will need to connect your script to SQLite. You may use this template to perform the connection: Remember to import the Code: pyodbcCode: import pyodbc
conn = pyodbc.connect('Driver={SQLite3 ODBC Driver};' 'Server=server_name;' 'Database=database_name;' 'Trusted_Connection=yes;')The cursor object created by Code: cursor()Code: cursor = conn.cursor()Now you will be able to create your table in SQLite For our example, here is the code that I used to create the table in SQL Server using Python: Code: 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 ); ''')Code: conn.commit()Committing the transaction is an important topic to discuss. The statement above explicitly commits the transaction. If you do not commit the transaction, the database will not create the table. The transaction will be rolled back. Other options to consider are: 1 – Add the autocommit parameter to connect. This will not require a manual commit. For example: Code: con = pyodbc.connect(your_connection_string, autocommit = True)2 – Use a Code: withCode: withCode: with pyodbc.connect(your_connection_string) as con: CREATE_TABLE_CODEStep 6: Insert Records to Verify Your Table is Configured Correctly. Code: cursor.execute(''' INSERT INTO PeopleInfo (PersonId, FirstName, LastName, Age) VALUES (1,'Bob','Smith', 55), (2, 'Jenny','Smith', 66) ''')
conn.commit()Code: cursor.execute('SELECT * FROM PeopleInfo')
for row in cursor: print(row)Code: conn.close()Database connections are an expensive resource and there might be limited connections available to your database. Remember to close your connection explicitly if you are not using a “with“ block as explained in Step 5. Complete code: Code: import pyodbc
conn = pyodbc.connect('Driver={SQLite3 ODBC Driver};' 'Server=localhost;' 'Database=testDB.db;' 'Trusted_Connection=yes;')
cursor = conn.cursor()
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 ); ''')
conn.commit() cursor.execute(''' INSERT INTO PeopleInfo (PersonId, FirstName, LastName, Age) VALUES (1,'Bob','Smith', 55), (2, 'Jenny','Smith', 66) ''')
conn.commit() cursor.execute('SELECT * FROM PeopleInfo ')
for row in cursor: print(row) conn.close()Output: Code: (1, 'Bob', 'Smith', 55, '2020-08-01 20:37:04')
(2, 'Jenny', 'Smith', 66, '2020-08-01 20:37:04')1 – Pyodbc Github repo: https://github.com/mkleehammer/pyodbc 2 – Pyodbc Getting Started: https://code.google.com/archive/p/pyodbc/wikis/GettingStarted.wiki 3 – Create tables with SQLite: https://www.sqlitetutorial.net/sqlite-create-table/ |