![]() ![]() # close communication with the PostgreSQL database serverĮxcept (Exception, psycopg2.DatabaseError) as error:Ĭreate_tables() Code language: Python ( python ) 2) Execute the Python program # create table one by one for command in commands: ![]() #!/usr/bin/python import psycopg2ĭef create_tables (): """ create tables in the PostgreSQL database""" The create_tables() function creates four tables in the suppliers database: vendors, parts, vendor_parts, and part_drawings. Second, inside the create_table.py file, define a new function called create_tables(). Finally, close the communication with the PostgreSQL database server by calling the close() methods of the cursor and connection objects.Ĭreating tables in Python example 1) Create a Python programįirst, create a new file called create_table.py.After that, execute the CREATE TABLE by calling the execute() method of the cursor object.Then, create a cursor object by calling the cursor() method of the connection object.The connect() function returns a connection object. Next, connect to the PostgreSQL database by calling the connect() function.First, construct CREATE TABLE statements.To create a new table in a PostgreSQL database, you use the following steps: Steps for creating PostgreSQL tables in Python If this is not the case, you should check out the CREATE TABLE tutorial. This tutorial assumes that you know how to write the CREATE TABLEstatement. Summary: in this tutorial, you will learn how to create new tables in the PostgreSQL database using Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |