![]() What are some of the things we want to be able to do with the database from Python? We want to be able to view all projects, view all tasks, add a new project, and so on. When we print out those results, we can see both of the projects that we had already manually entered into the database. To access the results from the query, we can use the fetchall() function as we see below. ![]() import nnector as mysqlĬursor.execute("select * from projects") Fetch Records Using Cursor fetchall()Įxecuting the query does not immediately return the results of the query. The connection now gives us access to the cursor(), which is how we can execute SQL queries against the database we are connected to. ![]() Print(db) Use The Cursor To Execute SQL Queries Once it is installed, the code below shows how to use the connect() function to establish a connection to the database. It can be installed using pip install mysql-connector-python. To connect to MySQL using Python, we can use the Python MySql Connector. | 2 | 1 | Prepare row for Lolla Rosa Lettuce | | 1 | 1 | Prepare row for Great Lakes Lettuce | | 2 | Lawn Care | Take care of various lawn tasks | | 1 | Plant Lettuce | Move lettuces seedlings from nursery to garden | Viewing the records from the command line shows that we’re ready to start working with this database from Python. Query OK, 1 row affected (0.004 sec) Viewing Database Records MariaDB > insert into tasks(description, project_id) values ("Mow the Lawn", 2) MariaDB > insert into tasks(description, project_id) values ("Fertilize Lawn", 2) MariaDB > insert into projects(title, description) values ("Lawn Care", "Take care of various lawn tasks") MariaDB > insert into tasks(description, project_id) values ("Prepare row for Lolla Rosa Lettuce", 1) MariaDB > insert into tasks(description, project_id) values ("Prepare row for Great Lakes Lettuce", 1) MariaDB > insert into projects(title, description) values ("Plant Lettuce", "Move lettuces seedlings from nursery to garden") This is how relational database management systems work. Therefore, any time we add new tasks, we must specify the ID of the project that the task is related to. The two tables are related to each other via the foreign key that we configured on the tasks table. Now let’s add some data in the form of projects and tasks to the database. MariaDB > create table tasks(task_id INT(11) not null auto_increment, project_id INT(11) not null, description varchar(255), primary key(task_id), foreign key(project_id) references projects(project_id)) MariaDB > create table projects(project_id INT(11) not null auto_increment, title varchar(30), description varchar(255), primary key(project_id)) Type '\c' to clear the current input statement. Server version: 10.4.18-MariaDB binary distributionĬopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab, and others. We can start right at the command line of the database. To get started, we can first create the database and tables we need to store some projects and tasks. In this tutorial, we’ll see how to connect to MariaDB using Python to create, read, update, and delete data. The server then accepts that request, performs the action, and sends a response back to the client with the results. The request contains various SQL statements or instructions selecting, updating, or deleting data. To make changes to the database, you send a request to the server. In other words, the database exists on a server. MySQL and MariaDB follow a client-server relationship. They use a relational model with tables to describe relationships between data. MySQL and MariaDB are relational database engines that are very popular systems used with Python.
0 Comments
Leave a Reply. |