![]() execute ( 'CREATE TABLE books (id serial PRIMARY KEY,' 'title varchar (150) NOT NULL,' 'author varchar (50) NOT NULL,' 'pages_num integer NOT NULL,' 'review text,' 'date_added date DEFAULT CURRENT_TIMESTAMP) ' ) # Insert data into the tableĬur. execute ( 'DROP TABLE IF EXISTS books ' )Ĭur. cursor ( ) # Execute a command: this creates a new tableĬur. environ ) # Open a cursor to perform database operationsĬur = conn. Once the installation is successfully finished, you’ll see a line similar to the following at the end of the output: With your virtual environment activated, use pip to install Flask and the psycopg2 library: In this step, you will install Flask and the psycopg2 library so that you can interact with your database using Python. Next, you’ll install this library alongside the Flask package. ![]() Postgres is now set up so that you can connect to and manage its database information via Python using the psycopg2 library. When you are finished, exit out of the PostgreSQL prompt by typing: You’ll see flask_db in the list of databases. To confirm the database was created, get the list of databases by typing the following command: GRANT ALL PRIVILEGES ON DATABASE flask_db TO sammy.Then give this new user access to administer your new database: CREATE USER sammy WITH PASSWORD ' password'.Next, create a database user for our project. Note: Every Postgres statement must end with a semi-colon, so make sure that your command ends with one if you are experiencing issues. You will be given a PostgreSQL prompt where you can set up your requirements.įirst, create a database for your project: Log in to an interactive Postgres session using the following command: You can use sudo and pass in the username with the -iu option. You need to use this user to perform administrative tasks. In this step, you’ll create a database called flask_db and a database user called sammy for your Flask application.ĭuring the Postgres installation, an operating system user named postgres was created to correspond to the postgres PostgreSQL administrative user. Step 1 - Creating the PostgreSQL Database and User ![]() Follow How To Install and Use PostgreSQL on Ubuntu 20.04 to set up your PostgreSQL database. PostgreSQL installed on your local machine, and access to the PostgreSQL prompt. You can review our How To Build a Website with HTML tutorial series for background knowledge. If you are not familiar with Flask, check out How to Create Your First Web Application Using Flask and Python and How to Use Templates in a Flask Application.Īn understanding of basic HTML concepts. In this tutorial the project directory is called flask_app.Īn understanding of basic Flask concepts, such as routes, view functions, and templates. Follow the tutorial for your distribution in How To Install and Set Up a Local Programming Environment for Python 3 series. ![]() PrerequisitesĪ local Python 3 programming environment. You’ll use it with Flask to perform basic tasks, such as connecting to a database server, creating tables, inserting data to a table, and retrieving data from a table. In this tutorial, you’ll build a small book review web application that demonstrates how to use the psycopg2 library, a PostgreSQL database adapter that allows you to interact with your PostgreSQL database in Python. It’s standards-compliant and has many advanced features such as reliable transactions and concurrency without read locks. PostgreSQL, or Postgres, is a relational database management system that provides an implementation of the SQL querying language. For example, you might not want users to add posts with no titles.įlask is a lightweight Python web framework that provides useful tools and features for creating web applications in the Python Language. The actions you perform to manipulate data will depend on specific features in your application. In a web application, these requirements might be a user adding a new post, deleting a post, or deleting their account, which might or might not delete their posts. You can add data to a database, retrieve it, modify it, or delete it, depending on different requirements and conditions. For example, in a social media application, you have a database where user data (personal information, posts, comments, followers) is stored in a way that can be efficiently manipulated. You use a database to store and maintain persistent data that can be retrieved and manipulated efficiently. In web applications, you usually need a database, which is an organized collection of data. The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |