In the above SELECT statement, instead of using the asterisk (*), we specified the id and name attributes. Now, to fetch id and names of those who have a salary greater than 800: import sqlite3ĬursorObj.execute('SELECT id, name FROM employees WHERE salary > 800.0') You can use the insert statement to populate the data, or you can enter them manually in the DB browser program. ![]() For this, let’s populate our table with more rows, then execute our query. For example, we want to fetch the ids and names of those employees whose salary is greater than 800. If you want to fetch specific data from the database, you can use the WHERE clause. You can also use the fetchall() in one line as follows: The above code will print out the records in our database as follows: After that, we will loop through the variable and print all values.ĬursorObj.execute('SELECT * FROM employees') To fetch the data from a database, we will execute the SELECT statement and then will use the fetchall() method of the cursor object to store the values into a variable. In the above code, we have defined two methods, the first one establishes a connection and the second method creates a cursor object to execute the create table statement. The code will be like this: import sqlite3ĬursorObj.execute("CREATE TABLE employees(id integer PRIMARY KEY, name text, salary real, department text, position text, hireDate text)") Let’s create employees with the following attributes: employees (id, name, salary, department, position, hireDate)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |